|
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\Functions\Unary\Matcher\IsDateString; |
|
10
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate; |
|
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 LocalDateMapper extends AbstractPropertyMapper |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var AsLocalDate */ |
|
22
|
|
|
private $options; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* C'tor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param AsLocalDate $options |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(AsLocalDate $options) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->options = $options; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return AsLocalDate |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getOptions() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->options; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Slumberer $slumberer |
|
44
|
|
|
* @param mixed $value |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
21 |
View Code Duplication |
public function slumber(Slumberer $slumberer, $value) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
21 |
|
if (! $value instanceof LocalDate) { |
|
51
|
4 |
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return [ |
|
55
|
17 |
|
'date' => new UTCDateTime($value->getTimestamp() * 1000), |
|
56
|
17 |
|
'tz' => $value->getTimezone()->getName(), |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Awaker $awaker |
|
62
|
|
|
* @param mixed $value |
|
63
|
|
|
* |
|
64
|
|
|
* @return LocalDate |
|
65
|
|
|
*/ |
|
66
|
17 |
|
public function awake(Awaker $awaker, $value) |
|
67
|
|
|
{ |
|
68
|
|
|
// default cases first |
|
69
|
17 |
|
$canAccess = $value instanceof \ArrayAccess || is_array($value); |
|
70
|
17 |
|
$hasComponents = isset($value['date'], $value['tz']); |
|
71
|
|
|
|
|
72
|
17 |
View Code Duplication |
if ($canAccess && $hasComponents && $value['date'] instanceof \DateTime) { |
|
|
|
|
|
|
73
|
17 |
|
return new LocalDate($value['date'], $value['tz']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// compatibility in case a SimpleDate was changed to a LocalDate |
|
77
|
|
|
if ($value instanceof \DateTime) { |
|
78
|
|
|
return LocalDate::raw($value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// compatibility in case a string was change to a LocalDate |
|
82
|
|
|
if (IsDateString::isValidDateString($value)) { |
|
83
|
|
|
return LocalDate::raw(new \DateTime($value)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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.