Skip to content

Commit 8045bc5

Browse files
committed
Simplify DateIntervalToStringTransformer invalid value handling
1 parent 72cca79 commit 8045bc5

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateIntervalToStringTransformer.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,17 @@ public function reverseTransform($value)
8989
if (!is_string($value)) {
9090
throw new TransformationFailedException('Expected a string.');
9191
}
92+
if (!$this->isISO8601($value)) {
93+
throw new TransformationFailedException('Non ISO 8601 date strings are not supported yet');
94+
}
95+
$valuePattern = '/^'.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $this->format).'$/';
96+
if (!preg_match($valuePattern, $value)) {
97+
throw new TransformationFailedException(
98+
sprintf('Value "%s" contains intervals not accepted by format "%s".', $value, $this->format)
99+
);
100+
}
92101
try {
93-
if ($this->isISO8601($value)) {
94-
$valuePattern = '/^'.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $this->format).'$/';
95-
if (!preg_match($valuePattern, $value)) {
96-
throw new TransformationFailedException(
97-
sprintf('Value "%s" contains intervals not accepted by format "%s".', $value, $this->format)
98-
);
99-
}
100-
$dateInterval = new \DateInterval($value);
101-
} else {
102-
throw new TransformationFailedException('Non ISO 8601 date strings are not supported yet');
103-
}
104-
} catch (TransformationFailedException $e) {
105-
throw $e;
102+
$dateInterval = new \DateInterval($value);
106103
} catch (\Exception $e) {
107104
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
108105
}

0 commit comments

Comments
 (0)