1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\BusinessHours\Day\Time; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents a time. |
7
|
|
|
*/ |
8
|
|
|
class Time implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The hours part of the time. |
12
|
|
|
* |
13
|
|
|
* @var integer |
14
|
|
|
*/ |
15
|
|
|
protected $hours; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The minutes part of the time. |
19
|
|
|
* |
20
|
|
|
* @var integer |
21
|
|
|
*/ |
22
|
|
|
protected $minutes = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The seconds part of the time. |
26
|
|
|
* |
27
|
|
|
* @var integer |
28
|
|
|
*/ |
29
|
|
|
protected $seconds = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param integer $hours The hours. |
35
|
|
|
* @param integer $minutes The minutes. |
36
|
|
|
* @param integer $seconds The seconds. |
37
|
|
|
*/ |
38
|
|
|
public function __construct($hours, $minutes = 0, $seconds = 0) |
39
|
|
|
{ |
40
|
|
|
$this->setHours($hours); |
41
|
|
|
$this->setMinutes($minutes); |
42
|
|
|
$this->setSeconds($seconds); |
43
|
|
|
} |
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
|
|
|
public function isBeforeOrEqual(Time $other) |
52
|
|
|
{ |
53
|
|
|
return $this->toInteger() <= $other->toInteger(); |
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
|
|
|
public function isAfterOrEqual(Time $other) |
63
|
|
|
{ |
64
|
|
|
return $this->toInteger() >= $other->toInteger(); |
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
|
|
|
public function isEqual(Time $other) |
74
|
|
|
{ |
75
|
|
|
return $this->toInteger() === $other->toInteger(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the integer representation of the time. |
80
|
|
|
* |
81
|
|
|
* @return integer |
82
|
|
|
*/ |
83
|
|
|
public function toInteger() |
84
|
|
|
{ |
85
|
|
|
return (int)sprintf('%d%02d%02d', $this->hours, $this->minutes, $this->seconds); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the hours. |
90
|
|
|
* |
91
|
|
|
* @param integer $hours The hours. |
92
|
|
|
* @return Time |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function setHours($hours) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$hours = (int)$hours; |
97
|
|
|
|
98
|
|
|
$this->timeElementsAreValid($hours, $this->minutes, $this->seconds); |
99
|
|
|
|
100
|
|
|
$this->hours = (int)$hours; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the hours. |
107
|
|
|
* |
108
|
|
|
* @return integer |
109
|
|
|
*/ |
110
|
|
|
public function getHours() |
111
|
|
|
{ |
112
|
|
|
return $this->hours; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the minutes. |
117
|
|
|
* |
118
|
|
|
* @param integer $minutes The minutes |
119
|
|
|
* @return Time |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function setMinutes($minutes) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$minutes = (int)$minutes; |
124
|
|
|
|
125
|
|
|
$this->timeElementsAreValid($this->hours, $minutes, $this->seconds); |
126
|
|
|
|
127
|
|
|
$this->minutes = (int)$minutes; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the minutes. |
134
|
|
|
* |
135
|
|
|
* @return integer |
136
|
|
|
*/ |
137
|
|
|
public function getMinutes() |
138
|
|
|
{ |
139
|
|
|
return $this->minutes; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the seconds. |
144
|
|
|
* |
145
|
|
|
* @param integer $seconds The seconds. |
146
|
|
|
* @return Time |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function setSeconds($seconds) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$seconds = (int)$seconds; |
151
|
|
|
|
152
|
|
|
$this->timeElementsAreValid($this->hours, $this->minutes, $seconds); |
153
|
|
|
|
154
|
|
|
$this->seconds = (int)$seconds; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the seconds. |
161
|
|
|
* |
162
|
|
|
* @return integer |
163
|
|
|
*/ |
164
|
|
|
public function getSeconds() |
165
|
|
|
{ |
166
|
|
|
return $this->seconds; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check if the time elements are valid. |
171
|
|
|
* |
172
|
|
|
* @param integer $hours The hours. |
173
|
|
|
* @param integer $minutes The minutes. |
174
|
|
|
* @param integer $seconds The seconds. |
175
|
|
|
* @return boolean |
176
|
|
|
* @throws \InvalidArgumentException If the elements are not valid. |
177
|
|
|
*/ |
178
|
|
|
private function timeElementsAreValid($hours, $minutes, $seconds) |
179
|
|
|
{ |
180
|
|
|
$exception = new \InvalidArgumentException( |
181
|
|
|
sprintf('Invalid time "%02d:%02d:%02d".', $hours, $minutes, $seconds) |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
if ((int)sprintf('%d%02d%02d', $hours, $minutes, $seconds) > 240000) { |
185
|
|
|
throw $exception; |
186
|
|
|
} elseif ($hours < 0 || $minutes < 0 || $seconds < 0) { |
187
|
|
|
throw $exception; |
188
|
|
|
} elseif ($hours <= 24 && $minutes <= 59 && $seconds <= 59) { |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
throw $exception; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
public function jsonSerialize() |
199
|
|
|
{ |
200
|
|
|
return array( |
201
|
|
|
'hours' => $this->hours, |
202
|
|
|
'minutes' => $this->minutes, |
203
|
|
|
'seconds' => $this->seconds, |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns a string representation of the time. |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function __toString() |
213
|
|
|
{ |
214
|
|
|
return sprintf('%02d:%02d:%02d', $this->hours, $this->minutes, $this->seconds); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.