[Form] Simplify DateTimeToStringTransformer

Avoid unneeded catch and re-throw of the same exception.
This commit is contained in:
Steffen Roßkamp 2015-10-14 12:34:59 +02:00 committed by Fabien Potencier
parent c94f18693e
commit 2c9b283e01
1 changed files with 15 additions and 17 deletions

View File

@ -90,7 +90,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
* Transforms a DateTime object into a date string with the configured format
* and timezone.
*
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
* @param \DateTime|\DateTimeInterface $value A DateTime object
*
* @return string A value as produced by PHP's date() function
*
@ -142,21 +142,21 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
throw new TransformationFailedException('Expected a string.');
}
$outputTz = new \DateTimeZone($this->outputTimezone);
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
$lastErrors = \DateTime::getLastErrors();
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
throw new TransformationFailedException(
implode(', ', array_merge(
array_values($lastErrors['warnings']),
array_values($lastErrors['errors'])
))
);
}
try {
$outputTz = new \DateTimeZone($this->outputTimezone);
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
$lastErrors = \DateTime::getLastErrors();
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
throw new TransformationFailedException(
implode(', ', array_merge(
array_values($lastErrors['warnings']),
array_values($lastErrors['errors'])
))
);
}
// On PHP versions < 5.3.7 we need to emulate the pipe operator
// and reset parts not given in the format to their equivalent
// of the UNIX base timestamp.
@ -224,8 +224,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
if ($this->inputTimezone !== $this->outputTimezone) {
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
}
} catch (TransformationFailedException $e) {
throw $e;
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}