public function reverseTransform($value)
{
if (null === $value) {
return null;
}
if (!is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}
if ('' === implode('', $value)) {
return null;
}
$emptyFields = array();
foreach ($this->fields as $field) {
if (!isset($value[$field])) {
$emptyFields[] = $field;
}
}
if (count($emptyFields) > 0) {
throw new TransformationFailedException(
sprintf('The fields "%s" should not be empty', implode('", "', $emptyFields)
));
}
if (isset($value['month']) && !ctype_digit($value['month']) && !is_int($value['month'])) {
throw new TransformationFailedException('This month is invalid');
}
if (isset($value['day']) && !ctype_digit($value['day']) && !is_int($value['day'])) {
throw new TransformationFailedException('This day is invalid');
}
if (isset($value['year']) && !ctype_digit($value['year']) && !is_int($value['year'])) {
throw new TransformationFailedException('This year is invalid');
}
if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) {
throw new TransformationFailedException('This is an invalid date');
}
try {
$dateTime = new \DateTime(sprintf(
'%s-%s-%s %s:%s:%s %s',
empty($value['year']) ? '1970' : $value['year'],
empty($value['month']) ? '1' : $value['month'],
empty($value['day']) ? '1' : $value['day'],
empty($value['hour']) ? '0' : $value['hour'],
empty($value['minute']) ? '0' : $value['minute'],
empty($value['second']) ? '0' : $value['second'],
$this->outputTimezone
));
if ($this->inputTimezone !== $this->outputTimezone) {
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
}
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
return $dateTime;
}