|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\OpeningHours\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use DateTimeZone; |
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Webfactor\Laravel\OpeningHours\Exceptions\Exception; |
|
9
|
|
|
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidDate; |
|
10
|
|
|
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidDayName; |
|
11
|
|
|
use Webfactor\Laravel\OpeningHours\Helpers\Arr; |
|
12
|
|
|
|
|
13
|
|
|
class OpeningHours |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Day[] */ |
|
16
|
|
|
protected $openingHours = []; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $exceptions = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var DateTimeZone|null */ |
|
22
|
|
|
protected $timezone = null; |
|
23
|
|
|
|
|
24
|
29 |
|
public function __construct($timezone = null) |
|
25
|
|
|
{ |
|
26
|
29 |
|
$this->timezone = $timezone ? new DateTimeZone($timezone) : null; |
|
27
|
|
|
|
|
28
|
29 |
|
$this->openingHours = Day::mapDays(function () { |
|
29
|
29 |
|
return new OpeningHoursForDay(); |
|
30
|
29 |
|
}); |
|
31
|
29 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $data |
|
35
|
|
|
* |
|
36
|
|
|
* @return static |
|
37
|
|
|
*/ |
|
38
|
29 |
|
public static function create(array $data) |
|
39
|
|
|
{ |
|
40
|
29 |
|
return (new static())->fill($data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public static function isValid(array $data): bool |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
1 |
|
static::create($data); |
|
52
|
|
|
|
|
53
|
1 |
|
return true; |
|
54
|
1 |
|
} catch (Exception $exception) { |
|
55
|
1 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
29 |
|
public function fill(array $data) |
|
60
|
|
|
{ |
|
61
|
29 |
|
list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data); |
|
62
|
|
|
|
|
63
|
28 |
|
foreach ($openingHours as $day => $openingHoursForThisDay) { |
|
64
|
21 |
|
$this->setOpeningHoursFromStrings($day, $openingHoursForThisDay); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
28 |
|
$this->setExceptionsFromStrings($exceptions); |
|
68
|
|
|
|
|
69
|
27 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function forWeek(): array |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->openingHours; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
17 |
|
public function forDay(string $day): OpeningHoursForDay |
|
78
|
|
|
{ |
|
79
|
17 |
|
$day = $this->normalizeDayName($day); |
|
80
|
|
|
|
|
81
|
17 |
|
return $this->openingHours[$day]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
11 |
|
public function forDate(DateTimeInterface $date): OpeningHoursForDay |
|
85
|
|
|
{ |
|
86
|
11 |
|
$date = $this->applyTimezone($date); |
|
87
|
|
|
|
|
88
|
11 |
|
return $this->exceptions[$date->format('Y-m-d')] ?? ($this->exceptions[$date->format('m-d')] ?? $this->forDay(Day::onDateTime($date))); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function exceptions(): array |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->exceptions; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
public function isOpenOn(string $day): bool |
|
97
|
|
|
{ |
|
98
|
5 |
|
return count($this->forDay($day)) > 0; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
public function isClosedOn(string $day): bool |
|
102
|
|
|
{ |
|
103
|
4 |
|
return ! $this->isOpenOn($day); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
public function isOpenAt(DateTimeInterface $dateTime): bool |
|
107
|
|
|
{ |
|
108
|
5 |
|
$dateTime = $this->applyTimezone($dateTime); |
|
109
|
|
|
|
|
110
|
5 |
|
$openingHoursForDay = $this->forDate($dateTime); |
|
111
|
|
|
|
|
112
|
5 |
|
return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
5 |
|
public function isClosedAt(DateTimeInterface $dateTime): bool |
|
116
|
|
|
{ |
|
117
|
5 |
|
return ! $this->isOpenAt($dateTime); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function isOpen(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->isOpenAt(new DateTime()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
public function isClosed(): bool |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->isClosedAt(new DateTime()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
4 |
|
public function nextOpen(DateTimeInterface $dateTime): DateTime |
|
131
|
|
|
{ |
|
132
|
4 |
|
$openingHoursForDay = $this->forDate($dateTime); |
|
133
|
4 |
|
$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime)); |
|
134
|
|
|
|
|
135
|
4 |
|
while ($nextOpen == false) { |
|
136
|
|
|
$dateTime |
|
137
|
3 |
|
->modify('+1 day') |
|
138
|
3 |
|
->setTime(0, 0, 0); |
|
139
|
|
|
|
|
140
|
3 |
|
$openingHoursForDay = $this->forDate($dateTime); |
|
141
|
|
|
|
|
142
|
3 |
|
$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
4 |
|
$nextDateTime = $nextOpen->toDateTime(); |
|
146
|
4 |
|
$dateTime->setTime($nextDateTime->format('G'), $nextDateTime->format('i'), 0); |
|
147
|
|
|
|
|
148
|
4 |
|
return $dateTime; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function regularClosingDays(): array |
|
152
|
|
|
{ |
|
153
|
2 |
|
return array_keys($this->filter(function (OpeningHoursForDay $openingHoursForDay) { |
|
154
|
2 |
|
return $openingHoursForDay->isEmpty(); |
|
155
|
2 |
|
})); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
public function regularClosingDaysISO(): array |
|
159
|
|
|
{ |
|
160
|
1 |
|
return Arr::map($this->regularClosingDays(), [Day::class, 'toISO']); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function exceptionalClosingDates(): array |
|
164
|
|
|
{ |
|
165
|
1 |
|
$dates = array_keys($this->filterExceptions(function (OpeningHoursForDay $openingHoursForDay, $date) { |
|
|
|
|
|
|
166
|
1 |
|
return $openingHoursForDay->isEmpty(); |
|
167
|
1 |
|
})); |
|
168
|
|
|
|
|
169
|
1 |
|
return Arr::map($dates, function ($date) { |
|
170
|
1 |
|
return DateTime::createFromFormat('Y-m-d', $date); |
|
171
|
1 |
|
}); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function setTimezone($timezone) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->timezone = new DateTimeZone($timezone); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
29 |
|
protected function parseOpeningHoursAndExceptions(array $data): array |
|
180
|
|
|
{ |
|
181
|
29 |
|
$exceptions = Arr::pull($data, 'exceptions', []); |
|
182
|
29 |
|
$openingHours = []; |
|
183
|
|
|
|
|
184
|
29 |
|
foreach ($data as $day => $openingHoursData) { |
|
185
|
22 |
|
$openingHours[$this->normalizeDayName($day)] = $openingHoursData; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
28 |
|
return [$openingHours, $exceptions]; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
21 |
|
protected function setOpeningHoursFromStrings(string $day, array $openingHours) |
|
192
|
|
|
{ |
|
193
|
21 |
|
$day = $this->normalizeDayName($day); |
|
194
|
|
|
|
|
195
|
21 |
|
$this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours); |
|
196
|
21 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
protected function setExceptionsFromStrings(array $exceptions) |
|
199
|
|
|
{ |
|
200
|
28 |
|
$this->exceptions = Arr::map($exceptions, function (array $openingHours, string $date) { |
|
201
|
10 |
|
$recurring = DateTime::createFromFormat('m-d', $date); |
|
202
|
|
|
|
|
203
|
10 |
|
if ($recurring === false || $recurring->format('m-d') !== $date) { |
|
204
|
9 |
|
$dateTime = DateTime::createFromFormat('Y-m-d', $date); |
|
205
|
|
|
|
|
206
|
9 |
|
if ($dateTime === false || $dateTime->format('Y-m-d') !== $date) { |
|
207
|
1 |
|
throw InvalidDate::invalidDate($date); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
9 |
|
return OpeningHoursForDay::fromStrings($openingHours); |
|
212
|
28 |
|
}); |
|
213
|
27 |
|
} |
|
214
|
|
|
|
|
215
|
25 |
|
protected function normalizeDayName(string $day) |
|
216
|
|
|
{ |
|
217
|
25 |
|
$day = strtolower($day); |
|
218
|
|
|
|
|
219
|
25 |
|
if (! Day::isValid($day)) { |
|
220
|
2 |
|
throw new InvalidDayName(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
24 |
|
return $day; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
11 |
|
protected function applyTimezone(DateTimeInterface $date) |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
11 |
|
if ($this->timezone) { |
|
229
|
|
|
$date = $date->setTimezone($this->timezone); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
11 |
|
return $date; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
2 |
|
public function filter(callable $callback): array |
|
236
|
|
|
{ |
|
237
|
2 |
|
return Arr::filter($this->openingHours, $callback); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function map(callable $callback): array |
|
241
|
|
|
{ |
|
242
|
|
|
return Arr::map($this->openingHours, $callback); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
2 |
|
public function flatMap(callable $callback): array |
|
246
|
|
|
{ |
|
247
|
2 |
|
return Arr::flatMap($this->openingHours, $callback); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
1 |
|
public function filterExceptions(callable $callback): array |
|
251
|
|
|
{ |
|
252
|
1 |
|
return Arr::filter($this->exceptions, $callback); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function mapExceptions(callable $callback): array |
|
256
|
|
|
{ |
|
257
|
|
|
return Arr::map($this->exceptions, $callback); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
1 |
|
public function flatMapExceptions(callable $callback): array |
|
261
|
|
|
{ |
|
262
|
1 |
|
return Arr::flatMap($this->exceptions, $callback); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
public function asStructuredData(): array |
|
266
|
|
|
{ |
|
267
|
|
View Code Duplication |
$regularHours = $this->flatMap(function (OpeningHoursForDay $openingHoursForDay, string $day) { |
|
|
|
|
|
|
268
|
1 |
|
return $openingHoursForDay->map(function (TimeRange $timeRange) use ($day) { |
|
269
|
|
|
return [ |
|
270
|
1 |
|
'@type' => 'OpeningHoursSpecification', |
|
271
|
1 |
|
'dayOfWeek' => ucfirst($day), |
|
272
|
1 |
|
'opens' => (string) $timeRange->start(), |
|
273
|
1 |
|
'closes' => (string) $timeRange->end(), |
|
274
|
|
|
]; |
|
275
|
1 |
|
}); |
|
276
|
1 |
|
}); |
|
277
|
|
|
|
|
278
|
1 |
|
$exceptions = $this->flatMapExceptions(function (OpeningHoursForDay $openingHoursForDay, string $date) { |
|
279
|
1 |
|
if ($openingHoursForDay->isEmpty()) { |
|
280
|
|
|
return [[ |
|
281
|
1 |
|
'@type' => 'OpeningHoursSpecification', |
|
282
|
1 |
|
'opens' => '00:00', |
|
283
|
1 |
|
'closes' => '00:00', |
|
284
|
1 |
|
'validFrom' => $date, |
|
285
|
1 |
|
'validThrough' => $date, |
|
286
|
|
|
]]; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
1 |
View Code Duplication |
return $openingHoursForDay->map(function (TimeRange $timeRange) use ($date) { |
|
|
|
|
|
|
290
|
|
|
return [ |
|
291
|
1 |
|
'@type' => 'OpeningHoursSpecification', |
|
292
|
1 |
|
'opens' => $timeRange->start(), |
|
293
|
1 |
|
'closes' => $timeRange->end(), |
|
294
|
1 |
|
'validFrom' => $date, |
|
295
|
1 |
|
'validThrough' => $date, |
|
296
|
|
|
]; |
|
297
|
1 |
|
}); |
|
298
|
1 |
|
}); |
|
299
|
|
|
|
|
300
|
1 |
|
return array_merge($regularHours, $exceptions); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.