From ee67b7ea92a893c6ce404093d079038bea50175e Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Tue, 24 Sep 2024 15:52:06 +0200 Subject: [PATCH 1/3] DateTime - Deserialize from more formats (default) --- src/Handler/DateHandler.php | 31 ++++++++++++++++++++++++------- tests/Handler/DateHandlerTest.php | 11 +++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Handler/DateHandler.php b/src/Handler/DateHandler.php index bc8fe94a6..e37bbea89 100644 --- a/src/Handler/DateHandler.php +++ b/src/Handler/DateHandler.php @@ -18,6 +18,11 @@ final class DateHandler implements SubscribingHandlerInterface */ private $defaultFormat; + /** + * @var array + */ + private $defaultDeserializationFormats; + /** * @var \DateTimeZone */ @@ -69,9 +74,17 @@ public static function getSubscribingMethods() return $methods; } - public function __construct(string $defaultFormat = \DateTime::ATOM, string $defaultTimezone = 'UTC', bool $xmlCData = true) - { + /** + * @param array $defaultDeserializationFormats + */ + public function __construct( + string $defaultFormat = \DateTime::ATOM, + string $defaultTimezone = 'UTC', + bool $xmlCData = true, + array $defaultDeserializationFormats = [\DateTime::ATOM] + ) { $this->defaultFormat = $defaultFormat; + $this->defaultDeserializationFormats = $defaultDeserializationFormats; $this->defaultTimezone = new \DateTimeZone($defaultTimezone); $this->xmlCData = $xmlCData; } @@ -86,15 +99,15 @@ public function serializeDateTimeInterface( SerializationContext $context ) { if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) { - return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type); + return $visitor->visitSimpleString($date->format($this->getSerializationFormat($type)), $type); } - $format = $this->getFormat($type); + $format = $this->getSerializationFormat($type); if ('U' === $format) { return $visitor->visitInteger((int) $date->format($format), $type); } - return $visitor->visitString($date->format($this->getFormat($type)), $type); + return $visitor->visitString($date->format($this->getSerializationFormat($type)), $type); } /** @@ -285,10 +298,14 @@ private function getDeserializationFormats(array $type): array return is_array($type['params'][2]) ? $type['params'][2] : [$type['params'][2]]; } - return [$this->getFormat($type)]; + if (isset($type['params'][0])) { + return is_array($type['params'][0]) ? $type['params'][0] : [$type['params'][0]]; + } + + return $this->defaultDeserializationFormats; } - private function getFormat(array $type): string + private function getSerializationFormat(array $type): string { return $type['params'][0] ?? $this->defaultFormat; } diff --git a/tests/Handler/DateHandlerTest.php b/tests/Handler/DateHandlerTest.php index 86d4955da..9031f45e7 100644 --- a/tests/Handler/DateHandlerTest.php +++ b/tests/Handler/DateHandlerTest.php @@ -169,4 +169,15 @@ public function testImmutableTimeZoneGetsPreservedWithUnixTimestamp() $actualDateTime->format(\DateTime::RFC3339), ); } + + public function testDefaultFormat() + { + $visitor = new JsonDeserializationVisitor(); + + $type = ['name' => 'DateTime']; + self::assertEquals( + \DateTime::createFromFormat('Y/m/d H:i:s', '2017/06/18 17:32:11', $this->timezone), + $this->handler->deserializeDateTimeFromJson($visitor, '2017-06-18T17:32:11Z', $type), + ); + } } From 1b5c27f15f710dd75e0a48f75d357dc8c8e05e32 Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Sat, 12 Oct 2024 10:26:26 +0200 Subject: [PATCH 2/3] DateTime - Remove BC break --- src/Handler/DateHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Handler/DateHandler.php b/src/Handler/DateHandler.php index e37bbea89..6d738d213 100644 --- a/src/Handler/DateHandler.php +++ b/src/Handler/DateHandler.php @@ -81,10 +81,10 @@ public function __construct( string $defaultFormat = \DateTime::ATOM, string $defaultTimezone = 'UTC', bool $xmlCData = true, - array $defaultDeserializationFormats = [\DateTime::ATOM] + array $defaultDeserializationFormats = [] ) { $this->defaultFormat = $defaultFormat; - $this->defaultDeserializationFormats = $defaultDeserializationFormats; + $this->defaultDeserializationFormats = [] === $defaultDeserializationFormats ? [$defaultFormat] : $defaultDeserializationFormats; $this->defaultTimezone = new \DateTimeZone($defaultTimezone); $this->xmlCData = $xmlCData; } @@ -299,7 +299,7 @@ private function getDeserializationFormats(array $type): array } if (isset($type['params'][0])) { - return is_array($type['params'][0]) ? $type['params'][0] : [$type['params'][0]]; + return [$type['params'][0]]; } return $this->defaultDeserializationFormats; From 206d352f3286825face2fe7cf2b002200c4a538a Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Sat, 12 Oct 2024 14:08:12 +0200 Subject: [PATCH 3/3] DateTime - Rename defaultFormat to defaultSerializationFormat --- src/Handler/DateHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Handler/DateHandler.php b/src/Handler/DateHandler.php index 6d738d213..d8ada409d 100644 --- a/src/Handler/DateHandler.php +++ b/src/Handler/DateHandler.php @@ -16,7 +16,7 @@ final class DateHandler implements SubscribingHandlerInterface /** * @var string */ - private $defaultFormat; + private $defaultSerializationFormat; /** * @var array @@ -83,7 +83,7 @@ public function __construct( bool $xmlCData = true, array $defaultDeserializationFormats = [] ) { - $this->defaultFormat = $defaultFormat; + $this->defaultSerializationFormat = $defaultFormat; $this->defaultDeserializationFormats = [] === $defaultDeserializationFormats ? [$defaultFormat] : $defaultDeserializationFormats; $this->defaultTimezone = new \DateTimeZone($defaultTimezone); $this->xmlCData = $xmlCData; @@ -307,7 +307,7 @@ private function getDeserializationFormats(array $type): array private function getSerializationFormat(array $type): string { - return $type['params'][0] ?? $this->defaultFormat; + return $type['params'][0] ?? $this->defaultSerializationFormat; } public function format(\DateInterval $dateInterval): string