Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime - Deserialize from more formats (default) #1559

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
);
}
}
Loading