Skip to content

Commit 230ba6b

Browse files
committed
Fix using DateIntervalNormalizer with union types
1 parent 6cfd0bf commit 230ba6b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\PropertyInfo\Type;
1415
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1617

1718
/**
1819
* Normalizes an instance of {@see \DateInterval} to an interval string.
@@ -70,17 +71,16 @@ public function hasCacheableSupportsMethod(): bool
7071
*
7172
* @return \DateInterval
7273
*
73-
* @throws InvalidArgumentException
74-
* @throws UnexpectedValueException
74+
* @throws NotNormalizableValueException
7575
*/
7676
public function denormalize($data, string $type, string $format = null, array $context = [])
7777
{
7878
if (!\is_string($data)) {
79-
throw new InvalidArgumentException(sprintf('Data expected to be a string, "%s" given.', get_debug_type($data)));
79+
throw NotNormalizableValueException::createForUnexpectedDataType('Data expected to be a string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
8080
}
8181

8282
if (!$this->isISO8601($data)) {
83-
throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
83+
throw NotNormalizableValueException::createForUnexpectedDataType('Expected a valid ISO 8601 interval string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
8484
}
8585

8686
$dateIntervalFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
@@ -98,7 +98,7 @@ public function denormalize($data, string $type, string $format = null, array $c
9898
}
9999
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?:(?P<$1>\d+)$2)?', preg_replace('/(T.*)$/', '($1)?', $dateIntervalFormat)).'$/';
100100
if (!preg_match($valuePattern, $data)) {
101-
throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
101+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, false);
102102
}
103103

104104
try {
@@ -114,8 +114,10 @@ public function denormalize($data, string $type, string $format = null, array $c
114114
}
115115

116116
return new \DateInterval($data);
117+
} catch (NotNormalizableValueException $e) {
118+
throw $e;
117119
} catch (\Exception $e) {
118-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
120+
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, false, $e->getCode(), $e);
119121
}
120122
}
121123

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy