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
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
public function __construct($defaultTimezone = 'UTC') |
47
|
|
|
{ |
48
|
5 |
|
$this->defaultTimezone = new \DateTimeZone($defaultTimezone); |
49
|
|
|
|
50
|
5 |
|
} |
51
|
|
|
|
52
|
5 |
|
public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
53
|
|
|
{ |
54
|
|
|
|
55
|
5 |
|
$v = $date->format(\DateTime::W3C); |
56
|
5 |
|
if (substr($v, -5) == "00:00") { |
57
|
4 |
|
$v = substr($v, 0, -6); |
58
|
4 |
|
} |
59
|
5 |
|
return $visitor->visitSimpleString($v, $type, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$attributes = $data->attributes('xsi', true); |
65
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->parseDateTime($data, $type); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
74
|
|
|
{ |
75
|
|
|
$v = $date->format('H:i:s'); |
76
|
|
|
if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) { |
77
|
|
|
$v .= $date->format('P'); |
78
|
|
|
} |
79
|
|
|
return $visitor->visitSimpleString($v, $type, $context); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function deserializeTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$attributes = $data->attributes('xsi', true); |
85
|
|
|
if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$data = (string)$data; |
90
|
|
|
|
91
|
|
|
return new \DateTime($data, $this->defaultTimezone); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function parseDateTime($data, array $type) |
95
|
|
|
{ |
96
|
|
|
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone; |
97
|
|
|
$datetime = new \DateTime((string)$data, $timezone); |
98
|
|
|
if (false === $datetime) { |
99
|
|
|
throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $datetime; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.