1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 30.09.2015 07:58 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property; |
7
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\Psi\Psi\IsDateString; |
9
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsSimpleDate; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer; |
12
|
|
|
use PeekAndPoke\Types\LocalDate; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Karsten J. Gerber <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class SimpleDateMapper extends AbstractPropertyMapper |
18
|
|
|
{ |
19
|
|
|
/** @var AsSimpleDate */ |
20
|
|
|
private $options; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* C'tor. |
24
|
|
|
* |
25
|
|
|
* @param AsSimpleDate $options |
26
|
|
|
*/ |
27
|
25 |
|
public function __construct(AsSimpleDate $options) |
28
|
|
|
{ |
29
|
25 |
|
$this->options = $options; |
30
|
25 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return AsSimpleDate |
34
|
|
|
*/ |
35
|
1 |
|
public function getOptions() |
36
|
|
|
{ |
37
|
1 |
|
return $this->options; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Slumberer $slumberer |
42
|
|
|
* @param LocalDate $value |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
12 |
|
public function slumber(Slumberer $slumberer, $value) |
47
|
|
|
{ |
48
|
12 |
|
if ($value instanceof \DateTimeInterface) { |
49
|
2 |
|
return $value->format('Y-m-d\TH:i:s.uP'); |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
if ($value instanceof LocalDate) { |
|
|
|
|
53
|
2 |
|
return $value->format('Y-m-d\TH:i:s.uP'); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Awaker $awaker |
61
|
|
|
* @param mixed $value |
62
|
|
|
* |
63
|
|
|
* @return \DateTime|null |
64
|
|
|
*/ |
65
|
9 |
|
public function awake(Awaker $awaker, $value) |
66
|
|
|
{ |
67
|
9 |
|
if ($value === null) { |
68
|
1 |
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
if (IsDateString::isValidDateString($value)) { |
72
|
1 |
|
return new \DateTime($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
if ($this->isAwakeLocalDateCompatible($value)) { |
76
|
2 |
|
return new \DateTime($value['date'], new \DateTimeZone($value['tz'])); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $value |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
7 |
|
private function isAwakeLocalDateCompatible($value) |
88
|
|
|
{ |
89
|
7 |
|
return isset($value['date'], $value['tz']) |
90
|
7 |
|
&& IsDateString::isValidDateString($value['date']) |
91
|
7 |
|
&& $this->isTimezone($value['tz']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* TODO: move this to Psi and make a IsTimezoneString |
96
|
|
|
* TODO: add more that are not in timezone_identifiers_list(): https://en.wikipedia.org/wiki/List_of_tz_database_time_zones |
97
|
|
|
* |
98
|
|
|
* @param $str |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
3 |
|
private function isTimezone($str) |
103
|
|
|
{ |
104
|
3 |
|
static $identifiers; |
105
|
|
|
|
106
|
3 |
|
if ($identifiers === null) { |
107
|
1 |
|
$ids = timezone_identifiers_list(); |
108
|
|
|
array_walk($ids, function (&$id) { $id = strtolower($id); }); |
|
|
|
|
109
|
|
|
|
110
|
1 |
|
$identifiers = array_flip($ids); |
111
|
|
|
|
112
|
|
|
// add some more |
113
|
1 |
|
$identifiers['utc'] = true; |
114
|
1 |
|
$identifiers['etc/utc'] = true; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return isset($identifiers[strtolower($str)]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|