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