1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\OpeningHours; |
4
|
|
|
|
5
|
|
|
use Spatie\OpeningHours\Exceptions\InvalidTimeRangeList; |
6
|
|
|
use Spatie\OpeningHours\Exceptions\InvalidTimeRangeString; |
7
|
|
|
|
8
|
|
|
class TimeRange |
9
|
|
|
{ |
10
|
|
|
/** @var \Spatie\OpeningHours\Time */ |
11
|
|
|
protected $start; |
12
|
|
|
|
13
|
|
|
/** @var \Spatie\OpeningHours\Time */ |
14
|
|
|
protected $end; |
15
|
|
|
|
16
|
|
|
protected function __construct(Time $start, Time $end) |
17
|
|
|
{ |
18
|
|
|
$this->start = $start; |
19
|
|
|
$this->end = $end; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function fromString(string $string): self |
23
|
|
|
{ |
24
|
|
|
$times = explode('-', $string); |
25
|
|
|
|
26
|
|
|
if (count($times) !== 2) { |
27
|
|
|
throw InvalidTimeRangeString::forString($string); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return new self(Time::fromString($times[0]), Time::fromString($times[1])); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function fromList(array $ranges): self |
34
|
|
|
{ |
35
|
|
|
if (count($ranges) === 0) { |
36
|
|
|
throw InvalidTimeRangeList::create(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($ranges as $range) { |
40
|
|
|
if (! ($range instanceof self)) { |
41
|
|
|
throw InvalidTimeRangeList::create(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$start = $ranges[0]->start(); |
46
|
|
|
$end = $ranges[0]->end(); |
47
|
|
|
|
48
|
|
|
foreach (array_slice($ranges, 1) as $range) { |
49
|
|
|
$rangeStart = $range->start(); |
50
|
|
|
if ($rangeStart->format('Gi') < $start->format('Gi')) { |
51
|
|
|
$start = $rangeStart; |
52
|
|
|
} |
53
|
|
|
$rangeEnd = $range->end(); |
54
|
|
|
if ($rangeEnd->format('Gi') > $end->format('Gi')) { |
55
|
|
|
$end = $rangeEnd; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return new self($start, $end); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function start(): Time |
63
|
|
|
{ |
64
|
|
|
return $this->start; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function end(): Time |
68
|
|
|
{ |
69
|
|
|
return $this->end; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function spillsOverToNextDay(): bool |
73
|
|
|
{ |
74
|
|
|
return $this->end->isBefore($this->start); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function containsTime(Time $time): bool |
78
|
|
|
{ |
79
|
|
|
if ($this->spillsOverToNextDay()) { |
80
|
|
|
if ($time->isSameOrAfter($this->start)) { |
|
|
|
|
81
|
|
|
return $time->isAfter($this->end); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $time->isBefore($this->end); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $time->isSameOrAfter($this->start) && $time->isBefore($this->end); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function overlaps(self $timeRange): bool |
91
|
|
|
{ |
92
|
|
|
return $this->containsTime($timeRange->start) || $this->containsTime($timeRange->end); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function format(string $timeFormat = 'H:i', string $rangeFormat = '%s-%s'): string |
96
|
|
|
{ |
97
|
|
|
return sprintf($rangeFormat, $this->start->format($timeFormat), $this->end->format($timeFormat)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function __toString(): string |
101
|
|
|
{ |
102
|
|
|
return $this->format(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: