|
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
|
31 |
|
protected function __construct(Time $start, Time $end) |
|
16
|
|
|
{ |
|
17
|
31 |
|
$this->start = $start; |
|
|
|
|
|
|
18
|
31 |
|
$this->end = $end; |
|
|
|
|
|
|
19
|
31 |
|
} |
|
20
|
|
|
|
|
21
|
32 |
|
public static function fromString(string $string): self |
|
22
|
|
|
{ |
|
23
|
32 |
|
$times = explode('-', $string); |
|
24
|
|
|
|
|
25
|
32 |
|
if (count($times) !== 2) { |
|
26
|
1 |
|
throw InvalidTimeRangeString::forString($string); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
31 |
|
return new self(Time::fromString($times[0]), Time::fromString($times[1])); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
7 |
|
public function start(): Time |
|
33
|
|
|
{ |
|
34
|
7 |
|
return $this->start; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function end(): Time |
|
38
|
|
|
{ |
|
39
|
3 |
|
return $this->end; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
17 |
|
public function spillsOverToNextDay(): bool |
|
43
|
|
|
{ |
|
44
|
17 |
|
return $this->end->isBefore($this->start); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
16 |
|
public function containsTime(Time $time): bool |
|
48
|
|
|
{ |
|
49
|
16 |
|
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
|
16 |
|
return $time->isSameOrAfter($this->start) && $time->isBefore($this->end); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
public function overlaps(TimeRange $timeRange): bool |
|
61
|
|
|
{ |
|
62
|
10 |
|
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..