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 DateInterval; |
14
|
|
|
use DatePeriod; |
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use DateTimeInterface; |
17
|
|
|
use JsonSerializable; |
18
|
|
|
use Traversable; |
19
|
|
|
use Zee\DateRange\States\RangeState; |
20
|
|
|
use Zee\DateRange\States\UndefinedRange; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Implementation of date range value object. |
24
|
|
|
*/ |
25
|
|
|
final class DateRange implements DateRangeInterface, JsonSerializable |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var RangeState |
29
|
|
|
*/ |
30
|
|
|
private $state; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param DateTimeInterface|null $startTime |
34
|
|
|
* @param DateTimeInterface|null $endTime |
35
|
|
|
*/ |
36
|
14 |
|
public function __construct(DateTimeInterface $startTime = null, DateTimeInterface $endTime = null) |
37
|
|
|
{ |
38
|
14 |
|
$state = new UndefinedRange(); |
39
|
|
|
|
40
|
14 |
|
if (isset($startTime)) { |
41
|
9 |
|
$state = $state->setStartTime($startTime); |
42
|
|
|
} |
43
|
|
|
|
44
|
14 |
|
if (isset($endTime)) { |
45
|
9 |
|
$state = $state->setEndTime($endTime); |
46
|
|
|
} |
47
|
|
|
|
48
|
12 |
|
$this->state = $state; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns string representation of range. |
53
|
|
|
* |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
public function __toString(): string |
57
|
|
|
{ |
58
|
4 |
|
return (string) $this->state; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function __debugInfo() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
1 |
|
'startTime' => $this->state->hasStartTime() |
68
|
1 |
|
? $this->state->getStartTime() |
69
|
|
|
: null, |
70
|
1 |
|
'endTime' => $this->state->hasEndTime() |
71
|
1 |
|
? $this->state->getEndTime() |
72
|
|
|
: null, |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns value ready to be encoded as JSON. |
78
|
|
|
* |
79
|
|
|
* The ISO-8601 range format is used for times. |
80
|
|
|
* |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
4 |
|
public function jsonSerialize(): array |
84
|
|
|
{ |
85
|
4 |
|
return $this->state->jsonSerialize(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
4 |
|
public function hasStartTime(): bool |
92
|
|
|
{ |
93
|
4 |
|
return $this->state->hasStartTime(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
4 |
|
public function hasEndTime(): bool |
100
|
|
|
{ |
101
|
4 |
|
return $this->state->hasEndTime(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
9 |
|
public function getStartTime(): DateTimeInterface |
108
|
|
|
{ |
109
|
9 |
|
return $this->state->getStartTime(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
9 |
|
public function getEndTime(): DateTimeInterface |
116
|
|
|
{ |
117
|
9 |
|
return $this->state->getEndTime(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
4 |
|
public function setStartTime(DateTimeInterface $time): DateRangeInterface |
124
|
|
|
{ |
125
|
4 |
|
$clone = clone $this; |
126
|
4 |
|
$clone->state = $clone->state->setStartTime($time); |
127
|
|
|
|
128
|
4 |
|
return $clone; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
4 |
|
public function setEndTime(DateTimeInterface $time): DateRangeInterface |
135
|
|
|
{ |
136
|
4 |
|
$clone = clone $this; |
137
|
4 |
|
$clone->state = $clone->state->setEndTime($time); |
138
|
|
|
|
139
|
4 |
|
return $clone; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
4 |
|
public function isStartedAt(DateTimeInterface $time): bool |
146
|
|
|
{ |
147
|
4 |
|
return $this->hasStartTime() && $this->state->compareStartTime($time) === 0; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
4 |
|
public function isEndedAt(DateTimeInterface $time): bool |
154
|
|
|
{ |
155
|
4 |
|
return $this->hasEndTime() && $this->state->compareEndTime($time) === 0; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
4 |
|
public function isStarted(): bool |
162
|
|
|
{ |
163
|
4 |
|
if ($this->hasStartTime()) { |
164
|
2 |
|
return $this->state->compareStartTime(new DateTimeImmutable()) <= 0; |
165
|
|
|
} else { |
166
|
2 |
|
return $this->hasEndTime(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
4 |
|
public function isEnded(): bool |
174
|
|
|
{ |
175
|
4 |
|
return $this->hasEndTime() && $this->state->compareEndTime(new DateTimeImmutable()) < 0; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
1 |
|
public function getDateInterval(): DateInterval |
182
|
|
|
{ |
183
|
1 |
|
return $this->getStartTime()->diff($this->getEndTime()); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
2 |
|
public function getDatePeriod(DateInterval $interval, int $option = 0): DatePeriod |
190
|
|
|
{ |
191
|
2 |
|
return new DatePeriod($this->getStartTime(), $interval, $this->getEndTime(), $option); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
1 |
|
public function split(DateInterval $interval): Traversable |
198
|
|
|
{ |
199
|
1 |
|
$startDate = $this->getStartTime(); |
200
|
1 |
|
$endDate = $this->getEndTime(); |
201
|
1 |
|
$period = $this->getDatePeriod($interval, DatePeriod::EXCLUDE_START_DATE); |
202
|
|
|
|
203
|
1 |
|
foreach ($period as $date) { |
204
|
1 |
|
yield new DateRange($startDate, $date); |
205
|
1 |
|
$startDate = $date; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
yield new DateRange($startDate, $endDate); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|