Skip to content

Commit

Permalink
Merge pull request #1559 from iKsSs/date-deserialization
Browse files Browse the repository at this point in the history
DateTime - Deserialize from more formats (default)
  • Loading branch information
scyzoryck authored Oct 16, 2024
2 parents 25b5ebe + 206d352 commit 43404be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ final class DateHandler implements SubscribingHandlerInterface
/**
* @var string
*/
private $defaultFormat;
private $defaultSerializationFormat;

/**
* @var array<string>
*/
private $defaultDeserializationFormats;

/**
* @var \DateTimeZone
Expand Down Expand Up @@ -69,9 +74,17 @@ public static function getSubscribingMethods()
return $methods;
}

public function __construct(string $defaultFormat = \DateTime::ATOM, string $defaultTimezone = 'UTC', bool $xmlCData = true)
{
$this->defaultFormat = $defaultFormat;
/**
* @param array<string> $defaultDeserializationFormats
*/
public function __construct(
string $defaultFormat = \DateTime::ATOM,
string $defaultTimezone = 'UTC',
bool $xmlCData = true,
array $defaultDeserializationFormats = []
) {
$this->defaultSerializationFormat = $defaultFormat;
$this->defaultDeserializationFormats = [] === $defaultDeserializationFormats ? [$defaultFormat] : $defaultDeserializationFormats;
$this->defaultTimezone = new \DateTimeZone($defaultTimezone);
$this->xmlCData = $xmlCData;
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -285,12 +298,16 @@ 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 [$type['params'][0]];
}

return $this->defaultDeserializationFormats;
}

private function getFormat(array $type): string
private function getSerializationFormat(array $type): string
{
return $type['params'][0] ?? $this->defaultFormat;
return $type['params'][0] ?? $this->defaultSerializationFormat;
}

public function format(\DateInterval $dateInterval): string
Expand Down
11 changes: 11 additions & 0 deletions tests/Handler/DateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
}

0 comments on commit 43404be

Please sign in to comment.