1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Period; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Iterator; |
7
|
|
|
use Countable; |
8
|
|
|
use ArrayAccess; |
9
|
|
|
|
10
|
|
|
class PeriodCollection implements ArrayAccess, Iterator, Countable |
11
|
|
|
{ |
12
|
|
|
use IterableImplementation; |
13
|
|
|
|
14
|
|
|
/** @var \Spatie\Period\Period[] */ |
15
|
|
|
protected $periods; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \Spatie\Period\Period ...$periods |
19
|
|
|
* |
20
|
|
|
* @return static |
21
|
|
|
*/ |
22
|
|
|
public static function make(Period ...$periods): PeriodCollection |
23
|
|
|
{ |
24
|
|
|
return new static(...$periods); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __construct(Period ...$periods) |
28
|
|
|
{ |
29
|
|
|
$this->periods = $periods; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function current(): Period |
33
|
|
|
{ |
34
|
|
|
return $this->periods[$this->position]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Spatie\Period\PeriodCollection $periodCollection |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
|
|
*/ |
42
|
|
|
public function overlapSingle(PeriodCollection $periodCollection): PeriodCollection |
43
|
|
|
{ |
44
|
|
|
$overlaps = static::make(); |
45
|
|
|
|
46
|
|
|
foreach ($this as $period) { |
47
|
|
|
foreach ($periodCollection as $otherPeriod) { |
48
|
|
|
if (! $period->overlapSingle($otherPeriod)) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$overlaps[] = $period->overlapSingle($otherPeriod); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $overlaps; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Spatie\Period\PeriodCollection ...$periodCollections |
61
|
|
|
* |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public function overlap(PeriodCollection ...$periodCollections): PeriodCollection |
65
|
|
|
{ |
66
|
|
|
$overlap = clone $this; |
67
|
|
|
|
68
|
|
|
foreach ($periodCollections as $periodCollection) { |
69
|
|
|
$overlap = $overlap->overlapSingle($periodCollection); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $overlap; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function boundaries(): ?Period |
76
|
|
|
{ |
77
|
|
|
$start = null; |
78
|
|
|
$end = null; |
79
|
|
|
|
80
|
|
|
foreach ($this as $period) { |
81
|
|
|
if ($start === null || $start > $period->getIncludedStart()) { |
82
|
|
|
$start = $period->getStart(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($end === null || $end < $period->getIncludedEnd()) { |
86
|
|
|
$end = $period->getEnd(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (! $start || ! $end) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
[$firstPeriod] = $this->periods; |
|
|
|
|
95
|
|
|
|
96
|
|
|
return new Period( |
97
|
|
|
$start, |
98
|
|
|
$end, |
99
|
|
|
$firstPeriod->getPrecisionMask(), |
100
|
|
|
Boundaries::EXCLUDE_NONE |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function gaps(): PeriodCollection |
108
|
|
|
{ |
109
|
|
|
$boundaries = $this->boundaries(); |
110
|
|
|
|
111
|
|
|
if (! $boundaries) { |
112
|
|
|
return static::make(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $boundaries->diff(...$this); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \Spatie\Period\Period $intersection |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function intersect(Period $intersection): PeriodCollection |
124
|
|
|
{ |
125
|
|
|
$intersected = static::make(); |
126
|
|
|
|
127
|
|
|
foreach ($this as $period) { |
128
|
|
|
$overlap = $intersection->overlapSingle($period); |
129
|
|
|
|
130
|
|
|
if ($overlap === null) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$intersected[] = $overlap; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $intersected; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \Spatie\Period\Period ...$periods |
142
|
|
|
* |
143
|
|
|
* @return static |
144
|
|
|
*/ |
145
|
|
|
public function add(Period ...$periods): PeriodCollection |
146
|
|
|
{ |
147
|
|
|
$collection = clone $this; |
148
|
|
|
|
149
|
|
|
foreach ($periods as $period) { |
150
|
|
|
$collection[] = $period; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $collection; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param \Closure $closure |
158
|
|
|
* |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
|
|
public function map(Closure $closure): PeriodCollection |
162
|
|
|
{ |
163
|
|
|
$collection = clone $this; |
164
|
|
|
|
165
|
|
|
foreach ($collection->periods as $key => $period) { |
166
|
|
|
$collection->periods[$key] = $closure($period); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $collection; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param \Closure $closure |
174
|
|
|
* @param mixed $initial |
175
|
|
|
* |
176
|
|
|
* @return mixed|null |
177
|
|
|
*/ |
178
|
|
|
public function reduce(Closure $closure, $initial = null) |
179
|
|
|
{ |
180
|
|
|
$carry = $initial; |
181
|
|
|
|
182
|
|
|
foreach ($this as $period) { |
183
|
|
|
$carry = $closure($carry, $period); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $carry; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function isEmpty(): bool |
190
|
|
|
{ |
191
|
|
|
return count($this->periods) === 0; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
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.