Skip to content

Commit 641472d

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [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 [CI] Add step to verify symfony/deprecation-contracts requirements
2 parents 8c5fb71 + ceadb4e commit 641472d

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
}
@@ -108,6 +105,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
108105
}
109106

110107
try {
108+
$timezone = $this->getTimezone($context);
109+
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
110+
111111
if (null !== $dateTimeFormat) {
112112
$object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
113113

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
/**
@@ -123,26 +123,26 @@ public function testDenormalizeIntervalsWithOmittedPartsBeingZero()
123123

124124
public function testDenormalizeExpectsString()
125125
{
126-
$this->expectException(InvalidArgumentException::class);
126+
$this->expectException(NotNormalizableValueException::class);
127127
$this->normalizer->denormalize(1234, \DateInterval::class);
128128
}
129129

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

137137
public function testDenormalizeInvalidDataThrowsException()
138138
{
139-
$this->expectException(UnexpectedValueException::class);
139+
$this->expectException(NotNormalizableValueException::class);
140140
$this->normalizer->denormalize('invalid interval', \DateInterval::class);
141141
}
142142

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

Tests/Normalizer/DateTimeNormalizerTest.php

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

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

0 commit comments

Comments
 (0)