Skip to content

Commit b066c78

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: [6.3] Remove unused test fixture [5.4] Remove unused test fixtures [Dotent] Add PHPDoc for `$overrideExistingVars` [SecurityBundle] Fix missing login-link element in xsd schema [Validator] Add missing Chinese translations #51934 [Serializer] Fix using `DateIntervalNormalizer` with union types [Validator] fix: add missing translations for for Thai (th) fix #52273 [doctrine-messenger] DB table locks on messenger_messages with many failures [Serializer] Handle defaultContext for DateTimeNormalizer declare constructor argument as optional for backwards compatibility [CI] Add step to verify symfony/deprecation-contracts requirements
2 parents cf7f907 + 641472d commit b066c78

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

Normalizer/DateIntervalNormalizer.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1616

1717
/**
1818
* Normalizes an instance of {@see \DateInterval} to an interval string.
@@ -73,17 +73,16 @@ public function hasCacheableSupportsMethod(): bool
7373
}
7474

7575
/**
76-
* @throws InvalidArgumentException
77-
* @throws UnexpectedValueException
76+
* @throws NotNormalizableValueException
7877
*/
7978
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): \DateInterval
8079
{
8180
if (!\is_string($data)) {
82-
throw new InvalidArgumentException(sprintf('Data expected to be a string, "%s" given.', get_debug_type($data)));
81+
throw NotNormalizableValueException::createForUnexpectedDataType('Data expected to be a string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
8382
}
8483

8584
if (!$this->isISO8601($data)) {
86-
throw new UnexpectedValueException('Expected a valid ISO 8601 interval string.');
85+
throw NotNormalizableValueException::createForUnexpectedDataType('Expected a valid ISO 8601 interval string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
8786
}
8887

8988
$dateIntervalFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
@@ -101,7 +100,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
101100
}
102101
$valuePattern = '/^'.$signPattern.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?:(?P<$1>\d+)$2)?', preg_replace('/(T.*)$/', '($1)?', $dateIntervalFormat)).'$/';
103102
if (!preg_match($valuePattern, $data)) {
104-
throw new UnexpectedValueException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat));
103+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Value "%s" contains intervals not accepted by format "%s".', $data, $dateIntervalFormat), $data, ['string'], $context['deserialization_path'] ?? null, false);
105104
}
106105

107106
try {
@@ -118,7 +117,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
118117

119118
return new \DateInterval($data);
120119
} catch (\Exception $e) {
121-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
120+
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, ['string'], $context['deserialization_path'] ?? null, false, $e->getCode(), $e);
122121
}
123122
}
124123

Normalizer/DateTimeNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,8 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
9393
*/
9494
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): \DateTimeInterface
9595
{
96-
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
97-
$timezone = $this->getTimezone($context);
98-
9996
if (\is_int($data) || \is_float($data)) {
100-
switch ($dateTimeFormat) {
97+
switch ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) {
10198
case 'U': $data = sprintf('%d', $data); break;
10299
case 'U.u': $data = sprintf('%.6F', $data); break;
103100
}
@@ -112,6 +109,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
112109
$type = \DateTimeImmutable::class;
113110
}
114111

112+
$timezone = $this->getTimezone($context);
113+
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
114+
115115
if (null !== $dateTimeFormat) {
116116
if (false !== $object = $type::createFromFormat($dateTimeFormat, $data, $timezone)) {
117117
return $object;

Tests/Normalizer/DateIntervalNormalizerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1717
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
1818

1919
/**
@@ -120,26 +120,26 @@ public function testDenormalizeIntervalsWithOmittedPartsBeingZero()
120120

121121
public function testDenormalizeExpectsString()
122122
{
123-
$this->expectException(InvalidArgumentException::class);
123+
$this->expectException(NotNormalizableValueException::class);
124124
$this->normalizer->denormalize(1234, \DateInterval::class);
125125
}
126126

127127
public function testDenormalizeNonISO8601IntervalStringThrowsException()
128128
{
129-
$this->expectException(UnexpectedValueException::class);
129+
$this->expectException(NotNormalizableValueException::class);
130130
$this->expectExceptionMessage('Expected a valid ISO 8601 interval string.');
131131
$this->normalizer->denormalize('10 years 2 months 3 days', \DateInterval::class, null);
132132
}
133133

134134
public function testDenormalizeInvalidDataThrowsException()
135135
{
136-
$this->expectException(UnexpectedValueException::class);
136+
$this->expectException(NotNormalizableValueException::class);
137137
$this->normalizer->denormalize('invalid interval', \DateInterval::class);
138138
}
139139

140140
public function testDenormalizeFormatMismatchThrowsException()
141141
{
142-
$this->expectException(UnexpectedValueException::class);
142+
$this->expectException(NotNormalizableValueException::class);
143143
$this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class, null, [DateIntervalNormalizer::FORMAT_KEY => 'P%yY%mM%dD']);
144144
}
145145

Tests/Normalizer/DateTimeNormalizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex
277277
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
278278
}
279279

280+
public function testDenormalizeTimestampWithFormatInContext()
281+
{
282+
$normalizer = new DateTimeNormalizer();
283+
$denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
284+
285+
$this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s'));
286+
}
287+
288+
public function testDenormalizeTimestampWithFormatInDefaultContext()
289+
{
290+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'U']);
291+
$denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class);
292+
293+
$this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s'));
294+
}
295+
280296
public function testDenormalizeDateTimeStringWithDefaultContextFormat()
281297
{
282298
$format = 'd/m/Y';

0 commit comments

Comments
 (0)