1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Zee Project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @see https://github.com/zee/ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zee\DateRange; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use DateTimeInterface; |
15
|
|
|
use JsonSerializable; |
16
|
|
|
use Zee\DateRange\States\RangeState; |
17
|
|
|
use Zee\DateRange\States\UndefinedRange; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implementation of date range value object. |
21
|
|
|
*/ |
22
|
|
|
final class DateRange implements DateRangeInterface, JsonSerializable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var RangeState |
26
|
|
|
*/ |
27
|
|
|
private $state; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param DateTimeInterface|null $startTime |
31
|
|
|
* @param DateTimeInterface|null $endTime |
32
|
|
|
*/ |
33
|
11 |
|
public function __construct(DateTimeInterface $startTime = null, DateTimeInterface $endTime = null) |
34
|
|
|
{ |
35
|
11 |
|
$state = new UndefinedRange(); |
36
|
|
|
|
37
|
11 |
|
if (isset($startTime)) { |
38
|
6 |
|
$state = $state->setStartTime($startTime); |
39
|
|
|
} |
40
|
|
|
|
41
|
11 |
|
if (isset($endTime)) { |
42
|
6 |
|
$state = $state->setEndTime($endTime); |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
$this->state = $state; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns string representation of range. |
50
|
|
|
* |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
4 |
|
public function __toString(): string |
54
|
|
|
{ |
55
|
4 |
|
return (string) $this->state; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function __debugInfo() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
1 |
|
'startTime' => $this->state->hasStartTime() |
65
|
1 |
|
? $this->state->getStartTime() |
66
|
|
|
: null, |
67
|
1 |
|
'endTime' => $this->state->hasEndTime() |
68
|
1 |
|
? $this->state->getEndTime() |
69
|
|
|
: null, |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns value ready to be encoded as JSON. |
75
|
|
|
* |
76
|
|
|
* The ISO-8601 range format is used for times. |
77
|
|
|
* |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
4 |
|
public function jsonSerialize(): array |
81
|
|
|
{ |
82
|
4 |
|
return $this->state->jsonSerialize(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
4 |
|
public function hasStartTime(): bool |
89
|
|
|
{ |
90
|
4 |
|
return $this->state->hasStartTime(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
4 |
|
public function hasEndTime(): bool |
97
|
|
|
{ |
98
|
4 |
|
return $this->state->hasEndTime(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
6 |
|
public function getStartTime(): DateTimeInterface |
105
|
|
|
{ |
106
|
6 |
|
return $this->state->getStartTime(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
6 |
|
public function getEndTime(): DateTimeInterface |
113
|
|
|
{ |
114
|
6 |
|
return $this->state->getEndTime(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
4 |
|
public function setStartTime(DateTimeInterface $time): DateRangeInterface |
121
|
|
|
{ |
122
|
4 |
|
$clone = clone $this; |
123
|
4 |
|
$clone->state = $clone->state->setStartTime($time); |
124
|
|
|
|
125
|
4 |
|
return $clone; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
4 |
|
public function setEndTime(DateTimeInterface $time): DateRangeInterface |
132
|
|
|
{ |
133
|
4 |
|
$clone = clone $this; |
134
|
4 |
|
$clone->state = $clone->state->setEndTime($time); |
135
|
|
|
|
136
|
4 |
|
return $clone; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
4 |
|
public function isStartedAt(DateTimeInterface $time): bool |
143
|
|
|
{ |
144
|
4 |
|
return $this->hasStartTime() && $this->state->compareStartTime($time) === 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
4 |
|
public function isEndedAt(DateTimeInterface $time): bool |
151
|
|
|
{ |
152
|
4 |
|
return $this->hasEndTime() && $this->state->compareEndTime($time) === 0; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
4 |
|
public function isStarted(): bool |
159
|
|
|
{ |
160
|
4 |
|
if ($this->hasStartTime()) { |
161
|
2 |
|
return $this->state->compareStartTime(new DateTimeImmutable()) <= 0; |
162
|
|
|
} else { |
163
|
2 |
|
return $this->hasEndTime(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
4 |
|
public function isEnded(): bool |
171
|
|
|
{ |
172
|
4 |
|
return $this->hasEndTime() && $this->state->compareEndTime(new DateTimeImmutable()) < 0; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|