|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GpsLab component. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Peter Gribanov <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Interval\Year; |
|
12
|
|
|
|
|
13
|
|
|
use GpsLab\Component\Interval\Exception\IncorrectIntervalException; |
|
14
|
|
|
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException; |
|
15
|
|
|
use GpsLab\Component\Interval\IntervalComparator; |
|
16
|
|
|
use GpsLab\Component\Interval\ComparableIntervalInterface; |
|
17
|
|
|
use GpsLab\Component\Interval\IntervalPointInterface; |
|
18
|
|
|
use GpsLab\Component\Interval\IntervalType; |
|
19
|
|
|
|
|
20
|
|
|
class YearInterval implements ComparableIntervalInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
const REGEXP = '/^ |
|
26
|
|
|
(?:\(|\[) # start type char |
|
27
|
|
|
\s* |
|
28
|
|
|
(?<start>\d{4}) # start point |
|
29
|
|
|
\s*,\s* # separator |
|
30
|
|
|
(?<end>\d{4}) # end point |
|
31
|
|
|
\s* |
|
32
|
|
|
(?:\)|\]) # end type char |
|
33
|
|
|
$/x'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var IntervalType |
|
37
|
|
|
*/ |
|
38
|
|
|
private $type; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var IntervalComparator |
|
42
|
|
|
*/ |
|
43
|
|
|
private $comparator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var YearIntervalPoint |
|
47
|
|
|
*/ |
|
48
|
|
|
private $start; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var YearIntervalPoint |
|
52
|
|
|
*/ |
|
53
|
|
|
private $end; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param YearIntervalPoint $start |
|
57
|
|
|
* @param YearIntervalPoint $end |
|
58
|
|
|
* @param IntervalType $type |
|
59
|
|
|
*/ |
|
60
|
2 |
|
private function __construct(YearIntervalPoint $start, YearIntervalPoint $end, IntervalType $type) |
|
61
|
|
|
{ |
|
62
|
2 |
|
if ($start->gte($end)) { |
|
63
|
|
|
throw IncorrectIntervalException::create(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
$this->type = $type; |
|
67
|
2 |
|
$this->start = $start; |
|
68
|
2 |
|
$this->end = $end; |
|
69
|
2 |
|
$this->comparator = new IntervalComparator($this); |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \DateTime $start |
|
74
|
|
|
* @param \DateTime $end |
|
75
|
|
|
* @param IntervalType $type |
|
76
|
|
|
* |
|
77
|
|
|
* @return self |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public static function create(\DateTime $start, \DateTime $end, IntervalType $type) |
|
80
|
|
|
{ |
|
81
|
2 |
|
return new self(new YearIntervalPoint($start), new YearIntervalPoint($end), $type); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \DateTime $start |
|
86
|
|
|
* @param \DateTime $end |
|
87
|
|
|
* |
|
88
|
|
|
* @return self |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function closed(\DateTime $start, \DateTime $end) |
|
91
|
|
|
{ |
|
92
|
|
|
return static::create($start, $end, IntervalType::closed()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param \DateTime $start |
|
97
|
|
|
* @param \DateTime $end |
|
98
|
|
|
* |
|
99
|
|
|
* @return self |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function halfClosed(\DateTime $start, \DateTime $end) |
|
102
|
|
|
{ |
|
103
|
|
|
return static::create($start, $end, IntervalType::halfClosed()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param \DateTime $start |
|
108
|
|
|
* @param \DateTime $end |
|
109
|
|
|
* |
|
110
|
|
|
* @return self |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function halfOpen(\DateTime $start, \DateTime $end) |
|
113
|
|
|
{ |
|
114
|
|
|
return static::create($start, $end, IntervalType::halfOpen()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param \DateTime $start |
|
119
|
|
|
* @param \DateTime $end |
|
120
|
|
|
* |
|
121
|
|
|
* @return self |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function open(\DateTime $start, \DateTime $end) |
|
124
|
|
|
{ |
|
125
|
|
|
return static::create($start, $end, IntervalType::open()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Create interval from string. |
|
130
|
|
|
* |
|
131
|
|
|
* Example formats for all interval types: |
|
132
|
|
|
* [2016, 2017] |
|
133
|
|
|
* (2015, 2016] |
|
134
|
|
|
* [2014, 2015) |
|
135
|
|
|
* (2013, 2014) |
|
136
|
|
|
* |
|
137
|
|
|
* Spaces are ignored in format. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $string |
|
140
|
|
|
* |
|
141
|
|
|
* @return self |
|
142
|
|
|
*/ |
|
143
|
2 |
|
public static function fromString($string) |
|
144
|
|
|
{ |
|
145
|
2 |
|
if (!preg_match(self::REGEXP, $string, $match)) { |
|
146
|
|
|
throw InvalidIntervalFormatException::create('[YYYY, YYYY]', $string); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
return self::create( |
|
150
|
2 |
|
(new \DateTime())->setDate($match['start'], 1, 1), |
|
151
|
2 |
|
(new \DateTime())->setDate($match['end'], 1, 1), |
|
152
|
2 |
|
IntervalType::fromString($string) |
|
153
|
2 |
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Checks if this interval is equal to the specified interval. |
|
158
|
|
|
* |
|
159
|
|
|
* @param YearInterval $interval |
|
160
|
|
|
* |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function equal(YearInterval $interval) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->comparator->equal($interval); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Does this interval contain the specified point. |
|
170
|
|
|
* |
|
171
|
|
|
* @param \DateTime $point |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function contains(\DateTime $point) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->comparator->contains(new YearIntervalPoint($point)); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Does this interval intersect the specified interval. |
|
182
|
|
|
* |
|
183
|
|
|
* @param YearInterval $interval |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function intersects(YearInterval $interval) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->comparator->intersects($interval); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Gets the intersection between this interval and another interval. |
|
194
|
|
|
* |
|
195
|
|
|
* @param YearInterval $interval |
|
196
|
|
|
* |
|
197
|
|
|
* @return self|null |
|
198
|
|
|
*/ |
|
199
|
|
|
public function intersection(YearInterval $interval) |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->comparator->intersection($interval); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Gets the covered interval between this Interval and another interval. |
|
206
|
|
|
* |
|
207
|
|
|
* @param YearInterval $interval |
|
208
|
|
|
* |
|
209
|
|
|
* @return self |
|
210
|
|
|
*/ |
|
211
|
|
|
public function cover(YearInterval $interval) |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->comparator->cover($interval); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Gets the gap between this interval and another interval. |
|
218
|
|
|
* |
|
219
|
|
|
* @param YearInterval $interval |
|
220
|
|
|
* |
|
221
|
|
|
* @return self|null |
|
222
|
|
|
*/ |
|
223
|
|
|
public function gap(YearInterval $interval) |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->comparator->gap($interval); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Does this interval abut with the interval specified. |
|
230
|
|
|
* |
|
231
|
|
|
* @param YearInterval $interval |
|
232
|
|
|
* |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
|
|
public function abuts(YearInterval $interval) |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->comparator->abuts($interval); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Joins the interval between the adjacent. |
|
242
|
|
|
* |
|
243
|
|
|
* @param YearInterval $interval |
|
244
|
|
|
* |
|
245
|
|
|
* @return self|null |
|
246
|
|
|
*/ |
|
247
|
|
|
public function join(YearInterval $interval) |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->comparator->join($interval); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Gets the union between this interval and another interval. |
|
254
|
|
|
* |
|
255
|
|
|
* @param YearInterval $interval |
|
256
|
|
|
* |
|
257
|
|
|
* @return self|null |
|
258
|
|
|
*/ |
|
259
|
|
|
public function union(YearInterval $interval) |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->comparator->union($interval); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* The point is before the interval. |
|
266
|
|
|
* |
|
267
|
|
|
* @param \DateTime $point |
|
268
|
|
|
* |
|
269
|
|
|
* @return bool |
|
270
|
|
|
*/ |
|
271
|
|
|
public function before(\DateTime $point) |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->comparator->before(new YearIntervalPoint($point)); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* The point is after the interval. |
|
278
|
|
|
* |
|
279
|
|
|
* @param \DateTime $point |
|
280
|
|
|
* |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
|
|
public function after(\DateTime $point) |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->comparator->after(new YearIntervalPoint($point)); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param \DateInterval|null $step |
|
290
|
|
|
* |
|
291
|
|
|
* @return \Generator |
|
292
|
|
|
*/ |
|
293
|
|
|
public function iterate(\DateInterval $step = null) |
|
294
|
|
|
{ |
|
295
|
|
|
$step = $step ?: new \DateInterval('P1Y'); |
|
296
|
|
|
|
|
297
|
|
|
$date = $this->start(); |
|
298
|
|
|
while ($date < $this->end()) { |
|
299
|
|
|
yield $date; |
|
300
|
|
|
$date->add($step); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param \DateInterval|null $step |
|
306
|
|
|
* |
|
307
|
|
|
* @return \DatePeriod |
|
308
|
|
|
*/ |
|
309
|
|
|
public function period(\DateInterval $step = null) |
|
310
|
|
|
{ |
|
311
|
|
|
$step = $step ?: new \DateInterval('P1Y'); |
|
312
|
|
|
|
|
313
|
|
|
return new \DatePeriod($this->start(), $step, $this->end()); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @return IntervalType |
|
318
|
|
|
*/ |
|
319
|
|
|
public function type() |
|
320
|
|
|
{ |
|
321
|
|
|
return $this->type; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @return \DateTime |
|
326
|
|
|
*/ |
|
327
|
|
|
public function start() |
|
328
|
|
|
{ |
|
329
|
|
|
return $this->start->value(); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @return \DateTime |
|
334
|
|
|
*/ |
|
335
|
|
|
public function end() |
|
336
|
|
|
{ |
|
337
|
|
|
return $this->end->value(); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @return YearIntervalPoint |
|
342
|
|
|
*/ |
|
343
|
1 |
|
public function startPoint() |
|
344
|
|
|
{ |
|
345
|
1 |
|
return $this->start; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @return YearIntervalPoint |
|
350
|
|
|
*/ |
|
351
|
1 |
|
public function endPoint() |
|
352
|
|
|
{ |
|
353
|
1 |
|
return $this->end; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Returns a copy of this Interval with the start point altered. |
|
358
|
|
|
* |
|
359
|
|
|
* @param IntervalPointInterface|YearIntervalPoint $start |
|
360
|
|
|
* |
|
361
|
|
|
* @return self |
|
362
|
|
|
*/ |
|
363
|
|
|
public function withStart(IntervalPointInterface $start) |
|
364
|
|
|
{ |
|
365
|
|
|
return new self($start, $this->end, $this->type); |
|
|
|
|
|
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Returns a copy of this Interval with the end point altered. |
|
370
|
|
|
* |
|
371
|
|
|
* @param IntervalPointInterface|YearIntervalPoint $end |
|
372
|
|
|
* |
|
373
|
|
|
* @return self |
|
374
|
|
|
*/ |
|
375
|
|
|
public function withEnd(IntervalPointInterface $end) |
|
376
|
|
|
{ |
|
377
|
|
|
return new self($this->start, $end, $this->type); |
|
|
|
|
|
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Returns a copy of this Interval with the interval type altered. |
|
382
|
|
|
* |
|
383
|
|
|
* @param IntervalType $type |
|
384
|
|
|
* |
|
385
|
|
|
* @return self |
|
386
|
|
|
*/ |
|
387
|
|
|
public function withType(IntervalType $type) |
|
388
|
|
|
{ |
|
389
|
|
|
return new self($this->start, $this->end, $type); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* @return string |
|
394
|
|
|
*/ |
|
395
|
1 |
|
public function __toString() |
|
396
|
|
|
{ |
|
397
|
1 |
|
return $this->type->getReadable($this); |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.