1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\BusinessHours; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Default implementation of BusinessInterface. |
7
|
|
|
*/ |
8
|
|
|
class Business implements BusinessInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The days. |
12
|
|
|
* |
13
|
|
|
* @var DayInterface[] |
14
|
|
|
*/ |
15
|
|
|
protected $days; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The time zone. |
19
|
|
|
* |
20
|
|
|
* @var \DateTimeZone |
21
|
|
|
*/ |
22
|
|
|
protected $timezone; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Creates a new business. |
26
|
|
|
* |
27
|
|
|
* @param DayInterface[] $days |
28
|
|
|
* @param \DateTimeZone|null $timezone |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $days, \DateTimeZone $timezone = null) |
31
|
|
|
{ |
32
|
|
|
$this->setDays($days); |
33
|
|
|
$this->timezone = $timezone ?: new \DateTimeZone(date_default_timezone_get()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the days. |
38
|
|
|
* |
39
|
|
|
* @return DayInterface[] |
40
|
|
|
*/ |
41
|
|
|
public function getDays() |
42
|
|
|
{ |
43
|
|
|
return array_values($this->days); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add a set of days. |
48
|
|
|
* |
49
|
|
|
* @param DayInterface[] $days The days. |
50
|
|
|
* @throws \InvalidArgumentException If no days are passed. |
51
|
|
|
*/ |
52
|
|
|
public function setDays(array $days) |
53
|
|
|
{ |
54
|
|
|
if (empty($days)) { |
55
|
|
|
throw new \InvalidArgumentException('At least one day must be added.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->days = []; |
59
|
|
|
|
60
|
|
|
foreach ($days as $day) { |
61
|
|
|
$this->addDay($day); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function within(\DateTime $date) |
69
|
|
|
{ |
70
|
|
|
$tmpDate = clone $date; |
71
|
|
|
$tmpDate->setTimezone($this->timezone); |
72
|
|
|
|
73
|
|
|
if (null !== $day = $this->getDay((int)$tmpDate->format('N'))) { |
74
|
|
|
return $day->isWithinOpeningHours(Time::fromDate($tmpDate)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getNextChangeDateTime(\DateTime $date = null) |
84
|
|
|
{ |
85
|
|
|
if ($date === null) { |
86
|
|
|
$date = new \DateTime('now', $this->timezone); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$dateInterval = $this->closestDateInterval($date); |
90
|
|
|
|
91
|
|
|
if ($this->within($date)) { |
92
|
|
|
return ($date == $dateInterval->getStart()) ? $dateInterval->getStart() : $dateInterval->getEnd(); |
93
|
|
|
} else { |
94
|
|
|
return $dateInterval->getStart(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function closestDateInterval(\DateTime $date) |
102
|
|
|
{ |
103
|
|
|
$tmpDate = clone $date; |
104
|
|
|
$tmpDate->setTimezone($this->timezone); |
105
|
|
|
|
106
|
|
|
return $this->getClosestInterval($tmpDate); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function jsonSerialize() |
113
|
|
|
{ |
114
|
|
|
return array( |
115
|
|
|
'days' => $this->days, |
116
|
|
|
'timezone' => $this->timezone->getName(), |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the closest business date interval after the given date. |
122
|
|
|
* |
123
|
|
|
* @param \DateTime $date |
124
|
|
|
* @return DateTimeInterval |
125
|
|
|
*/ |
126
|
|
|
private function getClosestDateIntervalAfter(\DateTime $date) |
127
|
|
|
{ |
128
|
|
|
$tmpDate = clone $date; |
129
|
|
|
$dayOfWeek = (int)$tmpDate->format('N'); |
130
|
|
|
$time = Time::fromDate($tmpDate); |
131
|
|
|
|
132
|
|
View Code Duplication |
if (null !== $day = $this->getDay($dayOfWeek)) { |
|
|
|
|
133
|
|
|
if (null !== $closestTime = $day->getClosestOpeningHoursInterval($time)) { |
134
|
|
|
$intervalStart = clone $tmpDate; |
135
|
|
|
$intervalEnd = clone $tmpDate; |
136
|
|
|
|
137
|
|
|
$intervalStart->setTime( |
138
|
|
|
$closestTime->getStart()->getHours(), |
139
|
|
|
$closestTime->getStart()->getMinutes(), |
140
|
|
|
$closestTime->getStart()->getSeconds() |
141
|
|
|
); |
142
|
|
|
$intervalEnd->setTime( |
143
|
|
|
$closestTime->getEnd()->getHours(), |
144
|
|
|
$closestTime->getEnd()->getMinutes(), |
145
|
|
|
$closestTime->getEnd()->getSeconds() |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
return new DateTimeInterval($intervalStart, $intervalEnd); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$tmpDate = $this->getDateAfter($tmpDate); |
153
|
|
|
|
154
|
|
|
$closestDay = $this->getClosestDayBefore((int)$tmpDate->format('N')); |
155
|
|
|
|
156
|
|
|
$openingTime = $closestDay->getOpeningTime(); |
157
|
|
|
$closestTime = $closestDay->getClosestOpeningHoursInterval($openingTime); |
158
|
|
|
|
159
|
|
|
$intervalStart = clone $tmpDate; |
160
|
|
|
$intervalEnd = clone $tmpDate; |
161
|
|
|
|
162
|
|
|
$intervalStart->setTime( |
163
|
|
|
$closestTime->getStart()->getHours(), |
164
|
|
|
$closestTime->getStart()->getMinutes(), |
165
|
|
|
$closestTime->getStart()->getSeconds() |
166
|
|
|
); |
167
|
|
|
$intervalEnd->setTime( |
168
|
|
|
$closestTime->getEnd()->getHours(), |
169
|
|
|
$closestTime->getEnd()->getMinutes(), |
170
|
|
|
$closestTime->getEnd()->getSeconds() |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
return new DateTimeInterval($intervalStart, $intervalEnd); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Gets the business date after the given date (excluding holidays). |
178
|
|
|
* |
179
|
|
|
* @param \DateTime $date |
180
|
|
|
* @return \DateTime |
181
|
|
|
*/ |
182
|
|
|
private function getDateAfter(\DateTime $date) |
183
|
|
|
{ |
184
|
|
|
$tmpDate = clone $date; |
185
|
|
|
$tmpDate->modify('+1 day'); |
186
|
|
|
|
187
|
|
|
$dayOfWeek = (int)$tmpDate->format('N'); |
188
|
|
|
$closestDay = $this->getClosestDayAfter($dayOfWeek); |
189
|
|
|
|
190
|
|
|
if ($closestDay->getDayOfWeek() !== $dayOfWeek) { |
191
|
|
|
$tmpDate->modify(sprintf('next %s', $closestDay->getDayOfWeekName())); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $tmpDate; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Gets the closest interval endpoint after the given date. |
199
|
|
|
* |
200
|
|
|
* @param \DateTime $date |
201
|
|
|
* @return DateTimeInterval |
202
|
|
|
*/ |
203
|
|
|
private function getClosestInterval(\DateTime $date) |
204
|
|
|
{ |
205
|
|
|
$tmpDate = clone $date; |
206
|
|
|
$dayOfWeek = (int)$tmpDate->format('N'); |
207
|
|
|
$time = Time::fromDate($tmpDate); |
208
|
|
|
|
209
|
|
View Code Duplication |
if (null !== $day = $this->getDay($dayOfWeek)) { |
|
|
|
|
210
|
|
|
if (null !== $closestTime = $day->getClosestOpeningHoursInterval($time)) { |
211
|
|
|
$intervalStart = clone $tmpDate; |
212
|
|
|
$intervalEnd = clone $tmpDate; |
213
|
|
|
|
214
|
|
|
$intervalStart->setTime( |
215
|
|
|
$closestTime->getStart()->getHours(), |
216
|
|
|
$closestTime->getStart()->getMinutes(), |
217
|
|
|
$closestTime->getStart()->getSeconds() |
218
|
|
|
); |
219
|
|
|
$intervalEnd->setTime( |
220
|
|
|
$closestTime->getEnd()->getHours(), |
221
|
|
|
$closestTime->getEnd()->getMinutes(), |
222
|
|
|
$closestTime->getEnd()->getSeconds() |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
return new DateTimeInterval($intervalStart, $intervalEnd); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->getClosestDateIntervalAfter($date); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Gets the closest business day before a given day number (including it). |
234
|
|
|
* |
235
|
|
|
* @param integer $dayNumber |
236
|
|
|
* @return DayInterface|null |
237
|
|
|
*/ |
238
|
|
|
private function getClosestDayBefore($dayNumber) |
239
|
|
|
{ |
240
|
|
|
if (null !== $day = $this->getDay($dayNumber)) { |
241
|
|
|
return $day; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $this->getDayBefore($dayNumber); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Gets the closest business day after a given day number (including it). |
249
|
|
|
* |
250
|
|
|
* @param integer $dayNumber |
251
|
|
|
* @return DayInterface|null |
252
|
|
|
*/ |
253
|
|
|
private function getClosestDayAfter($dayNumber) |
254
|
|
|
{ |
255
|
|
|
if (null !== $day = $this->getDay($dayNumber)) { |
256
|
|
|
return $day; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $this->getDayAfter($dayNumber); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Gets the business day before the day number. |
264
|
|
|
* |
265
|
|
|
* @param integer $dayNumber |
266
|
|
|
* @return DayInterface|null |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
private function getDayBefore($dayNumber) |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
$tmpDayNumber = $dayNumber; |
271
|
|
|
|
272
|
|
|
for ($i = 0; $i < 6; $i++) { |
273
|
|
|
$tmpDayNumber = (DayInterface::WEEK_DAY_MONDAY === $tmpDayNumber) ? DayInterface::WEEK_DAY_SUNDAY : --$tmpDayNumber; |
274
|
|
|
|
275
|
|
|
if (null !== $day = $this->getDay($tmpDayNumber)) { |
276
|
|
|
return $day; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this->getDay($dayNumber); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Gets the business day after the day number. |
285
|
|
|
* |
286
|
|
|
* @param integer $dayNumber |
287
|
|
|
* @return DayInterface|null |
288
|
|
|
*/ |
289
|
|
View Code Duplication |
private function getDayAfter($dayNumber) |
|
|
|
|
290
|
|
|
{ |
291
|
|
|
$tmpDayNumber = $dayNumber; |
292
|
|
|
|
293
|
|
|
for ($i = 0; $i < 6; $i++) { |
294
|
|
|
$tmpDayNumber = (DayInterface::WEEK_DAY_SUNDAY === $tmpDayNumber) ? DayInterface::WEEK_DAY_MONDAY : ++$tmpDayNumber; |
295
|
|
|
|
296
|
|
|
if (null !== $day = $this->getDay($tmpDayNumber)) { |
297
|
|
|
return $day; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this->getDay($dayNumber); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the day corresponding to the day number. |
306
|
|
|
* |
307
|
|
|
* @param integer $dayNumber |
308
|
|
|
* @return DayInterface|null |
309
|
|
|
*/ |
310
|
|
|
private function getDay($dayNumber) |
311
|
|
|
{ |
312
|
|
|
return isset($this->days[$dayNumber]) ? $this->days[$dayNumber] : null; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Add a day. |
317
|
|
|
* |
318
|
|
|
* @param DayInterface $day The day. |
319
|
|
|
*/ |
320
|
|
|
private function addDay(DayInterface $day) |
321
|
|
|
{ |
322
|
|
|
$this->days[$day->getDayOfWeek()] = $day; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
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.