|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\DateTime\Exception\InvalidDateException; |
|
6
|
|
|
use ValueObjects\Util\Util; |
|
7
|
|
|
use ValueObjects\ValueObjectInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Date implements ValueObjectInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Year */ |
|
12
|
|
|
protected $year; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Month */ |
|
15
|
|
|
protected $month; |
|
16
|
|
|
|
|
17
|
|
|
/** @var MonthDay */ |
|
18
|
|
|
protected $day; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns a new Date from native year, month and day values |
|
22
|
|
|
* |
|
23
|
|
|
* @param int $year |
|
|
|
|
|
|
24
|
|
|
* @param string $month |
|
|
|
|
|
|
25
|
|
|
* @param int $day |
|
|
|
|
|
|
26
|
|
|
* @return Date |
|
27
|
|
|
*/ |
|
28
|
3 |
|
public static function fromNative() |
|
29
|
|
|
{ |
|
30
|
3 |
|
$args = func_get_args(); |
|
31
|
|
|
|
|
32
|
3 |
|
$year = new Year($args[0]); |
|
33
|
3 |
|
$month = Month::fromNative($args[1]); |
|
34
|
3 |
|
$day = new MonthDay($args[2]); |
|
35
|
|
|
|
|
36
|
3 |
|
return new static($year, $month, $day); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns a new Date from a native PHP \DateTime |
|
41
|
|
|
* |
|
42
|
|
|
* @param \DateTime $date |
|
43
|
|
|
* @return Date |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public static function fromNativeDateTime(\DateTime $date) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$year = \intval($date->format('Y')); |
|
48
|
3 |
|
$month = Month::fromNativeDateTime($date); |
|
49
|
3 |
|
$day = \intval($date->format('d')); |
|
50
|
|
|
|
|
51
|
3 |
|
return new static(new Year($year), $month, new MonthDay($day)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns current Date |
|
56
|
|
|
* |
|
57
|
|
|
* @return Date |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public static function now() |
|
60
|
|
|
{ |
|
61
|
3 |
|
$date = new static(Year::now(), Month::now(), MonthDay::now()); |
|
62
|
|
|
|
|
63
|
3 |
|
return $date; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create a new Date |
|
68
|
|
|
* |
|
69
|
|
|
* @param Year $year |
|
70
|
|
|
* @param Month $month |
|
71
|
|
|
* @param MonthDay $day |
|
72
|
|
|
* @throws InvalidDateException |
|
73
|
|
|
*/ |
|
74
|
28 |
|
public function __construct(Year $year, Month $month, MonthDay $day) |
|
75
|
|
|
{ |
|
76
|
28 |
|
\DateTime::createFromFormat('Y-F-j', \sprintf('%d-%s-%d', $year->toNative(), $month, $day->toNative())); |
|
77
|
28 |
|
$nativeDateErrors = \DateTime::getLastErrors(); |
|
78
|
|
|
|
|
79
|
28 |
|
if ($nativeDateErrors['warning_count'] > 0 || $nativeDateErrors['error_count'] > 0) { |
|
80
|
1 |
|
throw new InvalidDateException($year->toNative(), $month->toNative(), $day->toNative()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
27 |
|
$this->year = $year; |
|
84
|
27 |
|
$this->month = $month; |
|
85
|
27 |
|
$this->day = $day; |
|
86
|
27 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Tells whether two Date are equal by comparing their values |
|
90
|
|
|
* |
|
91
|
|
|
* @param ValueObjectInterface $date |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
12 |
|
public function sameValueAs(ValueObjectInterface $date) |
|
95
|
|
|
{ |
|
96
|
12 |
|
if (false === Util::classEquals($this, $date)) { |
|
97
|
1 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
12 |
|
return $this->getYear()->sameValueAs($date->getYear()) && |
|
|
|
|
|
|
101
|
12 |
|
$this->getMonth()->sameValueAs($date->getMonth()) && |
|
|
|
|
|
|
102
|
12 |
|
$this->getDay()->sameValueAs($date->getDay()); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get year |
|
107
|
|
|
* |
|
108
|
|
|
* @return Year |
|
109
|
|
|
*/ |
|
110
|
22 |
|
public function getYear() |
|
111
|
|
|
{ |
|
112
|
22 |
|
return clone $this->year; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get month |
|
117
|
|
|
* |
|
118
|
|
|
* @return Month |
|
119
|
|
|
*/ |
|
120
|
22 |
|
public function getMonth() |
|
121
|
|
|
{ |
|
122
|
22 |
|
return $this->month; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get day |
|
127
|
|
|
* |
|
128
|
|
|
* @return MonthDay |
|
129
|
|
|
*/ |
|
130
|
22 |
|
public function getDay() |
|
131
|
|
|
{ |
|
132
|
22 |
|
return clone $this->day; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns a native PHP \DateTime version of the current Date |
|
137
|
|
|
* |
|
138
|
|
|
* @return \DateTime |
|
139
|
|
|
*/ |
|
140
|
7 |
|
public function toNativeDateTime() |
|
141
|
|
|
{ |
|
142
|
7 |
|
$year = $this->getYear()->toNative(); |
|
143
|
7 |
|
$month = $this->getMonth()->getNumericValue(); |
|
144
|
7 |
|
$day = $this->getDay()->toNative(); |
|
145
|
|
|
|
|
146
|
7 |
|
$date = new \DateTime(); |
|
147
|
7 |
|
$date->setDate($year, $month, $day); |
|
148
|
7 |
|
$date->setTime(0, 0, 0); |
|
149
|
|
|
|
|
150
|
7 |
|
return $date; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
return $this->toNativeDateTime(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns date as string in format Y-n-j |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
6 |
|
public function __toString() |
|
164
|
|
|
{ |
|
165
|
6 |
|
return $this->toNativeDateTime()->format('Y-n-j'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.