@@ -16,7 +16,12 @@ final class DateHandler implements SubscribingHandlerInterface
16
16
/**
17
17
* @var string
18
18
*/
19
- private $ defaultFormat ;
19
+ private $ defaultSerializationFormat ;
20
+
21
+ /**
22
+ * @var array<string>
23
+ */
24
+ private $ defaultDeserializationFormats ;
20
25
21
26
/**
22
27
* @var \DateTimeZone
@@ -69,9 +74,17 @@ public static function getSubscribingMethods()
69
74
return $ methods ;
70
75
}
71
76
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 ;
75
88
$ this ->defaultTimezone = new \DateTimeZone ($ defaultTimezone );
76
89
$ this ->xmlCData = $ xmlCData ;
77
90
}
@@ -86,15 +99,15 @@ public function serializeDateTimeInterface(
86
99
SerializationContext $ context
87
100
) {
88
101
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 );
90
103
}
91
104
92
- $ format = $ this ->getFormat ($ type );
105
+ $ format = $ this ->getSerializationFormat ($ type );
93
106
if ('U ' === $ format ) {
94
107
return $ visitor ->visitInteger ((int ) $ date ->format ($ format ), $ type );
95
108
}
96
109
97
- return $ visitor ->visitString ($ date ->format ($ this ->getFormat ($ type )), $ type );
110
+ return $ visitor ->visitString ($ date ->format ($ this ->getSerializationFormat ($ type )), $ type );
98
111
}
99
112
100
113
/**
@@ -285,12 +298,16 @@ private function getDeserializationFormats(array $type): array
285
298
return is_array ($ type ['params ' ][2 ]) ? $ type ['params ' ][2 ] : [$ type ['params ' ][2 ]];
286
299
}
287
300
288
- return [$ this ->getFormat ($ type )];
301
+ if (isset ($ type ['params ' ][0 ])) {
302
+ return [$ type ['params ' ][0 ]];
303
+ }
304
+
305
+ return $ this ->defaultDeserializationFormats ;
289
306
}
290
307
291
- private function getFormat (array $ type ): string
308
+ private function getSerializationFormat (array $ type ): string
292
309
{
293
- return $ type ['params ' ][0 ] ?? $ this ->defaultFormat ;
310
+ return $ type ['params ' ][0 ] ?? $ this ->defaultSerializationFormat ;
294
311
}
295
312
296
313
public function format (\DateInterval $ dateInterval ): string
0 commit comments