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\JsonDeserializationVisitor; |
8
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class JsonSchemaDateHandler implements SubscribingHandlerInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $defaultTimezone; |
15
|
|
|
|
16
|
|
View Code Duplication |
public static function getSubscribingMethods() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
return array( |
19
|
|
|
array( |
20
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
21
|
|
|
'format' => 'json', |
22
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
23
|
|
|
'method' => 'deserializeDate' |
24
|
|
|
), |
25
|
|
|
array( |
26
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
27
|
|
|
'format' => 'json', |
28
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
29
|
|
|
'method' => 'serializeDate' |
30
|
|
|
), |
31
|
|
|
array( |
32
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
33
|
|
|
'format' => 'json', |
34
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
35
|
|
|
'method' => 'deserializeDateTime' |
36
|
|
|
), |
37
|
|
|
array( |
38
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
39
|
|
|
'format' => 'json', |
40
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
41
|
|
|
'method' => 'serializeDateTime' |
42
|
|
|
), |
43
|
|
|
array( |
44
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
45
|
|
|
'format' => 'json', |
46
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
47
|
|
|
'method' => 'deserializeTime' |
48
|
|
|
), |
49
|
|
|
array( |
50
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
51
|
|
|
'format' => 'json', |
52
|
|
|
'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
53
|
|
|
'method' => 'serializeTime' |
54
|
|
|
), |
55
|
|
|
array( |
56
|
|
|
'type' => 'DateInterval', |
57
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
58
|
|
|
'format' => 'json', |
59
|
|
|
'method' => 'deserializeDateIntervalXml', |
60
|
|
|
), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __construct($defaultTimezone = 'UTC') |
65
|
|
|
{ |
66
|
|
|
$this->defaultTimezone = new \DateTimeZone($defaultTimezone); |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function deserializeDateIntervalXml(JsonDeserializationVisitor $visitor, $data, array $type){ |
|
|
|
|
71
|
|
|
return $this->createDateInterval((string)$data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function serializeDate(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return $date->format('Y-m-d'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function deserializeDate(JsonDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})(Z|([+-]\d{2}:\d{2}))?$/', $data)) { |
82
|
|
|
throw new RuntimeException(sprintf('Invalid date "%s", expected valid XML Schema date string.', $data)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->parseDateTime($data, $type); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function serializeDateTime(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
return $date->format(\DateTime::W3C); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function deserializeDateTime(JsonDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return $this->parseDateTime($data, $type); |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function serializeTime(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$v = $date->format('H:i:s'); |
103
|
|
|
if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) { |
104
|
|
|
$v .= $date->format('P'); |
105
|
|
|
} |
106
|
|
|
return $v; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function deserializeTime(JsonDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$data = (string)$data; |
112
|
|
|
|
113
|
|
|
return new \DateTime($data, $this->defaultTimezone); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
private function parseDateTime($data, array $type) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone; |
119
|
|
|
$datetime = new \DateTime((string)$data, $timezone); |
120
|
|
|
if (false === $datetime) { |
121
|
|
|
throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $datetime; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
private function createDateInterval($interval){ |
|
|
|
|
128
|
|
|
$f = 0.0; |
129
|
|
|
if (preg_match('~\.\d+~',$interval,$match)) { |
130
|
|
|
$interval = str_replace($match[0], "", $interval); |
131
|
|
|
$f = (float)$match[0]; |
132
|
|
|
} |
133
|
|
|
$di = new \DateInterval($interval); |
134
|
|
|
// milliseconds are only available from >=7.1 |
135
|
|
|
if(isset($di->f)){ |
136
|
|
|
$di->f= $f; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $di; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.