1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vlaswinkel\Lua\JMS; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
6
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
7
|
|
|
use JMS\Serializer\GraphNavigator; |
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
9
|
|
|
use JMS\Serializer\VisitorInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class LuaDateHandler |
13
|
|
|
* |
14
|
|
|
* @see https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/DateHandler.php |
15
|
|
|
* |
16
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
17
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
18
|
|
|
* @package Vlaswinkel\Lua\JMS |
19
|
|
|
*/ |
20
|
|
|
class LuaDateHandler implements SubscribingHandlerInterface { |
21
|
|
|
private $defaultFormat; |
22
|
|
|
private $defaultTimezone; |
23
|
|
|
|
24
|
|
|
public static function getSubscribingMethods() { |
25
|
|
|
$methods = []; |
26
|
|
|
$types = ['DateTime', 'DateInterval']; |
27
|
|
|
$methods[] = [ |
28
|
|
|
'type' => 'DateTime', |
29
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
30
|
|
|
'format' => 'lua', |
31
|
|
|
]; |
32
|
|
View Code Duplication |
foreach ($types as $type) { |
33
|
|
|
$methods[] = [ |
34
|
|
|
'type' => $type, |
35
|
|
|
'format' => 'lua', |
36
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
37
|
|
|
'method' => 'serialize' . $type, |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
return $methods; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __construct($defaultFormat = \DateTime::ISO8601, $defaultTimezone = 'UTC') { |
44
|
|
|
$this->defaultFormat = $defaultFormat; |
45
|
|
|
$this->defaultTimezone = new \DateTimeZone($defaultTimezone); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type, Context $context) { |
49
|
|
|
return $visitor->visitString($date->format($this->getFormat($type)), $type, $context); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function serializeDateInterval( |
53
|
|
|
VisitorInterface $visitor, |
54
|
|
|
\DateInterval $date, |
55
|
|
|
array $type, |
56
|
|
|
Context $context |
57
|
|
|
) { |
58
|
|
|
$iso8601DateIntervalString = $this->format($date); |
59
|
|
|
return $visitor->visitString($iso8601DateIntervalString, $type, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function deserializeDateTimeFromLua(LuaDeserializationVisitor $visitor, $data, array $type) { |
63
|
|
|
if (null === $data) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
return $this->parseDateTime($data, $type); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function parseDateTime($data, array $type) { |
70
|
|
|
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone; |
71
|
|
|
$format = $this->getFormat($type); |
72
|
|
|
$datetime = \DateTime::createFromFormat($format, (string)$data, $timezone); |
73
|
|
|
if (false === $datetime) { |
74
|
|
|
throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $format)); |
75
|
|
|
} |
76
|
|
|
return $datetime; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
* |
82
|
|
|
* @param array $type |
83
|
|
|
*/ |
84
|
|
|
private function getFormat(array $type) { |
85
|
|
|
return isset($type['params'][0]) ? $type['params'][0] : $this->defaultFormat; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \DateInterval $dateInterval |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function format(\DateInterval $dateInterval) { |
94
|
|
|
$format = 'P'; |
95
|
|
|
if (0 < $dateInterval->y) { |
96
|
|
|
$format .= $dateInterval->y . 'Y'; |
97
|
|
|
} |
98
|
|
|
if (0 < $dateInterval->m) { |
99
|
|
|
$format .= $dateInterval->m . 'M'; |
100
|
|
|
} |
101
|
|
|
if (0 < $dateInterval->d) { |
102
|
|
|
$format .= $dateInterval->d . 'D'; |
103
|
|
|
} |
104
|
|
|
if (0 < $dateInterval->h || 0 < $dateInterval->i || 0 < $dateInterval->s) { |
105
|
|
|
$format .= 'T'; |
106
|
|
|
} |
107
|
|
|
if (0 < $dateInterval->h) { |
108
|
|
|
$format .= $dateInterval->h . 'H'; |
109
|
|
|
} |
110
|
|
|
if (0 < $dateInterval->i) { |
111
|
|
|
$format .= $dateInterval->i . 'M'; |
112
|
|
|
} |
113
|
|
|
if (0 < $dateInterval->s) { |
114
|
|
|
$format .= $dateInterval->s . 'S'; |
115
|
|
|
} |
116
|
|
|
return $format; |
117
|
|
|
} |
118
|
|
|
} |