minor #16238 [Form] Remove unneeded catch and re-throw in DateTimeToStringTransformer (MisatoTremor)
This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes #16238).
Discussion
----------
[Form] Remove unneeded catch and re-throw in DateTimeToStringTransformer
| Q | A
| ------------- | ---
| Fixed tickets | #16237
| License | MIT
Also fixed PHPDoc param for ``transform``.
Commits
-------
2c9b283
[Form] Simplify DateTimeToStringTransformer Avoid unneeded catch and re-throw of the same exception.
This commit is contained in:
commit
66c99a0b95
|
@ -90,7 +90,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
||||||
* Transforms a DateTime object into a date string with the configured format
|
* Transforms a DateTime object into a date string with the configured format
|
||||||
* and timezone.
|
* 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
|
* @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.');
|
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 {
|
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
|
// On PHP versions < 5.3.7 we need to emulate the pipe operator
|
||||||
// and reset parts not given in the format to their equivalent
|
// and reset parts not given in the format to their equivalent
|
||||||
// of the UNIX base timestamp.
|
// of the UNIX base timestamp.
|
||||||
|
@ -224,8 +224,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
||||||
if ($this->inputTimezone !== $this->outputTimezone) {
|
if ($this->inputTimezone !== $this->outputTimezone) {
|
||||||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
||||||
}
|
}
|
||||||
} catch (TransformationFailedException $e) {
|
|
||||||
throw $e;
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue