1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler; |
3
|
|
|
|
4
|
|
|
use JMS\Serializer\Context; |
5
|
|
|
use JMS\Serializer\GraphNavigator; |
6
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
7
|
|
|
use JMS\Serializer\XmlDeserializationVisitor; |
8
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class XmlSchemaDateHandler implements SubscribingHandlerInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $defaultTimezone; |
15
|
|
|
|
16
|
|
|
public static function getSubscribingMethods() |
17
|
|
|
{ |
18
|
|
|
return array( |
19
|
|
|
array( |
20
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
21
|
|
|
'format' => 'xml', |
22
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
23
|
|
|
'method' => 'deserializeDateTime' |
24
|
|
|
), |
25
|
|
|
array( |
26
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
27
|
|
|
'format' => 'xml', |
28
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
29
|
|
|
'method' => 'serializeDateTime' |
30
|
|
|
), |
31
|
|
|
array( |
32
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
33
|
|
|
'format' => 'xml', |
34
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
35
|
|
|
'method' => 'deserializeTime' |
36
|
|
|
), |
37
|
|
|
array( |
38
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
39
|
|
|
'format' => 'xml', |
40
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
41
|
|
|
'method' => 'serializeTime' |
42
|
|
|
), |
43
|
|
|
array( |
44
|
|
|
'type' => 'DateInterval', |
45
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
46
|
|
|
'format' => 'xml', |
47
|
|
|
'method' => 'deserializeDateIntervalXml', |
48
|
|
|
), |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
public function __construct($defaultTimezone = 'UTC') |
53
|
|
|
{ |
54
|
5 |
|
$this->defaultTimezone = new \DateTimeZone($defaultTimezone); |
55
|
|
|
|
56
|
5 |
|
} |
57
|
|
|
|
58
|
|
|
public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $data, array $type){ |
|
|
|
|
59
|
|
|
$attributes = $data->attributes('xsi', true); |
60
|
|
|
if (isset($attributes['nil'][0]) && (string) $attributes['nil'][0] === 'true') { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
return new \DateInterval((string)$data); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
67
|
|
|
{ |
68
|
|
|
|
69
|
5 |
|
$v = $date->format(\DateTime::W3C); |
70
|
5 |
|
if (substr($v, -5) == "00:00") { |
71
|
4 |
|
$v = substr($v, 0, -6); |
72
|
4 |
|
} |
73
|
5 |
|
return $visitor->visitSimpleString($v, $type, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$attributes = $data->attributes('xsi', true); |
79
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->parseDateTime($data, $type); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
88
|
|
|
{ |
89
|
|
|
$v = $date->format('H:i:s'); |
90
|
|
|
if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) { |
91
|
|
|
$v .= $date->format('P'); |
92
|
|
|
} |
93
|
|
|
return $visitor->visitSimpleString($v, $type, $context); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function deserializeTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$attributes = $data->attributes('xsi', true); |
99
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$data = (string)$data; |
104
|
|
|
|
105
|
|
|
return new \DateTime($data, $this->defaultTimezone); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function parseDateTime($data, array $type) |
109
|
|
|
{ |
110
|
|
|
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone; |
111
|
|
|
$datetime = new \DateTime((string)$data, $timezone); |
112
|
|
|
if (false === $datetime) { |
113
|
|
|
throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $datetime; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.