1
1
<?php
2
-
3
2
namespace GoetasWebservices \Xsd \XsdToPhpRuntime \Jms \Handler ;
4
3
5
4
use JMS \Serializer \Context ;
@@ -59,6 +58,12 @@ public static function getSubscribingMethods()
59
58
'format ' => 'xml ' ,
60
59
'method ' => 'deserializeDateIntervalXml ' ,
61
60
),
61
+ array (
62
+ 'type ' => 'DateInterval ' ,
63
+ 'direction ' => GraphNavigator::DIRECTION_SERIALIZATION ,
64
+ 'format ' => 'xml ' ,
65
+ 'method ' => 'serializeDateInterval ' ,
66
+ ),
62
67
);
63
68
}
64
69
@@ -68,10 +73,9 @@ public function __construct($defaultTimezone = 'UTC')
68
73
69
74
}
70
75
71
- public function deserializeDateIntervalXml (XmlDeserializationVisitor $ visitor , $ data , array $ type )
72
- {
76
+ public function deserializeDateIntervalXml (XmlDeserializationVisitor $ visitor , $ data , array $ type ){
73
77
$ attributes = $ data ->attributes ('xsi ' , true );
74
- if (isset ($ attributes ['nil ' ][0 ]) && (string )$ attributes ['nil ' ][0 ] === 'true ' ) {
78
+ if (isset ($ attributes ['nil ' ][0 ]) && (string ) $ attributes ['nil ' ][0 ] === 'true ' ) {
75
79
return null ;
76
80
}
77
81
@@ -85,9 +89,39 @@ public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $
85
89
return $ dateInterval ;
86
90
}
87
91
88
- public function serializeDate (XmlSerializationVisitor $ visitor , \DateTime $ date , array $ type , Context $ context )
92
+ public function serializeDateInterval (XmlSerializationVisitor $ visitor , \DateInterval $ interval , array $ type , Context $ context )
89
93
{
94
+ $ date = array_filter (array (
95
+ 'Y ' => $ interval ->y ,
96
+ 'M ' => $ interval ->m ,
97
+ 'D ' => $ interval ->d
98
+ ));
99
+
100
+ // Reading all non-zero time parts.
101
+ $ time = array_filter (array (
102
+ 'H ' => $ interval ->h ,
103
+ 'M ' => $ interval ->i ,
104
+ 'S ' => $ interval ->s
105
+ ));
106
+
107
+ $ specString = 'P ' ;
108
+
109
+ // Adding each part to the spec-string.
110
+ foreach ($ date as $ key => $ value ) {
111
+ $ specString .= $ value . $ key ;
112
+ }
113
+ if (count ($ time ) > 0 ) {
114
+ $ specString .= 'T ' ;
115
+ foreach ($ time as $ key => $ value ) {
116
+ $ specString .= $ value . $ key ;
117
+ }
118
+ }
90
119
120
+ return $ visitor ->visitSimpleString ($ specString , $ type , $ context );
121
+ }
122
+
123
+ public function serializeDate (XmlSerializationVisitor $ visitor , \DateTime $ date , array $ type , Context $ context )
124
+ {
91
125
$ v = $ date ->format ('Y-m-d ' );
92
126
93
127
return $ visitor ->visitSimpleString ($ v , $ type , $ context );
0 commit comments