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