|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Speicher210\BusinessHours\Day\Time; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Represents a time interval. |
|
9
|
|
|
*/ |
|
10
|
|
|
class TimeInterval implements TimeIntervalInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The start time. |
|
14
|
|
|
* |
|
15
|
|
|
* @var Time |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $start; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The end time. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Time |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $end; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Time $start The start time. |
|
28
|
|
|
* @param Time $end The end time. |
|
29
|
|
|
* @throws \InvalidArgumentException If the opening time is not earlier than closing time. |
|
30
|
|
|
*/ |
|
31
|
102 |
|
public function __construct(Time $start, Time $end) |
|
32
|
|
|
{ |
|
33
|
102 |
|
$this->start = $start; |
|
34
|
102 |
|
$this->end = $end; |
|
35
|
|
|
|
|
36
|
102 |
|
if ($start->isAfterOrEqual($end)) { |
|
37
|
6 |
|
throw new \InvalidArgumentException( |
|
38
|
6 |
|
\sprintf('The opening time "%s" must be before the closing time "%s".', $start, $end) |
|
39
|
6 |
|
); |
|
40
|
|
|
} |
|
41
|
96 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new interval from time strings. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $startTime The start time |
|
47
|
|
|
* @param string $endTime The end time |
|
48
|
|
|
* @return TimeInterval |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
27 |
|
public static function fromString($startTime, $endTime): self |
|
52
|
|
|
{ |
|
53
|
27 |
|
return new static(TimeBuilder::fromString($startTime), TimeBuilder::fromString($endTime)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
99 |
|
public function contains(Time $time): bool |
|
60
|
|
|
{ |
|
61
|
99 |
|
return $this->start->isBeforeOrEqual($time) && $this->end->isAfterOrEqual($time); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
135 |
|
public function getStart(): Time |
|
68
|
|
|
{ |
|
69
|
135 |
|
return $this->start; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
135 |
|
public function getEnd(): Time |
|
76
|
|
|
{ |
|
77
|
135 |
|
return $this->end; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
9 |
|
public function jsonSerialize() |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
9 |
|
'start' => $this->start, |
|
87
|
9 |
|
'end' => $this->end, |
|
88
|
9 |
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function __clone() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->start = clone $this->start; |
|
94
|
12 |
|
$this->end = clone $this->end; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|