1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Period; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use Countable; |
7
|
|
|
use ArrayAccess; |
8
|
|
|
|
9
|
|
|
class PeriodCollection implements ArrayAccess, Iterator, Countable |
10
|
|
|
{ |
11
|
|
|
use IterableImplementation; |
12
|
|
|
|
13
|
|
|
/** @var \Spatie\Period\Period[] */ |
14
|
|
|
protected $periods; |
15
|
|
|
|
16
|
|
|
public function __construct(Period ...$periods) |
17
|
|
|
{ |
18
|
|
|
$this->periods = $periods; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function current(): Period |
22
|
|
|
{ |
23
|
|
|
return $this->periods[$this->position]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function overlapSingle(PeriodCollection $periodCollection): PeriodCollection |
27
|
|
|
{ |
28
|
|
|
$overlaps = new PeriodCollection(); |
29
|
|
|
|
30
|
|
|
foreach ($this as $period) { |
31
|
|
|
foreach ($periodCollection as $otherPeriod) { |
32
|
|
|
if (! $period->overlapSingle($otherPeriod)) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$overlaps[] = $period->overlapSingle($otherPeriod); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $overlaps; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function overlap(PeriodCollection ...$periodCollections): PeriodCollection |
44
|
|
|
{ |
45
|
|
|
$overlap = clone $this; |
46
|
|
|
|
47
|
|
|
foreach ($periodCollections as $periodCollection) { |
48
|
|
|
$overlap = $overlap->overlapSingle($periodCollection); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $overlap; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function boundaries(): ?Period |
55
|
|
|
{ |
56
|
|
|
$start = null; |
57
|
|
|
$end = null; |
58
|
|
|
|
59
|
|
|
foreach ($this as $period) { |
60
|
|
|
if ($start === null || $start > $period->getIncludedStart()) { |
61
|
|
|
$start = $period->getStart(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($end === null || $end < $period->getIncludedEnd()) { |
65
|
|
|
$end = $period->getEnd(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (! $start || ! $end) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
[$firstPeriod] = $this->periods; |
|
|
|
|
74
|
|
|
|
75
|
|
|
return new Period( |
76
|
|
|
$start, |
77
|
|
|
$end, |
78
|
|
|
$firstPeriod->getPrecisionMask(), |
79
|
|
|
Boundaries::EXCLUDE_NONE |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function gaps(): PeriodCollection |
84
|
|
|
{ |
85
|
|
|
$boundaries = $this->boundaries(); |
86
|
|
|
|
87
|
|
|
if (! $boundaries) { |
88
|
|
|
return new PeriodCollection(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $boundaries->diff(...$this); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function intersect(Period $intersection): PeriodCollection |
95
|
|
|
{ |
96
|
|
|
$intersected = new PeriodCollection(); |
97
|
|
|
|
98
|
|
|
foreach ($this as $period) { |
99
|
|
|
$overlap = $intersection->overlapSingle($period); |
100
|
|
|
|
101
|
|
|
if ($overlap === null) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$intersected[] = $overlap; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $intersected; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function add(Period ...$periods): PeriodCollection |
112
|
|
|
{ |
113
|
|
|
$collection = clone $this; |
114
|
|
|
|
115
|
|
|
foreach ($periods as $period) { |
116
|
|
|
$collection[] = $period; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $collection; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function isEmpty(): bool |
123
|
|
|
{ |
124
|
|
|
return count($this->periods) === 0; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.