Skip to content

Commit

Permalink
Allow feature only for deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 19, 2020
1 parent 0aefabc commit a2c5a59
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 39 deletions.
25 changes: 12 additions & 13 deletions doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,27 @@ Available Types:
+------------------------------------------------------------+--------------------------------------------------+
| DateTime | PHP's DateTime object (default format*/timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'formats'> | PHP's DateTime object (custom format/default |
| | timezone). Formats can either be a string or an |
| | array of string. |
| DateTime<'format'> | PHP's DateTime object (custom format/default |
| | timezone). |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'formats', 'zone'> | PHP's DateTime object (custom format/timezone) |
| DateTime<'format', 'zone'> | PHP's DateTime object (custom format/timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTime<'formats', 'zone', 'deserializeFormats'> | PHP's DateTime object (custom format/timezone, |
| DateTime<'format', 'zone', 'deserializeFormats'> | PHP's DateTime object (custom format/timezone, |
| | deserialize format). If you do not want to |
| | specify a specific timezone, use an empty |
| | string (''). |
| | string (''). DeserializeFormats can either be a |
| | string or an array of string. |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable | PHP's DateTimeImmutable object (default format*/ |
| | timezone). Formats can either be a string or an |
| | array of string. |
| | timezone). |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'formats'> | PHP's DateTimeImmutable object (custom format/ |
| DateTimeImmutable<'format'> | PHP's DateTimeImmutable object (custom format/ |
| | default timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'formats', 'zone'> | PHP's DateTimeImmutable object (custom format/ |
| DateTimeImmutable<'format', 'zone'> | PHP's DateTimeImmutable object (custom format/ |
| | timezone) |
+------------------------------------------------------------+--------------------------------------------------+
| DateTimeImmutable<'formats', 'zone', 'deserializeFormats'> | PHP's DateTimeImmutable object (custom format/ |
| DateTimeImmutable<'format', 'zone', 'deserializeFormats'> | PHP's DateTimeImmutable object (custom format/ |
| | timezone/deserialize format). If you do not want |
| | to specify a specific timezone, use an empty |
| | string (''). DeserializeFormats can either be a |
Expand Down Expand Up @@ -467,7 +466,7 @@ Examples:
private $endAt;
/**
* @Type("DateTime<['Y-m-d', 'Y/m/d']>")
* @Type("DateTime<'Y-m-d', '', ['Y-m-d', 'Y/m/d']>")
*/
private $publishedAt;
Expand All @@ -482,7 +481,7 @@ Examples:
private $updatedAt;
/**
* @Type("DateTimeImmutable<['Y-m-d', 'Y/m/d']>")
* @Type("DateTimeImmutable<'Y-m-d', '', ['Y-m-d', 'Y/m/d']>")
*/
private $deletedAt;
Expand Down
36 changes: 11 additions & 25 deletions src/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,16 @@ private function serializeDateTimeInterface(
array $type,
SerializationContext $context
) {
$formats = $this->getFormats($type);

foreach ($formats as $format) {
$dateFormatted = $date->format($format);

if (false !== $dateFormatted) {
if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) {
return $visitor->visitSimpleString($dateFormatted, $type);
}

if ('U' === $format) {
return $visitor->visitInteger((int) $dateFormatted, $type);
}
if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) {
return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type);
}

return $visitor->visitString($dateFormatted, $type);
}
$format = $this->getFormat($type);
if ('U' === $format) {
return $visitor->visitInteger((int) $date->format($format), $type);
}

throw new RuntimeException(sprintf('The date "%s" could not be formatted', $date));
return $visitor->visitString($date->format($this->getFormat($type)), $type);
}

/**
Expand Down Expand Up @@ -278,21 +269,16 @@ private function getDeserializationFormats(array $type): array
if (isset($type['params'][2])) {
return is_array($type['params'][2]) ? $type['params'][2] : [$type['params'][2]];
}
if (isset($type['params'][0])) {
return is_array($type['params'][0]) ? $type['params'][0] : [$type['params'][0]];
}
return [$this->defaultFormat];

return [$this->getFormat($type)];
}

/**
* @param array $type
*/
private function getFormats(array $type): array
private function getFormat(array $type): string
{
if (isset($type['params'][0])) {
return is_array($type['params'][0]) ? $type['params'][0] : [$type['params'][0]];
}
return [$this->defaultFormat];
return $type['params'][0] ?? $this->defaultFormat;
}

public function format(\DateInterval $dateInterval): string
Expand Down
2 changes: 1 addition & 1 deletion tests/Handler/DateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getParams()
[['Y-m-d']],
[['Y-m-d', '', 'Y-m-d|']],
[['Y-m-d', '', 'Y']],
[[['Y-m-d', 'Y/m/d'], '', 'Y']],
[['Y-m-d', '', ['Y-m-d', 'Y/m/d']]],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ public function getDateTime()
{
return [
['date_time', new \DateTime('2011-08-30 00:00', new \DateTimeZone('UTC')), 'DateTime'],
['date_time_multi_format', new \DateTime('2011-08-30 00:00', new \DateTimeZone('UTC')), "DateTime<'Y-m-d', '', ['Y-m-d','Y-m-d\TH:i:sP']>"],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected function getContent($key)
$outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
$outputs['date_time'] = '"2011-08-30T00:00:00+00:00"';
$outputs['date_time_immutable'] = '"2011-08-30T00:00:00+00:00"';
$outputs['date_time_multi_format'] = '"2011-08-30T00:00:00+00:00"';
$outputs['timestamp'] = '{"timestamp":1455148800}';
$outputs['timestamp_prev'] = '{"timestamp":"1455148800"}';
$outputs['date_interval'] = '"PT45M"';
Expand Down
2 changes: 2 additions & 0 deletions tests/Serializer/xml/date_time_multi_format.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<result><![CDATA[2011-08-30T00:00:00+00:00]]></result>
2 changes: 2 additions & 0 deletions tests/Serializer/xml/date_time_multi_format_no_cdata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>2011-08-30T00:00:00+00:00</result>

0 comments on commit a2c5a59

Please sign in to comment.