|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\Util\Util; |
|
6
|
|
|
use ValueObjects\ValueObjectInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Time implements ValueObjectInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var Hour */ |
|
11
|
|
|
protected $hour; |
|
12
|
|
|
|
|
13
|
|
|
/** @var Minute */ |
|
14
|
|
|
protected $minute; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Second */ |
|
17
|
|
|
protected $second; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Returns a nee Time object from native int hour, minute and second |
|
21
|
|
|
* |
|
22
|
|
|
* @param int $hour |
|
|
|
|
|
|
23
|
|
|
* @param int $minute |
|
|
|
|
|
|
24
|
|
|
* @param int $second |
|
|
|
|
|
|
25
|
|
|
* @return self |
|
26
|
|
|
*/ |
|
27
|
6 |
|
public static function fromNative() |
|
28
|
|
|
{ |
|
29
|
6 |
|
$args = func_get_args(); |
|
30
|
|
|
|
|
31
|
6 |
|
$hour = new Hour($args[0]); |
|
32
|
6 |
|
$minute = new Minute($args[1]); |
|
33
|
6 |
|
$second = new Second($args[2]); |
|
34
|
|
|
|
|
35
|
6 |
|
return new static($hour, $minute, $second); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns a new Time from a native PHP \DateTime |
|
40
|
|
|
* |
|
41
|
|
|
* @param \DateTime $time |
|
42
|
|
|
* @return self |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public static function fromNativeDateTime(\DateTime $time) |
|
45
|
|
|
{ |
|
46
|
3 |
|
$hour = \intval($time->format('G')); |
|
47
|
3 |
|
$minute = \intval($time->format('i')); |
|
48
|
3 |
|
$second = \intval($time->format('s')); |
|
49
|
|
|
|
|
50
|
3 |
|
return static::fromNative($hour, $minute, $second); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns current Time |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public static function now() |
|
59
|
|
|
{ |
|
60
|
3 |
|
$time = new static(Hour::now(), Minute::now(), Second::now()); |
|
61
|
|
|
|
|
62
|
3 |
|
return $time; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return zero time |
|
67
|
|
|
* |
|
68
|
|
|
* @return static |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public static function zero() |
|
71
|
|
|
{ |
|
72
|
2 |
|
$time = new static(new Hour(0), new Minute(0), new Second(0)); |
|
73
|
|
|
|
|
74
|
2 |
|
return $time; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns a new Time objects |
|
79
|
|
|
* |
|
80
|
|
|
* @param Hour $hour |
|
81
|
|
|
* @param Minute $minute |
|
82
|
|
|
* @param Second $second |
|
83
|
|
|
*/ |
|
84
|
28 |
|
public function __construct(Hour $hour, Minute $minute, Second $second) |
|
85
|
|
|
{ |
|
86
|
28 |
|
$this->hour = $hour; |
|
87
|
28 |
|
$this->minute = $minute; |
|
88
|
28 |
|
$this->second = $second; |
|
89
|
28 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Tells whether two Time are equal by comparing their values |
|
93
|
|
|
* |
|
94
|
|
|
* @param ValueObjectInterface $time |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
13 |
|
public function sameValueAs(ValueObjectInterface $time) |
|
98
|
|
|
{ |
|
99
|
13 |
|
if (false === Util::classEquals($this, $time)) { |
|
100
|
1 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
13 |
|
return $this->getHour()->sameValueAs($time->getHour()) && $this->getMinute()->sameValueAs($time->getMinute()) && $this->getSecond()->sameValueAs($time->getSecond()); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get hour |
|
108
|
|
|
* |
|
109
|
|
|
* @return Hour |
|
110
|
|
|
*/ |
|
111
|
24 |
|
public function getHour() |
|
112
|
|
|
{ |
|
113
|
24 |
|
return $this->hour; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get minute |
|
118
|
|
|
* |
|
119
|
|
|
* @return Minute |
|
120
|
|
|
*/ |
|
121
|
24 |
|
public function getMinute() |
|
122
|
|
|
{ |
|
123
|
24 |
|
return $this->minute; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get second |
|
128
|
|
|
* |
|
129
|
|
|
* @return Second |
|
130
|
|
|
*/ |
|
131
|
24 |
|
public function getSecond() |
|
132
|
|
|
{ |
|
133
|
24 |
|
return $this->second; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns a native PHP \DateTime version of the current Time. |
|
138
|
|
|
* Date is set to current. |
|
139
|
|
|
* |
|
140
|
|
|
* @return \DateTime |
|
141
|
|
|
*/ |
|
142
|
8 |
|
public function toNativeDateTime() |
|
143
|
|
|
{ |
|
144
|
8 |
|
$hour = $this->getHour()->toNative(); |
|
145
|
8 |
|
$minute = $this->getMinute()->toNative(); |
|
146
|
8 |
|
$second = $this->getSecond()->toNative(); |
|
147
|
|
|
|
|
148
|
8 |
|
$time = new \DateTime('now'); |
|
149
|
8 |
|
$time->setTime($hour, $minute, $second); |
|
150
|
|
|
|
|
151
|
8 |
|
return $time; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns time as string in format G:i:s |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
7 |
|
public function __toString() |
|
160
|
|
|
{ |
|
161
|
7 |
|
return $this->toNativeDateTime()->format('G:i:s'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
return [ |
|
168
|
|
|
'hour' => $this->hour, |
|
169
|
|
|
'minute' => $this->minute, |
|
170
|
|
|
'second' => $this->second |
|
171
|
|
|
]; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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.