1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Speicher210\BusinessHours\Day\Time; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents a time. |
9
|
|
|
*/ |
10
|
|
|
class Time implements \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The hours part of the time. |
14
|
|
|
* |
15
|
|
|
* @var integer |
16
|
|
|
*/ |
17
|
|
|
protected $hours; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The minutes part of the time. |
21
|
|
|
* |
22
|
|
|
* @var integer |
23
|
|
|
*/ |
24
|
|
|
protected $minutes = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The seconds part of the time. |
28
|
|
|
* |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
protected $seconds = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param integer $hours The hours. |
35
|
|
|
* @param integer $minutes The minutes. |
36
|
|
|
* @param integer $seconds The seconds. |
37
|
|
|
*/ |
38
|
282 |
|
public function __construct(int $hours, int $minutes = 0, int $seconds = 0) |
39
|
|
|
{ |
40
|
282 |
|
$this->setHours($hours); |
41
|
279 |
|
$this->setMinutes($minutes); |
42
|
270 |
|
$this->setSeconds($seconds); |
43
|
261 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Checks if this time is before or equal to an other time. |
47
|
|
|
* |
48
|
|
|
* @param Time $other The time to compare it against. |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
108 |
|
public function isBeforeOrEqual(Time $other): bool |
52
|
|
|
{ |
53
|
108 |
|
return $this->toSeconds() <= $other->toSeconds(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Checks if this time is after or equal to an other time. |
58
|
|
|
* |
59
|
|
|
* @param Time $other The time to compare it against. |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
180 |
|
public function isAfterOrEqual(Time $other): bool |
63
|
|
|
{ |
64
|
180 |
|
return $this->toSeconds() >= $other->toSeconds(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if this time is equal to another time. |
69
|
|
|
* |
70
|
|
|
* @param Time $other The time to compare it against. |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
9 |
|
public function isEqual(Time $other): bool |
74
|
|
|
{ |
75
|
9 |
|
return $this->toSeconds() === $other->toSeconds(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the time representation in seconds. |
80
|
|
|
* |
81
|
|
|
* @return integer |
82
|
|
|
*/ |
83
|
216 |
|
public function toSeconds(): int |
84
|
|
|
{ |
85
|
216 |
|
return 3600 * $this->hours + 60 * $this->minutes + $this->seconds; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the hours. |
90
|
|
|
* |
91
|
|
|
* @param integer $hours The hours. |
92
|
|
|
*/ |
93
|
|
|
public function setHours(int $hours): void |
94
|
282 |
|
{ |
95
|
|
|
$this->timeElementsAreValid($hours, $this->minutes, $this->seconds); |
96
|
282 |
|
|
97
|
|
|
$this->hours = $hours; |
98
|
282 |
|
} |
99
|
|
|
|
100
|
279 |
|
/** |
101
|
|
|
* Get the hours. |
102
|
279 |
|
* |
103
|
|
|
* @return integer |
104
|
|
|
*/ |
105
|
|
|
public function getHours(): int |
106
|
|
|
{ |
107
|
|
|
return $this->hours; |
108
|
|
|
} |
109
|
|
|
|
110
|
165 |
|
/** |
111
|
|
|
* Set the minutes. |
112
|
165 |
|
* |
113
|
|
|
* @param integer $minutes The minutes |
114
|
|
|
*/ |
115
|
|
|
public function setMinutes(int $minutes): void |
116
|
|
|
{ |
117
|
|
|
$this->timeElementsAreValid($this->hours, $minutes, $this->seconds); |
118
|
|
|
|
119
|
|
|
$this->minutes = $minutes; |
120
|
|
|
} |
121
|
279 |
|
|
122
|
|
|
/** |
123
|
279 |
|
* Get the minutes. |
124
|
|
|
* |
125
|
279 |
|
* @return integer |
126
|
|
|
*/ |
127
|
270 |
|
public function getMinutes(): int |
128
|
|
|
{ |
129
|
270 |
|
return $this->minutes; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the seconds. |
134
|
|
|
* |
135
|
|
|
* @param integer $seconds The seconds. |
136
|
|
|
*/ |
137
|
126 |
|
public function setSeconds(int $seconds): void |
138
|
|
|
{ |
139
|
126 |
|
$this->timeElementsAreValid($this->hours, $this->minutes, $seconds); |
140
|
|
|
|
141
|
|
|
$this->seconds = $seconds; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the seconds. |
146
|
|
|
* |
147
|
|
|
* @return integer |
148
|
270 |
|
*/ |
149
|
|
|
public function getSeconds(): int |
150
|
270 |
|
{ |
151
|
|
|
return $this->seconds; |
152
|
270 |
|
} |
153
|
|
|
|
154
|
261 |
|
/** |
155
|
|
|
* Check if the time elements are valid. |
156
|
261 |
|
* |
157
|
|
|
* @param integer $hours The hours. |
158
|
|
|
* @param integer $minutes The minutes. |
159
|
|
|
* @param integer $seconds The seconds. |
160
|
|
|
* @return boolean |
161
|
|
|
* @throws \InvalidArgumentException If the elements are not valid. |
162
|
|
|
*/ |
163
|
|
|
private function timeElementsAreValid($hours, $minutes, $seconds): bool |
164
|
93 |
|
{ |
165
|
|
|
$exception = new \InvalidArgumentException( |
166
|
93 |
|
\sprintf('Invalid time "%02d:%02d:%02d".', $hours, $minutes, $seconds) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
if ((int)\sprintf('%d%02d%02d', $hours, $minutes, $seconds) > 240000) { |
170
|
|
|
throw $exception; |
171
|
|
|
} |
172
|
|
|
if ($hours < 0 || $minutes < 0 || $seconds < 0) { |
173
|
|
|
throw $exception; |
174
|
|
|
} |
175
|
|
|
if ($hours <= 24 && $minutes <= 59 && $seconds <= 59) { |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
282 |
|
|
179
|
|
|
throw $exception; |
180
|
282 |
|
} |
181
|
282 |
|
|
182
|
282 |
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
282 |
|
*/ |
185
|
9 |
|
public function jsonSerialize() |
186
|
282 |
|
{ |
187
|
9 |
|
return [ |
188
|
279 |
|
'hours' => $this->hours, |
189
|
279 |
|
'minutes' => $this->minutes, |
190
|
|
|
'seconds' => $this->seconds, |
191
|
|
|
]; |
192
|
6 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns a string representation of the time. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
12 |
|
*/ |
199
|
|
|
public function __toString() |
200
|
|
|
{ |
201
|
12 |
|
return \sprintf('%02d:%02d:%02d', $this->hours, $this->minutes, $this->seconds); |
202
|
12 |
|
} |
203
|
|
|
} |
204
|
|
|
|