Skip to content

Commit 633f156

Browse files
committed
[Serializer] Handle defaultContext for DateTimeNormalizer
1 parent 923b2a8 commit 633f156

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,22 @@ public function supportsNormalization($data, string $format = null)
8888
*/
8989
public function denormalize($data, string $type, string $format = null, array $context = [])
9090
{
91-
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
92-
$timezone = $this->getTimezone($context);
93-
9491
if (\is_int($data) || \is_float($data)) {
95-
switch ($dateTimeFormat) {
96-
case 'U': $data = sprintf('%d', $data); break;
97-
case 'U.u': $data = sprintf('%.6F', $data); break;
98-
}
92+
$data = match ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) {
93+
'U' => sprintf('%d', $data),
94+
'U.u' => sprintf('%.6F', $data),
95+
default => $data,
96+
};
9997
}
10098

10199
if (!\is_string($data) || '' === trim($data)) {
102100
throw NotNormalizableValueException::createForUnexpectedDataType('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
103101
}
104102

105103
try {
104+
$timezone = $this->getTimezone($context);
105+
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
106+
106107
if (null !== $dateTimeFormat) {
107108
$object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
108109

src/Symfony/Component/Serializer/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, context: [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)
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