1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\OpeningHours; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Spatie\OpeningHours\Exceptions\Exception; |
8
|
|
|
use Spatie\OpeningHours\Exceptions\InvalidDate; |
9
|
|
|
use Spatie\OpeningHours\Exceptions\InvalidDayName; |
10
|
|
|
use Spatie\OpeningHours\Helpers\Arr; |
11
|
|
|
|
12
|
|
|
class OpeningHours |
13
|
|
|
{ |
14
|
|
|
/** @var \Spatie\OpeningHours\Day[] */ |
15
|
|
|
protected $openingHours; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
protected $exceptions = []; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->openingHours = Day::mapDays(function () { |
23
|
|
|
return new OpeningHoursForDay(); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $data |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
public static function create(array $data) |
33
|
|
|
{ |
34
|
|
|
return (new static())->fill($data); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $data |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public static function isValid(array $data): bool |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
static::create($data); |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} catch (Exception $exception) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function fill(array $data) |
54
|
|
|
{ |
55
|
|
|
list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data); |
56
|
|
|
|
57
|
|
|
foreach ($openingHours as $day => $openingHoursForThisDay) { |
58
|
|
|
$this->setOpeningHoursFromStrings($day, $openingHoursForThisDay); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->setExceptionsFromStrings($exceptions); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function forWeek(): array |
67
|
|
|
{ |
68
|
|
|
return $this->openingHours; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function forDay(string $day): OpeningHoursForDay |
72
|
|
|
{ |
73
|
|
|
$day = $this->normalizeDayName($day); |
74
|
|
|
|
75
|
|
|
return $this->openingHours[$day]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function forDate(DateTimeInterface $date): OpeningHoursForDay |
79
|
|
|
{ |
80
|
|
|
return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function exceptions(): array |
84
|
|
|
{ |
85
|
|
|
return $this->exceptions; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function isOpenOn(string $day): bool |
89
|
|
|
{ |
90
|
|
|
return count($this->forDay($day)) > 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isClosedOn(string $day): bool |
94
|
|
|
{ |
95
|
|
|
return ! $this->isOpenOn($day); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function isOpenAt(DateTimeInterface $dateTime): bool |
99
|
|
|
{ |
100
|
|
|
$openingHoursForDay = $this->forDate($dateTime); |
101
|
|
|
|
102
|
|
|
return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isClosedAt(DateTimeInterface $dateTime): bool |
106
|
|
|
{ |
107
|
|
|
return ! $this->isOpenAt($dateTime); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isOpen(): bool |
111
|
|
|
{ |
112
|
|
|
return $this->isOpenAt(new DateTime()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function isClosed(): bool |
116
|
|
|
{ |
117
|
|
|
return $this->isClosedAt(new DateTime()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function nextOpen(DateTimeInterface $dateTime) : DateTime |
121
|
|
|
{ |
122
|
|
|
$openingHoursForDay = $this->forDate($dateTime); |
123
|
|
|
$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime)); |
124
|
|
|
|
125
|
|
|
while ($nextOpen == false) { |
126
|
|
|
$dateTime |
127
|
|
|
->modify('+1 day') |
128
|
|
|
->setTime(0, 0, 0) |
129
|
|
|
; |
130
|
|
|
|
131
|
|
|
$openingHoursForDay = $this->forDate($dateTime); |
132
|
|
|
|
133
|
|
|
$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$nextDateTime = $nextOpen->toDateTime(); |
137
|
|
|
$dateTime->setTime($nextDateTime->format('G'), $nextDateTime->format('i'), 0); |
138
|
|
|
|
139
|
|
|
return $dateTime; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function parseOpeningHoursAndExceptions(array $data): array |
143
|
|
|
{ |
144
|
|
|
$exceptions = Arr::pull($data, 'exceptions', []); |
145
|
|
|
$openingHours = []; |
146
|
|
|
|
147
|
|
|
foreach ($data as $day => $openingHoursData) { |
148
|
|
|
$openingHours[$this->normalizeDayName($day)] = $openingHoursData; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return [$openingHours, $exceptions]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function setOpeningHoursFromStrings(string $day, array $openingHours) |
155
|
|
|
{ |
156
|
|
|
$day = $this->normalizeDayName($day); |
157
|
|
|
|
158
|
|
|
$this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function setExceptionsFromStrings(array $exceptions) |
162
|
|
|
{ |
163
|
|
|
$this->exceptions = Arr::map($exceptions, function (array $openingHours, string $date) { |
164
|
|
|
$dateTime = DateTime::createFromFormat('Y-m-d', $date); |
165
|
|
|
|
166
|
|
|
if ($dateTime === false || $dateTime->format('Y-m-d') !== $date) { |
167
|
|
|
throw InvalidDate::invalidDate($date); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return OpeningHoursForDay::fromStrings($openingHours); |
171
|
|
|
}); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function normalizeDayName(string $day) |
175
|
|
|
{ |
176
|
|
|
$day = strtolower($day); |
177
|
|
|
|
178
|
|
|
if (! Day::isValid($day)) { |
179
|
|
|
throw new InvalidDayName(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $day; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|