|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\OpeningHours; |
|
4
|
|
|
|
|
5
|
|
|
use Countable; |
|
6
|
|
|
use ArrayAccess; |
|
7
|
|
|
use ArrayIterator; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
use Spatie\OpeningHours\Helpers\Arr; |
|
10
|
|
|
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges; |
|
11
|
|
|
|
|
12
|
|
|
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Spatie\OpeningHours\TimeRange[] */ |
|
15
|
|
|
protected $openingHours = []; |
|
16
|
|
|
|
|
17
|
|
|
public static function fromStrings(array $strings) |
|
18
|
|
|
{ |
|
19
|
|
|
$openingHoursForDay = new static(); |
|
20
|
|
|
|
|
21
|
|
|
$timeRanges = Arr::map($strings, function ($string) { |
|
22
|
|
|
return TimeRange::fromString($string); |
|
23
|
|
|
}); |
|
24
|
|
|
|
|
25
|
|
|
$openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges); |
|
26
|
|
|
|
|
27
|
|
|
$openingHoursForDay->openingHours = $timeRanges; |
|
28
|
|
|
|
|
29
|
|
|
return $openingHoursForDay; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function isOpenAt(Time $time) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($this->openingHours as $timeRange) { |
|
35
|
|
|
if ($timeRange->containsTime($time)) { |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function nextOpen(Time $time) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach ($this->openingHours as $timeRange) { |
|
46
|
|
|
if ($nextOpen = $this->findNextOpenInWorkingHours($time, $timeRange)) { |
|
47
|
|
|
return $nextOpen; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($nextOpen = $this->findNextOpenInFreeTime($time, $timeRange)) { |
|
|
|
|
|
|
51
|
|
|
return $nextOpen; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function findNextOpenInWorkingHours(Time $time, TimeRange $timeRange) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($timeRange->containsTime($time) && next($timeRange) !== $timeRange) { |
|
61
|
|
|
return next($timeRange); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange, TimeRange &$prevTimeRange = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$timeOffRange = $prevTimeRange ? |
|
68
|
|
|
TimeRange::fromString($prevTimeRange->end().'-'.$timeRange->start()) : |
|
69
|
|
|
TimeRange::fromString('00:00-'.$timeRange->start()); |
|
70
|
|
|
|
|
71
|
|
|
if ($timeOffRange->containsTime($time) || $timeOffRange->start()->isSame($time)) { |
|
72
|
|
|
return $timeRange->start(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$prevTimeRange = $timeRange; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function offsetExists($offset): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return isset($this->openingHours[$offset]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function offsetGet($offset) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->openingHours[$offset]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function offsetSet($offset, $value) |
|
89
|
|
|
{ |
|
90
|
|
|
throw new \Exception(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function offsetUnset($offset) |
|
94
|
|
|
{ |
|
95
|
|
|
unset($this->openingHours[$offset]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function count(): int |
|
99
|
|
|
{ |
|
100
|
|
|
return count($this->openingHours); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getIterator() |
|
104
|
|
|
{ |
|
105
|
|
|
return new ArrayIterator($this->openingHours); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function isEmpty(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return empty($this->openingHours); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function map(callable $callback): array |
|
114
|
|
|
{ |
|
115
|
|
|
return Arr::map($this->openingHours, $callback); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function guardAgainstTimeRangeOverlaps(array $openingHours) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach (Arr::createUniquePairs($openingHours) as $timeRanges) { |
|
121
|
|
|
if ($timeRanges[0]->overlaps($timeRanges[1])) { |
|
122
|
|
|
throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function __toString() |
|
128
|
|
|
{ |
|
129
|
|
|
$values = []; |
|
130
|
|
|
foreach ($this->openingHours as $openingHour) { |
|
131
|
|
|
$values[] = (string) $openingHour; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return implode(',', $values); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.