From f425f15f5f39b2933a9fde3df7a1551eb81941e8 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Wed, 25 Oct 2023 04:53:43 +0200 Subject: [PATCH] [Serializer] Handle defaultContext for DateTimeNormalizer --- .../Serializer/Normalizer/DateTimeNormalizer.php | 8 ++++---- .../Tests/Normalizer/DateTimeNormalizerTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index 9736d8e78e4a..3b8b7fe1f0e9 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -88,11 +88,8 @@ public function supportsNormalization($data, string $format = null) */ public function denormalize($data, string $type, string $format = null, array $context = []) { - $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; - $timezone = $this->getTimezone($context); - if (\is_int($data) || \is_float($data)) { - switch ($dateTimeFormat) { + switch ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) { case 'U': $data = sprintf('%d', $data); break; case 'U.u': $data = sprintf('%.6F', $data); break; } @@ -103,6 +100,9 @@ public function denormalize($data, string $type, string $format = null, array $c } try { + $timezone = $this->getTimezone($context); + $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; + if (null !== $dateTimeFormat) { $object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php index 25b7c784fe0e..ee82f319d673 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php @@ -280,6 +280,22 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex $this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']); } + public function testDenormalizeTimestampWithFormatInContext() + { + $normalizer = new DateTimeNormalizer(); + $denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']); + + $this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s')); + } + + public function testDenormalizeTimestampWithFormatInDefaultContext() + { + $normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'U']); + $denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class); + + $this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s')); + } + public function testDenormalizeDateTimeStringWithDefaultContextFormat() { $format = 'd/m/Y'; 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