@@ -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
0 commit comments