1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\OpeningHours\Entities; |
4
|
|
|
|
5
|
|
|
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidTimeRangeString; |
6
|
|
|
|
7
|
|
|
class TimeRange |
8
|
|
|
{ |
9
|
|
|
/** @var \Spatie\OpeningHours\Time */ |
10
|
|
|
protected $start; |
11
|
|
|
|
12
|
|
|
/** @var \Spatie\OpeningHours\Time */ |
13
|
|
|
protected $end; |
14
|
|
|
|
15
|
43 |
|
protected function __construct(Time $start, Time $end) |
16
|
|
|
{ |
17
|
43 |
|
$this->start = $start; |
|
|
|
|
18
|
43 |
|
$this->end = $end; |
|
|
|
|
19
|
43 |
|
} |
20
|
|
|
|
21
|
44 |
|
public static function fromString(string $string): self |
22
|
|
|
{ |
23
|
44 |
|
$times = explode('-', $string); |
24
|
|
|
|
25
|
44 |
|
if (count($times) !== 2) { |
26
|
1 |
|
throw InvalidTimeRangeString::forString($string); |
27
|
|
|
} |
28
|
|
|
|
29
|
43 |
|
return new self(Time::fromString($times[0]), Time::fromString($times[1])); |
30
|
|
|
} |
31
|
|
|
|
32
|
18 |
|
public function start(): Time |
33
|
|
|
{ |
34
|
18 |
|
return $this->start; |
35
|
|
|
} |
36
|
|
|
|
37
|
14 |
|
public function end(): Time |
38
|
|
|
{ |
39
|
14 |
|
return $this->end; |
40
|
|
|
} |
41
|
|
|
|
42
|
29 |
|
public function spillsOverToNextDay(): bool |
43
|
|
|
{ |
44
|
29 |
|
return $this->end->isBefore($this->start); |
45
|
|
|
} |
46
|
|
|
|
47
|
28 |
|
public function containsTime(Time $time): bool |
48
|
|
|
{ |
49
|
28 |
|
if ($this->spillsOverToNextDay()) { |
50
|
2 |
|
if ($time->isAfter($this->start)) { |
51
|
2 |
|
return $time->isAfter($this->end); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return $time->isBefore($this->end); |
55
|
|
|
} |
56
|
|
|
|
57
|
28 |
|
return $time->isSameOrAfter($this->start) && $time->isBefore($this->end); |
58
|
|
|
} |
59
|
|
|
|
60
|
22 |
|
public function overlaps(TimeRange $timeRange): bool |
61
|
|
|
{ |
62
|
22 |
|
return $this->containsTime($timeRange->start) || $this->containsTime($timeRange->end); |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
public function format(string $timeFormat = 'H:i', string $rangeFormat = '%s-%s'): string |
66
|
|
|
{ |
67
|
9 |
|
return sprintf($rangeFormat, $this->start->format($timeFormat), $this->end->format($timeFormat)); |
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
public function __toString(): string |
71
|
|
|
{ |
72
|
8 |
|
return $this->format(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..