1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Business-hours. |
5
|
|
|
* Copyright (c) 2015 - 2016 original code: Florian Voutzinos <[email protected] |
6
|
|
|
* Copyright (c) 2015 - 2017 additions and changes: Speicher 210 GmbH |
7
|
|
|
* For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
11
|
|
|
|
12
|
|
|
namespace Speicher210\BusinessHours\Day; |
13
|
|
|
|
14
|
|
|
use Speicher210\BusinessHours\Day\Time\Time; |
15
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeInterval; |
16
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeIntervalInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract day class. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractDay implements DayInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The days of the week. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private const DAYS_OF_WEEK = [ |
29
|
|
|
DayInterface::WEEK_DAY_MONDAY => 'Monday', |
30
|
|
|
DayInterface::WEEK_DAY_TUESDAY => 'Tuesday', |
31
|
|
|
DayInterface::WEEK_DAY_WEDNESDAY => 'Wednesday', |
32
|
|
|
DayInterface::WEEK_DAY_THURSDAY => 'Thursday', |
33
|
|
|
DayInterface::WEEK_DAY_FRIDAY => 'Friday', |
34
|
|
|
DayInterface::WEEK_DAY_SATURDAY => 'Saturday', |
35
|
|
|
DayInterface::WEEK_DAY_SUNDAY => 'Sunday', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The day of week. |
40
|
|
|
* |
41
|
|
|
* @var integer |
42
|
|
|
*/ |
43
|
|
|
protected $dayOfWeek; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The time intervals. |
47
|
|
|
* |
48
|
|
|
* @var TimeIntervalInterface[] |
49
|
93 |
|
*/ |
50
|
|
|
protected $openingHoursIntervals; |
51
|
93 |
|
|
52
|
90 |
|
/** |
53
|
84 |
|
* @param integer $dayOfWeek The day of week. |
54
|
|
|
* @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals. |
55
|
|
|
*/ |
56
|
|
|
public function __construct(int $dayOfWeek, array $openingHoursIntervals) |
57
|
|
|
{ |
58
|
36 |
|
$this->setDayOfWeek($dayOfWeek); |
59
|
|
|
$this->setOpeningHoursIntervals($openingHoursIntervals); |
60
|
36 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getDayOfWeek(): int |
66
|
9 |
|
{ |
67
|
|
|
return $this->dayOfWeek; |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getDayOfWeekName(): string |
74
|
72 |
|
{ |
75
|
|
|
return self::DAYS_OF_WEEK[$this->dayOfWeek]; |
76
|
72 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getOpeningHoursIntervals(): array |
82
|
30 |
|
{ |
83
|
|
|
return $this->openingHoursIntervals; |
84
|
30 |
|
} |
85
|
30 |
|
|
86
|
18 |
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
24 |
|
*/ |
89
|
|
|
public function getClosestPreviousOpeningHoursInterval(Time $time): ?TimeIntervalInterface |
90
|
15 |
|
{ |
91
|
|
|
foreach ($this->openingHoursIntervals as $openingHoursInterval) { |
92
|
|
|
if ($openingHoursInterval->contains($time)) { |
93
|
|
|
return $openingHoursInterval; |
94
|
|
|
} |
95
|
|
|
} |
96
|
30 |
|
|
97
|
|
|
return $this->getPreviousOpeningHoursInterval($time); |
98
|
30 |
|
} |
99
|
30 |
|
|
100
|
18 |
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
21 |
|
*/ |
103
|
|
|
public function getClosestNextOpeningHoursInterval(Time $time): ?TimeIntervalInterface |
104
|
15 |
|
{ |
105
|
|
|
foreach ($this->openingHoursIntervals as $openingHoursInterval) { |
106
|
|
|
if ($openingHoursInterval->contains($time)) { |
107
|
|
|
return $openingHoursInterval; |
108
|
|
|
} |
109
|
|
|
} |
110
|
24 |
|
|
111
|
|
|
return $this->getNextOpeningHoursInterval($time); |
112
|
24 |
|
} |
113
|
24 |
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
24 |
|
*/ |
117
|
24 |
|
public function getPreviousOpeningHoursInterval(Time $time): ?TimeIntervalInterface |
118
|
|
|
{ |
119
|
24 |
|
$closestTime = null; |
120
|
21 |
|
$closestInterval = null; |
121
|
|
|
|
122
|
|
|
/** @var TimeIntervalInterface $interval */ |
123
|
15 |
|
foreach (\array_reverse($this->openingHoursIntervals) as $interval) { |
124
|
15 |
|
$distance = $time->toSeconds() - $interval->getEnd()->toSeconds(); |
125
|
15 |
|
|
126
|
15 |
|
if ($distance < 0) { |
127
|
|
|
continue; |
128
|
15 |
|
} |
129
|
|
|
|
130
|
|
|
if ($closestTime === null) { |
131
|
|
|
$closestTime = $interval->getEnd(); |
132
|
24 |
|
$closestInterval = $interval; |
133
|
|
|
} |
134
|
24 |
|
|
135
|
|
|
if ($distance < ($time->toSeconds() - $closestTime->toSeconds())) { |
136
|
|
|
$closestTime = $interval->getEnd(); |
137
|
|
|
$closestInterval = $interval; |
138
|
|
|
} |
139
|
|
|
} |
140
|
24 |
|
|
141
|
|
|
return $closestInterval; |
142
|
24 |
|
} |
143
|
24 |
|
|
144
|
|
|
/** |
145
|
24 |
|
* {@inheritdoc} |
146
|
24 |
|
*/ |
147
|
|
|
public function getNextOpeningHoursInterval(Time $time): ?TimeIntervalInterface |
148
|
24 |
|
{ |
149
|
21 |
|
$closestTime = null; |
150
|
|
|
$closestInterval = null; |
151
|
|
|
|
152
|
15 |
|
foreach ($this->openingHoursIntervals as $interval) { |
153
|
15 |
|
$distance = $interval->getStart()->toSeconds() - $time->toSeconds(); |
154
|
15 |
|
|
155
|
15 |
|
if ($distance < 0) { |
156
|
|
|
continue; |
157
|
15 |
|
} |
158
|
|
|
|
159
|
|
|
if ($closestTime === null) { |
160
|
|
|
$closestTime = $interval->getStart(); |
161
|
24 |
|
$closestInterval = $interval; |
162
|
|
|
} |
163
|
24 |
|
|
164
|
|
|
if ($distance < ($closestTime->toSeconds() - $time->toSeconds())) { |
165
|
|
|
$closestTime = $interval->getStart(); |
166
|
|
|
$closestInterval = $interval; |
167
|
|
|
} |
168
|
|
|
} |
169
|
9 |
|
|
170
|
|
|
return $closestInterval; |
171
|
9 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function getOpeningTime(): Time |
177
|
9 |
|
{ |
178
|
|
|
return $this->openingHoursIntervals[0]->getStart(); |
179
|
|
|
} |
180
|
9 |
|
|
181
|
|
|
/** |
182
|
9 |
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function getClosingTime(): Time |
185
|
|
|
{ |
186
|
|
|
/** @var TimeIntervalInterface $interval */ |
187
|
|
|
$interval = \end($this->openingHoursIntervals); |
188
|
60 |
|
|
189
|
|
|
return $interval->getEnd(); |
190
|
60 |
|
} |
191
|
60 |
|
|
192
|
36 |
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
45 |
|
*/ |
195
|
|
|
public function isWithinOpeningHours(Time $time): bool |
196
|
27 |
|
{ |
197
|
|
|
foreach ($this->openingHoursIntervals as $interval) { |
198
|
|
|
if ($interval->contains($time)) { |
199
|
|
|
return true; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return false; |
204
|
|
|
} |
205
|
93 |
|
|
206
|
|
|
/** |
207
|
93 |
|
* Set the day of week. |
208
|
3 |
|
* |
209
|
|
|
* @param integer $dayOfWeek |
210
|
|
|
* @throws \OutOfBoundsException If the given day is invalid. |
211
|
90 |
|
*/ |
212
|
90 |
|
protected function setDayOfWeek(int $dayOfWeek): void |
213
|
|
|
{ |
214
|
|
|
if (!isset(self::DAYS_OF_WEEK[$dayOfWeek])) { |
215
|
|
|
throw new \OutOfBoundsException(\sprintf('Invalid day of week "%s".', $dayOfWeek)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->dayOfWeek = $dayOfWeek; |
219
|
|
|
} |
220
|
90 |
|
|
221
|
|
|
/** |
222
|
90 |
|
* Set the opening hours intervals. |
223
|
3 |
|
* |
224
|
|
|
* @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals. |
225
|
|
|
* @throws \InvalidArgumentException If no days are passed or invalid interval is passed. |
226
|
87 |
|
*/ |
227
|
|
|
protected function setOpeningHoursIntervals(array $openingHoursIntervals): void |
228
|
87 |
|
{ |
229
|
87 |
|
if (empty($openingHoursIntervals)) { |
230
|
3 |
|
throw new \InvalidArgumentException('The day must have at least one opening interval.'); |
231
|
|
|
} |
232
|
|
|
|
233
|
84 |
|
$intervals = []; |
234
|
84 |
|
|
235
|
|
|
foreach ($openingHoursIntervals as $interval) { |
236
|
84 |
|
if (!$interval instanceof TimeIntervalInterface) { |
237
|
84 |
|
throw new \InvalidArgumentException(\sprintf('Interval must be a %s', TimeIntervalInterface::class)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$intervals[] = $interval; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->openingHoursIntervals = $this->flattenOpeningHoursIntervals($intervals); |
244
|
|
|
} |
245
|
84 |
|
|
246
|
|
|
/** |
247
|
84 |
|
* Flatten the intervals that overlap. |
248
|
84 |
|
* |
249
|
75 |
|
* @param TimeIntervalInterface[] $openingHoursIntervals |
250
|
75 |
|
* @return TimeIntervalInterface[] |
251
|
|
|
*/ |
252
|
84 |
|
protected function flattenOpeningHoursIntervals(array $openingHoursIntervals): array |
253
|
|
|
{ |
254
|
84 |
|
\usort( |
255
|
84 |
|
$openingHoursIntervals, |
256
|
84 |
|
function (TimeIntervalInterface $a, TimeIntervalInterface $b) { |
257
|
|
|
return ($a->getStart() > $b->getStart()) ? 1 : -1; |
258
|
84 |
|
} |
259
|
84 |
|
); |
260
|
84 |
|
|
261
|
84 |
|
$intervals = []; |
262
|
84 |
|
$tmpInterval = \reset($openingHoursIntervals); |
263
|
84 |
|
foreach ($openingHoursIntervals as $interval) { |
264
|
75 |
|
/** @var TimeInterval $tmpInterval */ |
265
|
75 |
|
if ($interval->getStart() <= $tmpInterval->getEnd()) { |
266
|
|
|
$tmpInterval = new TimeInterval( |
267
|
84 |
|
$tmpInterval->getStart(), |
268
|
|
|
\max($tmpInterval->getEnd(), $interval->getEnd()) |
269
|
84 |
|
); |
270
|
|
|
} else { |
271
|
84 |
|
$intervals[] = $tmpInterval; |
272
|
|
|
$tmpInterval = $interval; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$intervals[] = $tmpInterval; |
277
|
9 |
|
|
278
|
|
|
return $intervals; |
279
|
9 |
|
} |
280
|
|
|
|
281
|
9 |
|
public function __clone() |
282
|
9 |
|
{ |
283
|
9 |
|
$openingHoursIntervals = []; |
284
|
|
|
|
285
|
9 |
|
foreach ($this->openingHoursIntervals as $openingHoursInterval) { |
286
|
9 |
|
$openingHoursIntervals[] = clone $openingHoursInterval; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$this->openingHoursIntervals = $openingHoursIntervals; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|