1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Period (https://period.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Period; |
15
|
|
|
|
16
|
|
|
use DateInterval; |
17
|
|
|
use DatePeriod; |
18
|
|
|
use DateTimeImmutable; |
19
|
|
|
use DateTimeInterface; |
20
|
|
|
use DateTimeZone; |
21
|
|
|
use JsonSerializable; |
22
|
|
|
use function array_filter; |
23
|
|
|
use function array_keys; |
24
|
|
|
use function implode; |
25
|
|
|
use function sprintf; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A immutable value object class to manipulate Time interval. |
29
|
|
|
* |
30
|
|
|
* @package League.period |
31
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
32
|
|
|
* @since 1.0.0 |
33
|
|
|
*/ |
34
|
|
|
final class Period implements JsonSerializable |
35
|
|
|
{ |
36
|
|
|
private const ISO8601_FORMAT = 'Y-m-d\TH:i:s.u\Z'; |
37
|
|
|
|
38
|
|
|
private const BOUNDARY_TYPE = [ |
39
|
|
|
self::INCLUDE_START_EXCLUDE_END => 1, |
40
|
|
|
self::INCLUDE_ALL => 1, |
41
|
|
|
self::EXCLUDE_START_INCLUDE_END => 1, |
42
|
|
|
self::EXCLUDE_ALL => 1, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
public const INCLUDE_START_EXCLUDE_END = '[)'; |
46
|
|
|
|
47
|
|
|
public const EXCLUDE_START_INCLUDE_END = '(]'; |
48
|
|
|
|
49
|
|
|
public const EXCLUDE_ALL = '()'; |
50
|
|
|
|
51
|
|
|
public const INCLUDE_ALL = '[]'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The starting datepoint. |
55
|
|
|
* |
56
|
|
|
* @var DateTimeImmutable |
57
|
|
|
*/ |
58
|
|
|
private $startDate; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The ending datepoint. |
62
|
|
|
* |
63
|
|
|
* @var DateTimeImmutable |
64
|
|
|
*/ |
65
|
|
|
private $endDate; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The boundary type. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $boundaryType; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a new instance. |
76
|
|
|
* |
77
|
|
|
* @param mixed $startDate the starting datepoint |
78
|
|
|
* @param mixed $endDate the ending datepoint |
79
|
|
|
* |
80
|
|
|
* @throws Exception If $startDate is greater than $endDate |
81
|
|
|
*/ |
82
|
921 |
|
public function __construct($startDate, $endDate, string $boundaryType = self::INCLUDE_START_EXCLUDE_END) |
83
|
|
|
{ |
84
|
921 |
|
$startDate = self::getDatepoint($startDate); |
85
|
921 |
|
$endDate = self::getDatepoint($endDate); |
86
|
909 |
|
if ($startDate > $endDate) { |
87
|
66 |
|
throw new Exception('The ending datepoint must be greater or equal to the starting datepoint'); |
88
|
|
|
} |
89
|
|
|
|
90
|
885 |
|
if (!isset(self::BOUNDARY_TYPE[$boundaryType])) { |
91
|
6 |
|
throw new Exception(sprintf( |
92
|
6 |
|
'The boundary type `%s` is invalid. The only valid values are %s', |
93
|
6 |
|
$boundaryType, |
94
|
6 |
|
'`'.implode('`, `', array_keys(self::BOUNDARY_TYPE)).'`' |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
|
98
|
882 |
|
$this->startDate = $startDate; |
99
|
882 |
|
$this->endDate = $endDate; |
100
|
882 |
|
$this->boundaryType = $boundaryType; |
101
|
882 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns a DateTimeImmutable instance. |
105
|
|
|
* |
106
|
|
|
* @param mixed $datepoint a Datepoint |
107
|
|
|
*/ |
108
|
1086 |
|
private static function getDatepoint($datepoint): DateTimeImmutable |
109
|
|
|
{ |
110
|
1086 |
|
if ($datepoint instanceof DateTimeImmutable) { |
111
|
864 |
|
return $datepoint; |
112
|
|
|
} |
113
|
|
|
|
114
|
735 |
|
return Datepoint::create($datepoint); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a DateInterval instance. |
119
|
|
|
* |
120
|
|
|
* @param mixed $duration a Duration |
121
|
|
|
*/ |
122
|
360 |
|
private static function getDuration($duration): DateInterval |
123
|
|
|
{ |
124
|
360 |
|
if ($duration instanceof DateInterval) { |
125
|
150 |
|
return $duration; |
126
|
|
|
} |
127
|
|
|
|
128
|
210 |
|
return Duration::create($duration); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/************************************************** |
132
|
|
|
* Named constructors |
133
|
|
|
**************************************************/ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
6 |
|
public static function __set_state(array $interval) |
139
|
|
|
{ |
140
|
6 |
|
return new self($interval['startDate'], $interval['endDate'], $interval['boundaryType'] ?? self::INCLUDE_START_EXCLUDE_END); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Creates new instance from a starting datepoint and a duration. |
145
|
|
|
* |
146
|
|
|
* @param mixed $startDate the starting datepoint |
147
|
|
|
* @param mixed $duration a Duration |
148
|
|
|
*/ |
149
|
132 |
|
public static function after($startDate, $duration, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
150
|
|
|
{ |
151
|
132 |
|
$startDate = self::getDatepoint($startDate); |
152
|
|
|
|
153
|
132 |
|
return new self($startDate, $startDate->add(self::getDuration($duration)), $boundaryType); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Creates new instance from a ending datepoint and a duration. |
158
|
|
|
* |
159
|
|
|
* @param mixed $endDate the ending datepoint |
160
|
|
|
* @param mixed $duration a Duration |
161
|
|
|
*/ |
162
|
27 |
|
public static function before($endDate, $duration, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
163
|
|
|
{ |
164
|
27 |
|
$endDate = self::getDatepoint($endDate); |
165
|
|
|
|
166
|
27 |
|
return new self($endDate->sub(self::getDuration($duration)), $endDate, $boundaryType); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Creates new instance where the given duration is simultaneously |
171
|
|
|
* subtracted from and added to the datepoint. |
172
|
|
|
* |
173
|
|
|
* @param mixed $datepoint a Datepoint |
174
|
|
|
* @param mixed $duration a Duration |
175
|
|
|
*/ |
176
|
24 |
|
public static function around($datepoint, $duration, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
177
|
|
|
{ |
178
|
24 |
|
$datepoint = self::getDatepoint($datepoint); |
179
|
24 |
|
$duration = self::getDuration($duration); |
180
|
|
|
|
181
|
24 |
|
return new self($datepoint->sub($duration), $datepoint->add($duration), $boundaryType); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Creates new instance from a DatePeriod. |
186
|
|
|
*/ |
187
|
12 |
|
public static function fromDatePeriod(DatePeriod $datePeriod, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
188
|
|
|
{ |
189
|
12 |
|
return new self($datePeriod->getStartDate(), $datePeriod->getEndDate(), $boundaryType); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Creates new instance for a specific year. |
194
|
|
|
*/ |
195
|
12 |
|
public static function fromYear(int $year, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
196
|
|
|
{ |
197
|
12 |
|
$startDate = (new DateTimeImmutable())->setDate($year, 1, 1)->setTime(0, 0); |
198
|
|
|
|
199
|
12 |
|
return new self($startDate, $startDate->add(new DateInterval('P1Y')), $boundaryType); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Creates new instance for a specific ISO year. |
204
|
|
|
*/ |
205
|
6 |
|
public static function fromIsoYear(int $year, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
206
|
|
|
{ |
207
|
6 |
|
return new self( |
208
|
6 |
|
(new DateTimeImmutable())->setISODate($year, 1)->setTime(0, 0), |
209
|
6 |
|
(new DateTimeImmutable())->setISODate(++$year, 1)->setTime(0, 0), |
210
|
2 |
|
$boundaryType |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Creates new instance for a specific year and semester. |
216
|
|
|
*/ |
217
|
18 |
|
public static function fromSemester(int $year, int $semester = 1, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
218
|
|
|
{ |
219
|
18 |
|
$month = (($semester - 1) * 6) + 1; |
220
|
18 |
|
$startDate = (new DateTimeImmutable())->setDate($year, $month, 1)->setTime(0, 0); |
221
|
|
|
|
222
|
18 |
|
return new self($startDate, $startDate->add(new DateInterval('P6M')), $boundaryType); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Creates new instance for a specific year and quarter. |
227
|
|
|
*/ |
228
|
18 |
|
public static function fromQuarter(int $year, int $quarter = 1, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
229
|
|
|
{ |
230
|
18 |
|
$month = (($quarter - 1) * 3) + 1; |
231
|
18 |
|
$startDate = (new DateTimeImmutable())->setDate($year, $month, 1)->setTime(0, 0); |
232
|
|
|
|
233
|
18 |
|
return new self($startDate, $startDate->add(new DateInterval('P3M')), $boundaryType); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Creates new instance for a specific year and month. |
238
|
|
|
*/ |
239
|
75 |
|
public static function fromMonth(int $year, int $month = 1, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
240
|
|
|
{ |
241
|
75 |
|
$startDate = (new DateTimeImmutable())->setDate($year, $month, 1)->setTime(0, 0); |
242
|
|
|
|
243
|
75 |
|
return new self($startDate, $startDate->add(new DateInterval('P1M')), $boundaryType); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Creates new instance for a specific ISO8601 week. |
248
|
|
|
*/ |
249
|
21 |
|
public static function fromIsoWeek(int $year, int $week = 1, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
250
|
|
|
{ |
251
|
21 |
|
$startDate = (new DateTimeImmutable())->setISODate($year, $week, 1)->setTime(0, 0); |
252
|
|
|
|
253
|
21 |
|
return new self($startDate, $startDate->add(new DateInterval('P7D')), $boundaryType); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Creates new instance for a specific year, month and day. |
258
|
|
|
*/ |
259
|
54 |
|
public static function fromDay(int $year, int $month = 1, int $day = 1, string $boundaryType = self::INCLUDE_START_EXCLUDE_END): self |
260
|
|
|
{ |
261
|
54 |
|
$startDate = (new DateTimeImmutable())->setDate($year, $month, $day)->setTime(0, 0); |
262
|
|
|
|
263
|
54 |
|
return new self($startDate, $startDate->add(new DateInterval('P1D')), $boundaryType); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/************************************************** |
267
|
|
|
* Basic getters |
268
|
|
|
**************************************************/ |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns the starting datepoint. |
272
|
|
|
*/ |
273
|
243 |
|
public function getStartDate(): DateTimeImmutable |
274
|
|
|
{ |
275
|
243 |
|
return $this->startDate; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns the ending datepoint. |
280
|
|
|
*/ |
281
|
219 |
|
public function getEndDate(): DateTimeImmutable |
282
|
|
|
{ |
283
|
219 |
|
return $this->endDate; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Returns the instance boundary type. |
288
|
|
|
*/ |
289
|
159 |
|
public function getBoundaryType(): string |
290
|
|
|
{ |
291
|
159 |
|
return $this->boundaryType; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns the instance duration as expressed in seconds. |
296
|
|
|
*/ |
297
|
36 |
|
public function getTimestampInterval(): float |
298
|
|
|
{ |
299
|
36 |
|
return $this->endDate->getTimestamp() - $this->startDate->getTimestamp(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the instance duration as a DateInterval object. |
304
|
|
|
*/ |
305
|
129 |
|
public function getDateInterval(): DateInterval |
306
|
|
|
{ |
307
|
129 |
|
return $this->startDate->diff($this->endDate); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/************************************************** |
311
|
|
|
* String representation |
312
|
|
|
**************************************************/ |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Returns the string representation as a ISO8601 interval format. |
316
|
|
|
* |
317
|
|
|
* @deprecated since version 4.10 |
318
|
|
|
* @see ::toIso8601() |
319
|
|
|
*/ |
320
|
6 |
|
public function __toString() |
321
|
|
|
{ |
322
|
6 |
|
return $this->toIso8601(self::ISO8601_FORMAT); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Returns the string representation as a ISO8601 interval format. |
327
|
|
|
* |
328
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals |
329
|
|
|
* @param ?string $format |
330
|
|
|
*/ |
331
|
18 |
|
public function toIso8601(?string $format = null): string |
332
|
|
|
{ |
333
|
18 |
|
$utc = new DateTimeZone('UTC'); |
334
|
18 |
|
$format = $format ?? self::ISO8601_FORMAT; |
335
|
|
|
|
336
|
18 |
|
$startDate = $this->startDate->setTimezone($utc)->format($format); |
337
|
18 |
|
$endDate = $this->endDate->setTimezone($utc)->format($format); |
338
|
|
|
|
339
|
18 |
|
return $startDate.'/'.$endDate; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the JSON representation of an instance. |
344
|
|
|
* |
345
|
|
|
* Based on the JSON representation of dates as |
346
|
|
|
* returned by Javascript Date.toJSON() method. |
347
|
|
|
* |
348
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON |
349
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString |
350
|
|
|
* |
351
|
|
|
* @return array<string> |
352
|
|
|
*/ |
353
|
12 |
|
public function jsonSerialize() |
354
|
|
|
{ |
355
|
12 |
|
[$startDate, $endDate] = explode('/', $this->toIso8601(), 2); |
356
|
|
|
|
357
|
12 |
|
return ['startDate' => $startDate, 'endDate' => $endDate]; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns the mathematical representation of an instance as a left close, right open interval. |
362
|
|
|
* |
363
|
|
|
* @see https://en.wikipedia.org/wiki/Interval_(mathematics)#Notations_for_intervals |
364
|
|
|
* @see https://php.net/manual/en/function.date.php |
365
|
|
|
* @see https://www.postgresql.org/docs/9.3/static/rangetypes.html |
366
|
|
|
* |
367
|
|
|
* @param string $format the format of the outputted date string |
368
|
|
|
*/ |
369
|
24 |
|
public function format(string $format): string |
370
|
|
|
{ |
371
|
24 |
|
return $this->boundaryType[0] |
372
|
24 |
|
.$this->startDate->format($format) |
373
|
24 |
|
.', ' |
374
|
24 |
|
.$this->endDate->format($format) |
375
|
24 |
|
.$this->boundaryType[1]; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/************************************************** |
379
|
|
|
* Boundary related methods |
380
|
|
|
**************************************************/ |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Tells whether the start datepoint is included in the boundary. |
384
|
|
|
*/ |
385
|
12 |
|
public function isStartIncluded(): bool |
386
|
|
|
{ |
387
|
12 |
|
return '[' === $this->boundaryType[0]; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Tells whether the start datepoint is excluded from the boundary. |
392
|
|
|
*/ |
393
|
81 |
|
public function isStartExcluded(): bool |
394
|
|
|
{ |
395
|
81 |
|
return '(' === $this->boundaryType[0]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Tells whether the end datepoint is included in the boundary. |
400
|
|
|
*/ |
401
|
12 |
|
public function isEndIncluded(): bool |
402
|
|
|
{ |
403
|
12 |
|
return ']' === $this->boundaryType[1]; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Tells whether the end datepoint is excluded from the boundary. |
408
|
|
|
*/ |
409
|
81 |
|
public function isEndExcluded(): bool |
410
|
|
|
{ |
411
|
81 |
|
return ')' === $this->boundaryType[1]; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/************************************************** |
415
|
|
|
* Duration comparison methods |
416
|
|
|
**************************************************/ |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Compares two instances according to their duration. |
420
|
|
|
* |
421
|
|
|
* Returns: |
422
|
|
|
* <ul> |
423
|
|
|
* <li> -1 if the current Interval is lesser than the submitted Interval object</li> |
424
|
|
|
* <li> 1 if the current Interval is greater than the submitted Interval object</li> |
425
|
|
|
* <li> 0 if both Interval objects have the same duration</li> |
426
|
|
|
* </ul> |
427
|
|
|
*/ |
428
|
60 |
|
public function durationCompare(self $interval): int |
429
|
|
|
{ |
430
|
60 |
|
return $this->startDate->add($this->getDateInterval()) |
431
|
60 |
|
<=> $this->startDate->add($interval->getDateInterval()); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Tells whether the current instance duration is equal to the submitted one. |
436
|
|
|
*/ |
437
|
6 |
|
public function durationEquals(self $interval): bool |
438
|
|
|
{ |
439
|
6 |
|
return 0 === $this->durationCompare($interval); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Tells whether the current instance duration is greater than the submitted one. |
444
|
|
|
*/ |
445
|
18 |
|
public function durationGreaterThan(self $interval): bool |
446
|
|
|
{ |
447
|
18 |
|
return 1 === $this->durationCompare($interval); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Tells whether the current instance duration is less than the submitted one. |
452
|
|
|
*/ |
453
|
12 |
|
public function durationLessThan(self $interval): bool |
454
|
|
|
{ |
455
|
12 |
|
return -1 === $this->durationCompare($interval); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/************************************************** |
459
|
|
|
* Relation methods |
460
|
|
|
**************************************************/ |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Tells whether an instance is entirely before the specified index. |
464
|
|
|
* |
465
|
|
|
* The index can be a DateTimeInterface object or another Period object. |
466
|
|
|
* |
467
|
|
|
* [--------------------) |
468
|
|
|
* [--------------------) |
469
|
|
|
* |
470
|
|
|
* @param mixed $index a datepoint or a Period object |
471
|
|
|
*/ |
472
|
90 |
|
public function isBefore($index): bool |
473
|
|
|
{ |
474
|
90 |
|
if ($index instanceof self) { |
475
|
48 |
|
return $this->endDate < $index->startDate |
476
|
48 |
|
|| ($this->endDate == $index->startDate && $this->boundaryType[1] !== $index->boundaryType[0]); |
477
|
|
|
} |
478
|
|
|
|
479
|
42 |
|
$datepoint = self::getDatepoint($index); |
480
|
42 |
|
return $this->endDate < $datepoint |
481
|
42 |
|
|| ($this->endDate == $datepoint && ')' === $this->boundaryType[1]); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Tells whether the current instance end date meets the interval start date. |
486
|
|
|
* |
487
|
|
|
* [--------------------) |
488
|
|
|
* [--------------------) |
489
|
|
|
*/ |
490
|
309 |
|
public function bordersOnStart(self $interval): bool |
491
|
|
|
{ |
492
|
309 |
|
return $this->endDate == $interval->startDate |
493
|
309 |
|
&& '][' !== $this->boundaryType[1].$interval->boundaryType[0]; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Tells whether two intervals share the same start datepoint |
498
|
|
|
* and the same starting boundary type. |
499
|
|
|
* |
500
|
|
|
* [----------) |
501
|
|
|
* [--------------------) |
502
|
|
|
* |
503
|
|
|
* or |
504
|
|
|
* |
505
|
|
|
* [--------------------) |
506
|
|
|
* [---------) |
507
|
|
|
* |
508
|
|
|
* @param mixed $index a datepoint or a Period object |
509
|
|
|
*/ |
510
|
27 |
|
public function isStartedBy($index): bool |
511
|
|
|
{ |
512
|
27 |
|
if ($index instanceof self) { |
513
|
15 |
|
return $this->startDate == $index->startDate |
514
|
15 |
|
&& $this->boundaryType[0] === $index->boundaryType[0]; |
515
|
|
|
} |
516
|
|
|
|
517
|
12 |
|
$index = self::getDatepoint($index); |
518
|
|
|
|
519
|
12 |
|
return $index == $this->startDate && '[' === $this->boundaryType[0]; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Tells whether an instance is fully contained in the specified interval. |
524
|
|
|
* |
525
|
|
|
* [----------) |
526
|
|
|
* [--------------------) |
527
|
|
|
*/ |
528
|
39 |
|
public function isDuring(self $interval): bool |
529
|
|
|
{ |
530
|
39 |
|
return $interval->containsInterval($this); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Tells whether an instance fully contains the specified index. |
535
|
|
|
* |
536
|
|
|
* The index can be a DateTimeInterface object or another Period object. |
537
|
|
|
* |
538
|
|
|
* @param mixed $index a datepoint or a Period object |
539
|
|
|
*/ |
540
|
144 |
|
public function contains($index): bool |
541
|
|
|
{ |
542
|
144 |
|
if ($index instanceof self) { |
543
|
66 |
|
return $this->containsInterval($index); |
544
|
|
|
} |
545
|
|
|
|
546
|
78 |
|
return $this->containsDatepoint(self::getDatepoint($index), $this->boundaryType); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Tells whether an instance fully contains another instance. |
551
|
|
|
* |
552
|
|
|
* [--------------------) |
553
|
|
|
* [----------) |
554
|
|
|
*/ |
555
|
66 |
|
private function containsInterval(self $interval): bool |
556
|
|
|
{ |
557
|
66 |
|
if ($this->startDate < $interval->startDate && $this->endDate > $interval->endDate) { |
558
|
18 |
|
return true; |
559
|
|
|
} |
560
|
|
|
|
561
|
63 |
|
if ($this->startDate == $interval->startDate && $this->endDate == $interval->endDate) { |
562
|
21 |
|
return $this->boundaryType === $interval->boundaryType || '[]' === $this->boundaryType; |
563
|
|
|
} |
564
|
|
|
|
565
|
42 |
|
if ($this->startDate == $interval->startDate) { |
566
|
12 |
|
return ($this->boundaryType[0] === $interval->boundaryType[0] || '[' === $this->boundaryType[0]) |
567
|
12 |
|
&& $this->containsDatepoint($this->startDate->add($interval->getDateInterval()), $this->boundaryType); |
568
|
|
|
} |
569
|
|
|
|
570
|
30 |
|
if ($this->endDate == $interval->endDate) { |
571
|
18 |
|
return ($this->boundaryType[1] === $interval->boundaryType[1] || ']' === $this->boundaryType[1]) |
572
|
18 |
|
&& $this->containsDatepoint($this->endDate->sub($interval->getDateInterval()), $this->boundaryType); |
|
|
|
|
573
|
|
|
} |
574
|
|
|
|
575
|
12 |
|
return false; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Tells whether an instance contains a datepoint. |
580
|
|
|
* |
581
|
|
|
* [------|------------) |
582
|
|
|
*/ |
583
|
108 |
|
private function containsDatepoint(DateTimeInterface $datepoint, string $boundaryType): bool |
584
|
|
|
{ |
585
|
|
|
switch ($boundaryType) { |
586
|
108 |
|
case self::EXCLUDE_ALL: |
587
|
9 |
|
return $datepoint > $this->startDate && $datepoint < $this->endDate; |
588
|
99 |
|
case self::INCLUDE_ALL: |
589
|
3 |
|
return $datepoint >= $this->startDate && $datepoint <= $this->endDate; |
590
|
96 |
|
case self::EXCLUDE_START_INCLUDE_END: |
591
|
9 |
|
return $datepoint > $this->startDate && $datepoint <= $this->endDate; |
592
|
87 |
|
case self::INCLUDE_START_EXCLUDE_END: |
593
|
|
|
default: |
594
|
87 |
|
return $datepoint >= $this->startDate && $datepoint < $this->endDate; |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Tells whether two intervals share the same datepoints. |
600
|
|
|
* |
601
|
|
|
* [--------------------) |
602
|
|
|
* [--------------------) |
603
|
|
|
*/ |
604
|
288 |
|
public function equals(self $interval): bool |
605
|
|
|
{ |
606
|
288 |
|
return $this->startDate == $interval->startDate |
607
|
288 |
|
&& $this->endDate == $interval->endDate |
608
|
288 |
|
&& $this->boundaryType === $interval->boundaryType; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Tells whether two intervals share the same end datepoint |
613
|
|
|
* and the same ending boundary type. |
614
|
|
|
* |
615
|
|
|
* [----------) |
616
|
|
|
* [--------------------) |
617
|
|
|
* |
618
|
|
|
* or |
619
|
|
|
* |
620
|
|
|
* [--------------------) |
621
|
|
|
* [---------) |
622
|
|
|
* |
623
|
|
|
* @param mixed $index a datepoint or a Period object |
624
|
|
|
*/ |
625
|
24 |
|
public function isEndedBy($index): bool |
626
|
|
|
{ |
627
|
24 |
|
if ($index instanceof self) { |
628
|
12 |
|
return $this->endDate == $index->endDate |
629
|
12 |
|
&& $this->boundaryType[1] === $index->boundaryType[1]; |
630
|
|
|
} |
631
|
|
|
|
632
|
12 |
|
$index = self::getDatepoint($index); |
633
|
|
|
|
634
|
12 |
|
return $index == $this->endDate && ']' === $this->boundaryType[1]; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Tells whether the current instance start date meets the interval end date. |
639
|
|
|
* |
640
|
|
|
* [--------------------) |
641
|
|
|
* [--------------------) |
642
|
|
|
*/ |
643
|
291 |
|
public function bordersOnEnd(self $interval): bool |
644
|
|
|
{ |
645
|
291 |
|
return $interval->bordersOnStart($this); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Tells whether an interval is entirely after the specified index. |
650
|
|
|
* The index can be a DateTimeInterface object or another Period object. |
651
|
|
|
* |
652
|
|
|
* [--------------------) |
653
|
|
|
* [--------------------) |
654
|
|
|
* |
655
|
|
|
* @param mixed $index a datepoint or a Period object |
656
|
|
|
*/ |
657
|
54 |
|
public function isAfter($index): bool |
658
|
|
|
{ |
659
|
54 |
|
if ($index instanceof self) { |
660
|
24 |
|
return $index->isBefore($this); |
661
|
|
|
} |
662
|
|
|
|
663
|
30 |
|
$datepoint = self::getDatepoint($index); |
664
|
30 |
|
return $this->startDate > $datepoint |
665
|
30 |
|
|| ($this->startDate == $datepoint && '(' === $this->boundaryType[0]); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Tells whether two intervals abuts. |
670
|
|
|
* |
671
|
|
|
* [--------------------) |
672
|
|
|
* [--------------------) |
673
|
|
|
* or |
674
|
|
|
* [--------------------) |
675
|
|
|
* [--------------------) |
676
|
|
|
*/ |
677
|
309 |
|
public function abuts(self $interval): bool |
678
|
|
|
{ |
679
|
309 |
|
return $this->bordersOnStart($interval) || $this->bordersOnEnd($interval); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Tells whether two intervals overlaps. |
684
|
|
|
* |
685
|
|
|
* [--------------------) |
686
|
|
|
* [--------------------) |
687
|
|
|
*/ |
688
|
291 |
|
public function overlaps(self $interval): bool |
689
|
|
|
{ |
690
|
291 |
|
return !$this->abuts($interval) |
691
|
291 |
|
&& $this->startDate < $interval->endDate |
692
|
291 |
|
&& $this->endDate > $interval->startDate; |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/************************************************** |
696
|
|
|
* Manipulating instance duration |
697
|
|
|
**************************************************/ |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* Returns the difference between two instances expressed in seconds. |
701
|
|
|
*/ |
702
|
6 |
|
public function timestampIntervalDiff(self $interval): float |
703
|
|
|
{ |
704
|
6 |
|
return $this->getTimestampInterval() - $interval->getTimestampInterval(); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Returns the difference between two instances expressed with a DateInterval object. |
709
|
|
|
*/ |
710
|
12 |
|
public function dateIntervalDiff(self $interval): DateInterval |
711
|
|
|
{ |
712
|
12 |
|
return $this->endDate->diff($this->startDate->add($interval->getDateInterval())); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Allows iteration over a set of dates and times, |
717
|
|
|
* recurring at regular intervals, over the instance. |
718
|
|
|
* |
719
|
|
|
* @see http://php.net/manual/en/dateperiod.construct.php |
720
|
|
|
* |
721
|
|
|
* @param mixed $duration a Duration |
722
|
|
|
*/ |
723
|
54 |
|
public function getDatePeriod($duration, int $option = 0): DatePeriod |
724
|
|
|
{ |
725
|
54 |
|
return new DatePeriod($this->startDate, self::getDuration($duration), $this->endDate, $option); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Allows iteration over a set of dates and times, |
730
|
|
|
* recurring at regular intervals, over the instance backwards starting from |
731
|
|
|
* the instance ending datepoint. |
732
|
|
|
* |
733
|
|
|
* @param mixed $duration a Duration |
734
|
|
|
*/ |
735
|
24 |
|
public function getDatePeriodBackwards($duration, int $option = 0): iterable |
736
|
|
|
{ |
737
|
24 |
|
$duration = self::getDuration($duration); |
738
|
24 |
|
$date = $this->endDate; |
739
|
24 |
|
if ((bool) ($option & DatePeriod::EXCLUDE_START_DATE)) { |
740
|
12 |
|
$date = $this->endDate->sub($duration); |
741
|
|
|
} |
742
|
|
|
|
743
|
24 |
|
while ($date > $this->startDate) { |
744
|
24 |
|
yield $date; |
745
|
24 |
|
$date = $date->sub($duration); |
746
|
|
|
} |
747
|
24 |
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Allows splitting an instance in smaller Period objects according to a given interval. |
751
|
|
|
* |
752
|
|
|
* The returned iterable Interval set is ordered so that: |
753
|
|
|
* <ul> |
754
|
|
|
* <li>The first returned object MUST share the starting datepoint of the parent object.</li> |
755
|
|
|
* <li>The last returned object MUST share the ending datepoint of the parent object.</li> |
756
|
|
|
* <li>The last returned object MUST have a duration equal or lesser than the submitted interval.</li> |
757
|
|
|
* <li>All returned objects except for the first one MUST start immediately after the previously returned object</li> |
758
|
|
|
* </ul> |
759
|
|
|
* |
760
|
|
|
* @param mixed $duration a Duration |
761
|
|
|
* |
762
|
|
|
* @return iterable<Period> |
763
|
|
|
*/ |
764
|
30 |
|
public function split($duration): iterable |
765
|
|
|
{ |
766
|
30 |
|
$duration = self::getDuration($duration); |
767
|
30 |
|
foreach ($this->getDatePeriod($duration) as $startDate) { |
768
|
30 |
|
$endDate = $startDate->add($duration); |
769
|
30 |
|
if ($endDate > $this->endDate) { |
770
|
12 |
|
$endDate = $this->endDate; |
771
|
|
|
} |
772
|
|
|
|
773
|
30 |
|
yield new self($startDate, $endDate, $this->boundaryType); |
774
|
|
|
} |
775
|
30 |
|
} |
776
|
|
|
|
777
|
|
|
/** |
778
|
|
|
* Allows splitting an instance in smaller Period objects according to a given interval. |
779
|
|
|
* |
780
|
|
|
* The returned iterable Period set is ordered so that: |
781
|
|
|
* <ul> |
782
|
|
|
* <li>The first returned object MUST share the ending datepoint of the parent object.</li> |
783
|
|
|
* <li>The last returned object MUST share the starting datepoint of the parent object.</li> |
784
|
|
|
* <li>The last returned object MUST have a duration equal or lesser than the submitted interval.</li> |
785
|
|
|
* <li>All returned objects except for the first one MUST end immediately before the previously returned object</li> |
786
|
|
|
* </ul> |
787
|
|
|
* |
788
|
|
|
* @param mixed $duration a Duration |
789
|
|
|
* |
790
|
|
|
* @return iterable<Period> |
791
|
|
|
*/ |
792
|
18 |
|
public function splitBackwards($duration): iterable |
793
|
|
|
{ |
794
|
18 |
|
$endDate = $this->endDate; |
795
|
18 |
|
$duration = self::getDuration($duration); |
796
|
|
|
do { |
797
|
18 |
|
$startDate = $endDate->sub($duration); |
798
|
18 |
|
if ($startDate < $this->startDate) { |
799
|
6 |
|
$startDate = $this->startDate; |
800
|
|
|
} |
801
|
18 |
|
yield new self($startDate, $endDate, $this->boundaryType); |
802
|
|
|
|
803
|
18 |
|
$endDate = $startDate; |
804
|
18 |
|
} while ($endDate > $this->startDate); |
805
|
18 |
|
} |
806
|
|
|
|
807
|
|
|
/************************************************** |
808
|
|
|
* Manipulation instance endpoints and boundaries |
809
|
|
|
**************************************************/ |
810
|
|
|
|
811
|
|
|
/** |
812
|
|
|
* Returns the computed intersection between two instances as a new instance. |
813
|
|
|
* |
814
|
|
|
* [--------------------) |
815
|
|
|
* ∩ |
816
|
|
|
* [----------) |
817
|
|
|
* = |
818
|
|
|
* [----) |
819
|
|
|
* |
820
|
|
|
* @throws Exception If both objects do not overlaps |
821
|
|
|
*/ |
822
|
153 |
|
public function intersect(self $interval): self |
823
|
|
|
{ |
824
|
153 |
|
if (!$this->overlaps($interval)) { |
825
|
12 |
|
throw new Exception('Both '.self::class.' objects should overlaps'); |
826
|
|
|
} |
827
|
|
|
|
828
|
141 |
|
$startDate = $this->startDate; |
829
|
141 |
|
$endDate = $this->endDate; |
830
|
141 |
|
$boundaryType = $this->boundaryType; |
831
|
141 |
|
if ($interval->startDate > $this->startDate) { |
832
|
132 |
|
$boundaryType[0] = $interval->boundaryType[0]; |
833
|
132 |
|
$startDate = $interval->startDate; |
834
|
|
|
} |
835
|
|
|
|
836
|
141 |
|
if ($interval->endDate < $this->endDate) { |
837
|
33 |
|
$boundaryType[1] = $interval->boundaryType[1]; |
838
|
33 |
|
$endDate = $interval->endDate; |
839
|
|
|
} |
840
|
|
|
|
841
|
141 |
|
$intersect = new self($startDate, $endDate, $boundaryType); |
842
|
141 |
|
if ($intersect->equals($this)) { |
843
|
21 |
|
return $this; |
844
|
|
|
} |
845
|
|
|
|
846
|
138 |
|
return $intersect; |
847
|
|
|
} |
848
|
|
|
|
849
|
|
|
/** |
850
|
|
|
* Returns the computed difference between two overlapping instances as |
851
|
|
|
* an array containing Period objects or the null value. |
852
|
|
|
* |
853
|
|
|
* The array will always contains 2 elements: |
854
|
|
|
* |
855
|
|
|
* <ul> |
856
|
|
|
* <li>an NULL filled array if both objects have the same datepoints</li> |
857
|
|
|
* <li>one Period object and NULL if both objects share one datepoint</li> |
858
|
|
|
* <li>two Period objects if both objects share no datepoint</li> |
859
|
|
|
* </ul> |
860
|
|
|
* |
861
|
|
|
* [--------------------) |
862
|
|
|
* \ |
863
|
|
|
* [-----------) |
864
|
|
|
* = |
865
|
|
|
* [--------------) + [-----) |
866
|
|
|
* |
867
|
|
|
* @return array<null|Period> |
868
|
|
|
*/ |
869
|
99 |
|
public function diff(self $interval): array |
870
|
|
|
{ |
871
|
99 |
|
if ($interval->equals($this)) { |
872
|
12 |
|
return [null, null]; |
873
|
|
|
} |
874
|
|
|
|
875
|
87 |
|
$intersect = $this->intersect($interval); |
876
|
81 |
|
$merge = $this->merge($interval); |
877
|
81 |
|
if ($merge->startDate == $intersect->startDate) { |
878
|
9 |
|
$first = ')' === $intersect->boundaryType[1] ? '[' : '('; |
879
|
9 |
|
$boundary = $first.$merge->boundaryType[1]; |
880
|
|
|
|
881
|
9 |
|
return [$merge->startingOn($intersect->endDate)->withBoundaryType($boundary), null]; |
882
|
|
|
} |
883
|
|
|
|
884
|
75 |
|
if ($merge->endDate == $intersect->endDate) { |
885
|
6 |
|
$last = '(' === $intersect->boundaryType[0] ? ']' : ')'; |
886
|
6 |
|
$boundary = $merge->boundaryType[0].$last; |
887
|
|
|
|
888
|
6 |
|
return [$merge->endingOn($intersect->startDate)->withBoundaryType($boundary), null]; |
889
|
|
|
} |
890
|
|
|
|
891
|
69 |
|
$last = '(' === $intersect->boundaryType[0] ? ']' : ')'; |
892
|
69 |
|
$lastBoundary = $merge->boundaryType[0].$last; |
893
|
|
|
|
894
|
69 |
|
$first = ')' === $intersect->boundaryType[1] ? '[' : '('; |
895
|
69 |
|
$firstBoundary = $first.$merge->boundaryType[1]; |
896
|
|
|
|
897
|
|
|
return [ |
898
|
69 |
|
$merge->endingOn($intersect->startDate)->withBoundaryType($lastBoundary), |
899
|
69 |
|
$merge->startingOn($intersect->endDate)->withBoundaryType($firstBoundary), |
900
|
|
|
]; |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
/** |
904
|
|
|
* DEPRECATION WARNING! This method will be removed in the next major point release. |
905
|
|
|
* |
906
|
|
|
* @deprecated since version 4.9.0 |
907
|
|
|
* @see ::subtract |
908
|
|
|
*/ |
909
|
3 |
|
public function substract(self $interval): Sequence |
910
|
|
|
{ |
911
|
3 |
|
return $this->subtract($interval); |
912
|
|
|
} |
913
|
|
|
|
914
|
|
|
/** |
915
|
|
|
* Returns the difference set operation between two intervals as a Sequence. |
916
|
|
|
* The Sequence can contain from 0 to 2 Periods depending on the result of |
917
|
|
|
* the operation. |
918
|
|
|
* |
919
|
|
|
* [--------------------) |
920
|
|
|
* - |
921
|
|
|
* [-----------) |
922
|
|
|
* = |
923
|
|
|
* [--------------) |
924
|
|
|
*/ |
925
|
24 |
|
public function subtract(self $interval): Sequence |
926
|
|
|
{ |
927
|
24 |
|
if (!$this->overlaps($interval)) { |
928
|
9 |
|
return new Sequence($this); |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
$filter = function ($item): bool { |
932
|
18 |
|
return null !== $item && $this->overlaps($item); |
933
|
18 |
|
}; |
934
|
|
|
|
935
|
18 |
|
return new Sequence(...array_filter($this->diff($interval), $filter)); |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* Returns the computed gap between two instances as a new instance. |
940
|
|
|
* |
941
|
|
|
* [--------------------) |
942
|
|
|
* + |
943
|
|
|
* [----------) |
944
|
|
|
* = |
945
|
|
|
* [---) |
946
|
|
|
* |
947
|
|
|
* @throws Exception If both instance overlaps |
948
|
|
|
*/ |
949
|
84 |
|
public function gap(self $interval): self |
950
|
|
|
{ |
951
|
84 |
|
if ($this->overlaps($interval)) { |
952
|
18 |
|
throw new Exception('Both '.self::class.' objects must not overlaps'); |
953
|
|
|
} |
954
|
|
|
|
955
|
66 |
|
$boundaryType = $this->isEndExcluded() ? '[' : '('; |
956
|
66 |
|
$boundaryType .= $interval->isStartExcluded() ? ']' : ')'; |
957
|
66 |
|
if ($interval->startDate > $this->startDate) { |
958
|
66 |
|
return new self($this->endDate, $interval->startDate, $boundaryType); |
959
|
|
|
} |
960
|
|
|
|
961
|
6 |
|
return new self($interval->endDate, $this->startDate, $this->boundaryType); |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* Merges one or more instances to return a new instance. |
966
|
|
|
* The resulting instance represents the largest duration possible. |
967
|
|
|
* |
968
|
|
|
* This method MUST retain the state of the current instance, and return |
969
|
|
|
* an instance that contains the specified new datepoints. |
970
|
|
|
* |
971
|
|
|
* [--------------------) |
972
|
|
|
* + |
973
|
|
|
* [----------) |
974
|
|
|
* = |
975
|
|
|
* [--------------------------) |
976
|
|
|
* |
977
|
|
|
* |
978
|
|
|
* @param Period ...$intervals |
979
|
|
|
*/ |
980
|
132 |
|
public function merge(self ...$intervals): self |
981
|
|
|
{ |
982
|
132 |
|
$carry = $this; |
983
|
132 |
|
foreach ($intervals as $period) { |
984
|
126 |
|
if ($carry->startDate > $period->startDate) { |
985
|
33 |
|
$carry = new self( |
986
|
33 |
|
$period->startDate, |
987
|
33 |
|
$carry->endDate, |
988
|
33 |
|
$period->boundaryType[0].$carry->boundaryType[1] |
989
|
|
|
); |
990
|
|
|
} |
991
|
|
|
|
992
|
126 |
|
if ($carry->endDate < $period->endDate) { |
993
|
108 |
|
$carry = new self( |
994
|
108 |
|
$carry->startDate, |
995
|
108 |
|
$period->endDate, |
996
|
108 |
|
$carry->boundaryType[0].$period->boundaryType[1] |
997
|
|
|
); |
998
|
|
|
} |
999
|
|
|
} |
1000
|
|
|
|
1001
|
132 |
|
return $carry; |
1002
|
|
|
} |
1003
|
|
|
|
1004
|
|
|
|
1005
|
|
|
/************************************************** |
1006
|
|
|
* Mutation methods |
1007
|
|
|
**************************************************/ |
1008
|
|
|
|
1009
|
|
|
/** |
1010
|
|
|
* Returns an instance with the specified starting datepoint. |
1011
|
|
|
* |
1012
|
|
|
* This method MUST retain the state of the current instance, and return |
1013
|
|
|
* an instance that contains the specified starting datepoint. |
1014
|
|
|
* |
1015
|
|
|
* @param mixed $startDate the new starting datepoint |
1016
|
|
|
*/ |
1017
|
120 |
|
public function startingOn($startDate): self |
1018
|
|
|
{ |
1019
|
120 |
|
$startDate = self::getDatepoint($startDate); |
1020
|
120 |
|
if ($startDate == $this->startDate) { |
1021
|
6 |
|
return $this; |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
120 |
|
return new self($startDate, $this->endDate, $this->boundaryType); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
/** |
1028
|
|
|
* Returns an instance with the specified ending datepoint. |
1029
|
|
|
* |
1030
|
|
|
* This method MUST retain the state of the current instance, and return |
1031
|
|
|
* an instance that contains the specified ending datepoint. |
1032
|
|
|
* |
1033
|
|
|
* @param mixed $endDate the new ending datepoint |
1034
|
|
|
*/ |
1035
|
120 |
|
public function endingOn($endDate): self |
1036
|
|
|
{ |
1037
|
120 |
|
$endDate = self::getDatepoint($endDate); |
1038
|
120 |
|
if ($endDate == $this->endDate) { |
1039
|
6 |
|
return $this; |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
120 |
|
return new self($this->startDate, $endDate, $this->boundaryType); |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
|
|
/** |
1046
|
|
|
* Returns an instance with the specified boundary type. |
1047
|
|
|
* |
1048
|
|
|
* This method MUST retain the state of the current instance, and return |
1049
|
|
|
* an instance with the specified range type. |
1050
|
|
|
*/ |
1051
|
87 |
|
public function withBoundaryType(string $boundaryType): self |
1052
|
|
|
{ |
1053
|
87 |
|
if ($boundaryType === $this->boundaryType) { |
1054
|
72 |
|
return $this; |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
45 |
|
return new self($this->startDate, $this->endDate, $boundaryType); |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
/** |
1061
|
|
|
* Returns a new instance with a new ending datepoint. |
1062
|
|
|
* |
1063
|
|
|
* This method MUST retain the state of the current instance, and return |
1064
|
|
|
* an instance that contains the specified ending datepoint. |
1065
|
|
|
* |
1066
|
|
|
* @param mixed $duration a Duration |
1067
|
|
|
*/ |
1068
|
12 |
|
public function withDurationAfterStart($duration): self |
1069
|
|
|
{ |
1070
|
12 |
|
return $this->endingOn($this->startDate->add(self::getDuration($duration))); |
1071
|
|
|
} |
1072
|
|
|
|
1073
|
|
|
/** |
1074
|
|
|
* Returns a new instance with a new starting datepoint. |
1075
|
|
|
* |
1076
|
|
|
* This method MUST retain the state of the current instance, and return |
1077
|
|
|
* an instance that contains the specified starting datepoint. |
1078
|
|
|
* |
1079
|
|
|
* @param mixed $duration a Duration |
1080
|
|
|
*/ |
1081
|
12 |
|
public function withDurationBeforeEnd($duration): self |
1082
|
|
|
{ |
1083
|
12 |
|
return $this->startingOn($this->endDate->sub(self::getDuration($duration))); |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
/** |
1087
|
|
|
* Returns a new instance with a new starting datepoint |
1088
|
|
|
* moved forward or backward by the given interval. |
1089
|
|
|
* |
1090
|
|
|
* This method MUST retain the state of the current instance, and return |
1091
|
|
|
* an instance that contains the specified starting datepoint. |
1092
|
|
|
* |
1093
|
|
|
* @param mixed $duration a Duration |
1094
|
|
|
*/ |
1095
|
18 |
|
public function moveStartDate($duration): self |
1096
|
|
|
{ |
1097
|
18 |
|
return $this->startingOn($this->startDate->add(self::getDuration($duration))); |
1098
|
|
|
} |
1099
|
|
|
|
1100
|
|
|
/** |
1101
|
|
|
* Returns a new instance with a new ending datepoint |
1102
|
|
|
* moved forward or backward by the given interval. |
1103
|
|
|
* |
1104
|
|
|
* This method MUST retain the state of the current instance, and return |
1105
|
|
|
* an instance that contains the specified ending datepoint. |
1106
|
|
|
* |
1107
|
|
|
* @param mixed $duration a Duration |
1108
|
|
|
*/ |
1109
|
15 |
|
public function moveEndDate($duration): self |
1110
|
|
|
{ |
1111
|
15 |
|
return $this->endingOn($this->endDate->add(self::getDuration($duration))); |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
/** |
1115
|
|
|
* Returns a new instance where the datepoints |
1116
|
|
|
* are moved forwards or backward simultaneously by the given DateInterval. |
1117
|
|
|
* |
1118
|
|
|
* This method MUST retain the state of the current instance, and return |
1119
|
|
|
* an instance that contains the specified new datepoints. |
1120
|
|
|
* |
1121
|
|
|
* @param mixed $duration a Duration |
1122
|
|
|
*/ |
1123
|
24 |
|
public function move($duration): self |
1124
|
|
|
{ |
1125
|
24 |
|
$duration = self::getDuration($duration); |
1126
|
24 |
|
$interval = new self($this->startDate->add($duration), $this->endDate->add($duration), $this->boundaryType); |
1127
|
24 |
|
if ($this->equals($interval)) { |
1128
|
6 |
|
return $this; |
1129
|
|
|
} |
1130
|
|
|
|
1131
|
24 |
|
return $interval; |
1132
|
|
|
} |
1133
|
|
|
|
1134
|
|
|
/** |
1135
|
|
|
* Returns an instance where the given DateInterval is simultaneously |
1136
|
|
|
* subtracted from the starting datepoint and added to the ending datepoint. |
1137
|
|
|
* |
1138
|
|
|
* Depending on the duration value, the resulting instance duration will be expanded or shrinked. |
1139
|
|
|
* |
1140
|
|
|
* This method MUST retain the state of the current instance, and return |
1141
|
|
|
* an instance that contains the specified new datepoints. |
1142
|
|
|
* |
1143
|
|
|
* @param mixed $duration a Duration |
1144
|
|
|
*/ |
1145
|
24 |
|
public function expand($duration): self |
1146
|
|
|
{ |
1147
|
24 |
|
$duration = self::getDuration($duration); |
1148
|
24 |
|
$interval = new self($this->startDate->sub($duration), $this->endDate->add($duration), $this->boundaryType); |
1149
|
18 |
|
if ($this->equals($interval)) { |
1150
|
6 |
|
return $this; |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
12 |
|
return $interval; |
1154
|
|
|
} |
1155
|
|
|
} |
1156
|
|
|
|