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
|
|
|
public function __construct(Period ...$periods) |
18
|
|
|
{ |
19
|
|
|
$this->periods = $periods; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function current(): Period |
23
|
|
|
{ |
24
|
|
|
return $this->periods[$this->position]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function overlapSingle(PeriodCollection $periodCollection): PeriodCollection |
28
|
|
|
{ |
29
|
|
|
$overlaps = new PeriodCollection(); |
30
|
|
|
|
31
|
|
|
foreach ($this as $period) { |
32
|
|
|
foreach ($periodCollection as $otherPeriod) { |
33
|
|
|
if (! $period->overlapSingle($otherPeriod)) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$overlaps[] = $period->overlapSingle($otherPeriod); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $overlaps; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function overlap(PeriodCollection ...$periodCollections): PeriodCollection |
45
|
|
|
{ |
46
|
|
|
$overlap = clone $this; |
47
|
|
|
|
48
|
|
|
foreach ($periodCollections as $periodCollection) { |
49
|
|
|
$overlap = $overlap->overlapSingle($periodCollection); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $overlap; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function boundaries(): ?Period |
56
|
|
|
{ |
57
|
|
|
$start = null; |
58
|
|
|
$end = null; |
59
|
|
|
|
60
|
|
|
foreach ($this as $period) { |
61
|
|
|
if ($start === null || $start > $period->getIncludedStart()) { |
62
|
|
|
$start = $period->getStart(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($end === null || $end < $period->getIncludedEnd()) { |
66
|
|
|
$end = $period->getEnd(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (! $start || ! $end) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
[$firstPeriod] = $this->periods; |
|
|
|
|
75
|
|
|
|
76
|
|
|
return new Period( |
77
|
|
|
$start, |
78
|
|
|
$end, |
79
|
|
|
$firstPeriod->getPrecisionMask(), |
80
|
|
|
Boundaries::EXCLUDE_NONE |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function gaps(): PeriodCollection |
85
|
|
|
{ |
86
|
|
|
$boundaries = $this->boundaries(); |
87
|
|
|
|
88
|
|
|
if (! $boundaries) { |
89
|
|
|
return new PeriodCollection(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $boundaries->diff(...$this); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function intersect(Period $intersection): PeriodCollection |
96
|
|
|
{ |
97
|
|
|
$intersected = new PeriodCollection(); |
98
|
|
|
|
99
|
|
|
foreach ($this as $period) { |
100
|
|
|
$overlap = $intersection->overlapSingle($period); |
101
|
|
|
|
102
|
|
|
if ($overlap === null) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$intersected[] = $overlap; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $intersected; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function add(Period ...$periods): PeriodCollection |
113
|
|
|
{ |
114
|
|
|
$collection = clone $this; |
115
|
|
|
|
116
|
|
|
foreach ($periods as $period) { |
117
|
|
|
$collection[] = $period; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $collection; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function map(Closure $closure): PeriodCollection |
124
|
|
|
{ |
125
|
|
|
$collection = clone $this; |
126
|
|
|
|
127
|
|
|
foreach ($collection->periods as $key => $period) { |
128
|
|
|
$collection->periods[$key] = $closure($period); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $collection; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function reduce(Closure $closure, $initial = null) |
135
|
|
|
{ |
136
|
|
|
$carry = $initial; |
137
|
|
|
|
138
|
|
|
foreach ($this as $period) { |
139
|
|
|
$carry = $closure($carry, $period); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $carry; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function isEmpty(): bool |
146
|
|
|
{ |
147
|
|
|
return count($this->periods) === 0; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.