From 3f64db3b3a70f0a2649643b6f5ba1ee2b7ad36d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 11 May 2021 19:11:22 +0200 Subject: [PATCH] remove 'u' from date normalizer --- .../Serializer/Normalizer/DateNormalizer.php | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/DateNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/DateNormalizer.php index fd902b184..8e333a2a4 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/DateNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/DateNormalizer.php @@ -20,15 +20,16 @@ namespace Chill\MainBundle\Serializer\Normalizer; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; -class DateNormalizer implements NormalizerInterface +class DateNormalizer implements NormalizerInterface, DenormalizerInterface { public function normalize($date, string $format = null, array $context = array()) { /** @var \DateTimeInterface $date */ return [ - 'datetime' => $date->format(\DateTimeInterface::ISO8601), - 'u' => $date->getTimestamp() + 'datetime' => $date->format(\DateTimeInterface::ISO8601) ]; } @@ -36,4 +37,24 @@ class DateNormalizer implements NormalizerInterface { return $data instanceof \DateTimeInterface; } + + public function denormalize($data, string $type, string $format = null, array $context = []) + { + switch ($type) { + case \DateTime::class: + return \DateTime::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']); + case \DateTimeInterface::class: + case \DateTimeImmutable::class: + default: + return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']); + } + } + + public function supportsDenormalization($data, string $type, string $format = null): bool + { + return $type === \DateTimeInterface::class || + $type === \DateTime::class || + $type === \DateTimeImmutable::class || + (\is_array($data) && array_key_exists('datetime', $data)); + } }