1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\ValueObject; |
4
|
|
|
|
5
|
|
|
abstract class AbstractFragmentedDuration |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Fragments. |
9
|
|
|
* |
10
|
|
|
* @var array<integer|null> |
11
|
|
|
*/ |
12
|
|
|
protected $fragments = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Fragment sizes. |
16
|
|
|
* |
17
|
|
|
* @var array<integer|null> |
18
|
|
|
*/ |
19
|
|
|
protected $sizes = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Transversal/parrallel units. |
23
|
|
|
* |
24
|
|
|
* @var array<integer|null> |
25
|
|
|
*/ |
26
|
|
|
protected $transversals = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class constructor. |
30
|
|
|
* |
31
|
|
|
* @param array<integer|null> $fragments |
32
|
|
|
* @param array<integer|null> $sizes |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $fragments = [], array $sizes = []) |
35
|
|
|
{ |
36
|
|
|
$this->fragments = $this->fillArrayInput($fragments); |
37
|
|
|
$this->sizes = $this->fillArrayInput($sizes); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get time fragment. |
42
|
|
|
* |
43
|
|
|
* @param integer $i |
44
|
|
|
* |
45
|
|
|
* @return integer|null |
46
|
|
|
*/ |
47
|
|
|
public function get($i) |
48
|
|
|
{ |
49
|
|
|
if (isset($this->fragments[$i])) { |
50
|
|
|
return $this->fragments[$i]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all fragments. |
56
|
|
|
* |
57
|
|
|
* @return array<integer|null> |
58
|
|
|
*/ |
59
|
|
|
public function all() |
60
|
|
|
{ |
61
|
|
|
$res = []; |
62
|
|
|
$len = count($this->fragments); |
63
|
|
|
|
64
|
|
|
for ($index=0; $index < $len; $index++) { |
65
|
|
|
$res[] = $this->get($index); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $res; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Count all sequencial non-null fragments (which are actually exploitable |
73
|
|
|
* to determine time) |
74
|
|
|
* |
75
|
|
|
* @return integer |
76
|
|
|
*/ |
77
|
|
|
public function countMeaningfull() |
78
|
|
|
{ |
79
|
|
|
$res = 0; |
80
|
|
|
|
81
|
|
|
foreach ($this->fragments as $value) { |
82
|
|
|
if (null === $value) { |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$res++; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $res; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set time fragment, adding null fragments if needed. |
94
|
|
|
* |
95
|
|
|
* @param integer $index |
96
|
|
|
* @param integer|null $value |
97
|
|
|
*/ |
98
|
|
|
public function with($index, $value) |
99
|
|
|
{ |
100
|
|
|
$res = clone $this; |
101
|
|
|
|
102
|
|
|
$res->fragments = $this->insertInList($res->fragments, $index, $value); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set all fragments, adding null fragments if needed. |
109
|
|
|
* |
110
|
|
|
* @param array<integer|null> $fragments |
111
|
|
|
* |
112
|
|
|
* @return static a new instance. |
113
|
|
|
*/ |
114
|
|
|
public function withFragments(array $fragments) |
115
|
|
|
{ |
116
|
|
|
$res = clone $this; |
117
|
|
|
|
118
|
|
|
$res->fragments = $res->fillArrayInput($fragments); |
119
|
|
|
|
120
|
|
|
return $res; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get time fragment size. |
125
|
|
|
* |
126
|
|
|
* @param integer $i |
127
|
|
|
* |
128
|
|
|
* @return integer|null |
129
|
|
|
*/ |
130
|
|
|
public function getSize($i) |
131
|
|
|
{ |
132
|
|
|
if (isset($this->sizes[$i])) { |
133
|
|
|
return $this->sizes[$i]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get all fragment sizes. |
139
|
|
|
* |
140
|
|
|
* @return array<integer|null> |
141
|
|
|
*/ |
142
|
|
|
public function allSizes() |
143
|
|
|
{ |
144
|
|
|
return $this->sizes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set time fragment size, adding null values if needed. |
149
|
|
|
* |
150
|
|
|
* @param integer $index |
151
|
|
|
* @param integer|null $value |
152
|
|
|
*/ |
153
|
|
|
public function withSize($index, $value) |
154
|
|
|
{ |
155
|
|
|
$res = clone $this; |
156
|
|
|
|
157
|
|
|
$res->sizes = $this->insertInList($res->sizes, $index, $value); |
158
|
|
|
|
159
|
|
|
return $res; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set all sizes, adding null values if needed. |
164
|
|
|
* |
165
|
|
|
* @param array<integer|null> $sizes |
166
|
|
|
* |
167
|
|
|
* @return static a new instance. |
168
|
|
|
*/ |
169
|
|
|
public function withSizes(array $sizes) |
170
|
|
|
{ |
171
|
|
|
$res = clone $this; |
172
|
|
|
|
173
|
|
|
$res->sizes = $res->fillArrayInput($sizes); |
174
|
|
|
|
175
|
|
|
return $res; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Insrt a value in a list, inserting null values if needed to keep a |
180
|
|
|
* conscutive indexing. |
181
|
|
|
* |
182
|
|
|
* @param array $values Actual values. |
183
|
|
|
* @param integer $index New value index. |
184
|
|
|
* @param mixed|null $value New value. |
185
|
|
|
* |
186
|
|
|
* @return array new value list. |
187
|
|
|
*/ |
188
|
|
|
public function insertInList(array $values, $index, $value) |
189
|
|
|
{ |
190
|
|
|
for ($i=count($values); $i < $index; $i++) { |
191
|
|
|
$values[$i] = null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$values[$index] = $value; |
195
|
|
|
|
196
|
|
|
return $values; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get transversal unit. |
201
|
|
|
* |
202
|
|
|
* @param integer $i |
203
|
|
|
* |
204
|
|
|
* @return integer|null |
205
|
|
|
*/ |
206
|
|
|
public function getTransversal($i) |
207
|
|
|
{ |
208
|
|
|
if (isset($this->transversals[$i])) { |
209
|
|
|
return $this->transversals[$i]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get all transversal units. |
215
|
|
|
* |
216
|
|
|
* @return array<integer|null> |
217
|
|
|
*/ |
218
|
|
|
public function allTransversals() |
219
|
|
|
{ |
220
|
|
|
return $this->transversals; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set transversal unit, adding null values if needed. |
225
|
|
|
* |
226
|
|
|
* @param integer $index |
227
|
|
|
* @param integer|null $value |
228
|
|
|
*/ |
229
|
|
|
public function withTransversal($index, $value) |
230
|
|
|
{ |
231
|
|
|
$res = clone $this; |
232
|
|
|
|
233
|
|
|
$res->transversals = $this->insertInList($res->transversals, $index, $value); |
234
|
|
|
|
235
|
|
|
return $res; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set all transversal units, adding null values if needed. |
240
|
|
|
* |
241
|
|
|
* @param array<integer|null> $transversals |
242
|
|
|
* |
243
|
|
|
* @return static a new instance. |
244
|
|
|
*/ |
245
|
|
|
public function withTransversals(array $transversals) |
246
|
|
|
{ |
247
|
|
|
$res = clone $this; |
248
|
|
|
|
249
|
|
|
$res->transversals = $res->fillArrayInput($transversals); |
250
|
|
|
|
251
|
|
|
return $res; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Ensure each consecutive key of the input array is set (filling with nulls) |
256
|
|
|
* |
257
|
|
|
* @param array<mixed|null> $input |
258
|
|
|
* @param mixed $default Filling value. |
259
|
|
|
* |
260
|
|
|
* @return array<mixed|null> |
261
|
|
|
*/ |
262
|
|
|
protected function fillArrayInput(array $input, $default = 0) |
263
|
|
|
{ |
264
|
|
|
if (empty($input)) { |
265
|
|
|
return []; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$res = array_fill($default, max(array_keys($input)), null); |
269
|
|
|
|
270
|
|
|
foreach ($input as $index => $value) { |
271
|
|
|
$res[$index] = $value; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $res; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|