1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
6
|
|
|
use JMS\Serializer\GraphNavigator; |
7
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
8
|
|
|
use JMS\Serializer\XmlDeserializationVisitor; |
9
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
class XmlSchemaDateHandler implements SubscribingHandlerInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $defaultTimezone; |
16
|
|
|
|
17
|
|
|
public static function getSubscribingMethods() |
18
|
|
|
{ |
19
|
|
|
return array( |
20
|
|
|
array( |
21
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
22
|
|
|
'format' => 'xml', |
23
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
24
|
|
|
'method' => 'deserializeDate' |
25
|
|
|
), |
26
|
|
|
array( |
27
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
28
|
|
|
'format' => 'xml', |
29
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
30
|
|
|
'method' => 'serializeDate' |
31
|
|
|
), |
32
|
|
|
array( |
33
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
34
|
|
|
'format' => 'xml', |
35
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
36
|
|
|
'method' => 'deserializeDateTime' |
37
|
|
|
), |
38
|
|
|
array( |
39
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
40
|
|
|
'format' => 'xml', |
41
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
42
|
|
|
'method' => 'serializeDateTime' |
43
|
|
|
), |
44
|
|
|
array( |
45
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
46
|
|
|
'format' => 'xml', |
47
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
48
|
|
|
'method' => 'deserializeTime' |
49
|
|
|
), |
50
|
|
|
array( |
51
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
52
|
|
|
'format' => 'xml', |
53
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
54
|
|
|
'method' => 'serializeTime' |
55
|
|
|
), |
56
|
|
|
array( |
57
|
|
|
'type' => 'DateInterval', |
58
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
59
|
|
|
'format' => 'xml', |
60
|
|
|
'method' => 'deserializeDateIntervalXml', |
61
|
|
|
), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
17 |
|
public function __construct($defaultTimezone = 'UTC') |
66
|
|
|
{ |
67
|
17 |
|
$this->defaultTimezone = new \DateTimeZone($defaultTimezone); |
68
|
|
|
|
69
|
17 |
|
} |
70
|
|
|
|
71
|
|
|
public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$attributes = $data->attributes('xsi', true); |
74
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//Accept negative intervals like -PT1M23S. Safe to assume that "-" doesn't exist elsewhere in a valid interval spec. |
79
|
|
|
$interval = str_replace('-', '', (string)$data, $count); |
80
|
|
|
$dateInterval = new \DateInterval($interval); |
81
|
|
|
|
82
|
|
|
//Invert if a negative sign was found |
83
|
|
|
$dateInterval->invert = !!$count; |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $dateInterval; |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
public function serializeDate(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
89
|
|
|
{ |
90
|
|
|
|
91
|
5 |
|
$v = $date->format('Y-m-d'); |
92
|
|
|
|
93
|
5 |
|
return $visitor->visitSimpleString($v, $type, $context); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
public function deserializeDate(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
97
|
|
|
{ |
98
|
5 |
|
$attributes = $data->attributes('xsi', true); |
99
|
5 |
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
5 |
|
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})(Z|([+-]\d{2}:\d{2}))?$/', $data)) { |
103
|
1 |
|
throw new RuntimeException(sprintf('Invalid date "%s", expected valid XML Schema date string.', $data)); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
return $this->parseDateTime($data, $type); |
107
|
|
|
} |
108
|
|
|
|
109
|
7 |
|
public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
110
|
|
|
{ |
111
|
|
|
|
112
|
7 |
|
$v = $date->format(\DateTime::W3C); |
113
|
|
|
|
114
|
7 |
|
return $visitor->visitSimpleString($v, $type, $context); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$attributes = $data->attributes('xsi', true); |
120
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->parseDateTime($data, $type); |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
129
|
|
|
{ |
130
|
|
|
$v = $date->format('H:i:s'); |
131
|
|
|
if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) { |
132
|
|
|
$v .= $date->format('P'); |
133
|
|
|
} |
134
|
|
|
return $visitor->visitSimpleString($v, $type, $context); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function deserializeTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$attributes = $data->attributes('xsi', true); |
140
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$data = (string)$data; |
145
|
|
|
|
146
|
|
|
return new \DateTime($data, $this->defaultTimezone); |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
private function parseDateTime($data, array $type) |
150
|
|
|
{ |
151
|
4 |
|
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone; |
152
|
4 |
|
$datetime = new \DateTime((string)$data, $timezone); |
153
|
4 |
|
if (false === $datetime) { |
154
|
|
|
throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data)); |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
return $datetime; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.