Skip to content

Commit 43404be

Browse files
authored
Merge pull request #1559 from iKsSs/date-deserialization
DateTime - Deserialize from more formats (default)
2 parents 25b5ebe + 206d352 commit 43404be

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/Handler/DateHandler.php

+27-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ final class DateHandler implements SubscribingHandlerInterface
1616
/**
1717
* @var string
1818
*/
19-
private $defaultFormat;
19+
private $defaultSerializationFormat;
20+
21+
/**
22+
* @var array<string>
23+
*/
24+
private $defaultDeserializationFormats;
2025

2126
/**
2227
* @var \DateTimeZone
@@ -69,9 +74,17 @@ public static function getSubscribingMethods()
6974
return $methods;
7075
}
7176

72-
public function __construct(string $defaultFormat = \DateTime::ATOM, string $defaultTimezone = 'UTC', bool $xmlCData = true)
73-
{
74-
$this->defaultFormat = $defaultFormat;
77+
/**
78+
* @param array<string> $defaultDeserializationFormats
79+
*/
80+
public function __construct(
81+
string $defaultFormat = \DateTime::ATOM,
82+
string $defaultTimezone = 'UTC',
83+
bool $xmlCData = true,
84+
array $defaultDeserializationFormats = []
85+
) {
86+
$this->defaultSerializationFormat = $defaultFormat;
87+
$this->defaultDeserializationFormats = [] === $defaultDeserializationFormats ? [$defaultFormat] : $defaultDeserializationFormats;
7588
$this->defaultTimezone = new \DateTimeZone($defaultTimezone);
7689
$this->xmlCData = $xmlCData;
7790
}
@@ -86,15 +99,15 @@ public function serializeDateTimeInterface(
8699
SerializationContext $context
87100
) {
88101
if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) {
89-
return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type);
102+
return $visitor->visitSimpleString($date->format($this->getSerializationFormat($type)), $type);
90103
}
91104

92-
$format = $this->getFormat($type);
105+
$format = $this->getSerializationFormat($type);
93106
if ('U' === $format) {
94107
return $visitor->visitInteger((int) $date->format($format), $type);
95108
}
96109

97-
return $visitor->visitString($date->format($this->getFormat($type)), $type);
110+
return $visitor->visitString($date->format($this->getSerializationFormat($type)), $type);
98111
}
99112

100113
/**
@@ -285,12 +298,16 @@ private function getDeserializationFormats(array $type): array
285298
return is_array($type['params'][2]) ? $type['params'][2] : [$type['params'][2]];
286299
}
287300

288-
return [$this->getFormat($type)];
301+
if (isset($type['params'][0])) {
302+
return [$type['params'][0]];
303+
}
304+
305+
return $this->defaultDeserializationFormats;
289306
}
290307

291-
private function getFormat(array $type): string
308+
private function getSerializationFormat(array $type): string
292309
{
293-
return $type['params'][0] ?? $this->defaultFormat;
310+
return $type['params'][0] ?? $this->defaultSerializationFormat;
294311
}
295312

296313
public function format(\DateInterval $dateInterval): string

tests/Handler/DateHandlerTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,15 @@ public function testImmutableTimeZoneGetsPreservedWithUnixTimestamp()
169169
$actualDateTime->format(\DateTime::RFC3339),
170170
);
171171
}
172+
173+
public function testDefaultFormat()
174+
{
175+
$visitor = new JsonDeserializationVisitor();
176+
177+
$type = ['name' => 'DateTime'];
178+
self::assertEquals(
179+
\DateTime::createFromFormat('Y/m/d H:i:s', '2017/06/18 17:32:11', $this->timezone),
180+
$this->handler->deserializeDateTimeFromJson($visitor, '2017-06-18T17:32:11Z', $type),
181+
);
182+
}
172183
}

0 commit comments

Comments
 (0)