public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
{
$dateFormat = $this->getDefaultDateFormat($configuration);
if (is_string($source)) {
$dateAsString = $source;
} elseif (is_integer($source)) {
$dateAsString = strval($source);
} else {
if (isset($source['date']) && is_string($source['date'])) {
$dateAsString = $source['date'];
} elseif (isset($source['date']) && is_integer($source['date'])) {
$dateAsString = strval($source['date']);
} elseif ($this->isDatePartKeysProvided($source)) {
if ($source['day'] < 1 || $source['month'] < 1 || $source['year'] < 1) {
return new Error('Could not convert the given date parts into a DateTime object because one or more parts were 0.', 1333032779);
}
$dateAsString = sprintf('%d-%d-%d', $source['year'], $source['month'], $source['day']);
} else {
throw new TypeConverterException('Could not convert the given source into a DateTime object because it was not an array with a valid date as a string', 1308003914);
}
if (isset($source['dateFormat']) && $source['dateFormat'] !== '') {
$dateFormat = $source['dateFormat'];
}
}
if ($dateAsString === '') {
return null;
}
if (ctype_digit($dateAsString) && $configuration === null && (!is_array($source) || !isset($source['dateFormat']))) {
$dateFormat = 'U';
}
if (is_array($source) && isset($source['timezone']) && strlen($source['timezone']) !== 0) {
try {
$timezone = new \DateTimeZone($source['timezone']);
} catch (\Exception $exception) {
throw new TypeConverterException('The specified timezone "' . $source['timezone'] . '" is invalid.', 1308240974);
}
$date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
} else {
$date = $targetType::createFromFormat($dateFormat, $dateAsString);
}
if ($date === false) {
return new Error('The date "%s" was not recognized (for format "%s").', 1307719788, [$dateAsString, $dateFormat]);
}
if (is_array($source)) {
$this->overrideTimeIfSpecified($date, $source);
}
return $date;
}