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\AsLocalDate; |
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 LocalDateMapper extends AbstractPropertyMapper |
18
|
|
|
{ |
19
|
|
|
/** @var AsLocalDate */ |
20
|
|
|
private $options; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* C'tor. |
24
|
|
|
* |
25
|
|
|
* @param AsLocalDate $options |
26
|
|
|
*/ |
27
|
32 |
|
public function __construct(AsLocalDate $options) |
28
|
|
|
{ |
29
|
32 |
|
$this->options = $options; |
30
|
32 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return AsLocalDate |
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 array |
45
|
|
|
*/ |
46
|
17 |
|
public function slumber(Slumberer $slumberer, $value) |
47
|
|
|
{ |
48
|
17 |
|
if (!$value instanceof LocalDate) { |
|
|
|
|
49
|
8 |
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return [ |
53
|
9 |
|
'date' => $value->format('Y-m-d\TH:i:s.uP'), |
54
|
9 |
|
'tz' => $value->getTimezone()->getName(), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Awaker $awaker |
60
|
|
|
* @param mixed $value |
61
|
|
|
* |
62
|
|
|
* @return LocalDate |
63
|
|
|
*/ |
64
|
11 |
|
public function awake(Awaker $awaker, $value) |
65
|
|
|
{ |
66
|
11 |
|
if ($value === null) { |
67
|
1 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// check for a complex input with date and timezone |
71
|
10 |
|
if (isset($value['date'], $value['tz'])) { |
72
|
|
|
|
73
|
5 |
|
if (false === ($tz = @timezone_open($value['tz']))) { |
74
|
1 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
if (IsDateString::isValidDateString($value['date'])) { |
78
|
3 |
|
return new LocalDate($value['date'], $tz); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// check for a simple input |
85
|
5 |
|
if (IsDateString::isValidDateString($value)) { |
86
|
2 |
|
return LocalDate::raw(new \DateTime($value)); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return null; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|