1
|
|
|
<?php |
2
|
|
|
namespace Ds\Traits; |
3
|
|
|
|
4
|
|
|
use Ds\Sequence; |
5
|
|
|
use OutOfRangeException; |
6
|
|
|
use UnderflowException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Common functionality of all structures that implement 'Sequence'. Because the |
10
|
|
|
* polyfill's only goal is to achieve consistent behaviour, all sequences will |
11
|
|
|
* share the same implementation using an array array. |
12
|
|
|
* |
13
|
|
|
* @package Ds\Traits |
14
|
|
|
*/ |
15
|
|
|
trait GenericSequence |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array internal array used to store the values of the sequence. |
19
|
|
|
*/ |
20
|
|
|
private $array = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritDoc |
24
|
|
|
*/ |
25
|
|
|
public function __construct($values = null) |
26
|
|
|
{ |
27
|
|
|
if ($values) { |
28
|
|
|
$this->pushAll($values); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function toArray(): array |
36
|
|
|
{ |
37
|
|
|
return $this->array; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function apply(callable $callback) |
44
|
|
|
{ |
45
|
|
|
foreach ($this->array as &$value) { |
46
|
|
|
$value = $callback($value); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function merge($values): Sequence |
54
|
|
|
{ |
55
|
|
|
$copy = $this->copy(); |
|
|
|
|
56
|
|
|
$copy->pushAll($values); |
57
|
|
|
return $copy; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function count(): int |
64
|
|
|
{ |
65
|
|
|
return count($this->array); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function contains(...$values): bool |
72
|
|
|
{ |
73
|
|
|
foreach ($values as $value) { |
74
|
|
|
if ($this->find($value) === false) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function filter(callable $callback = null): Sequence |
86
|
|
|
{ |
87
|
|
|
return new self(array_filter($this->array, $callback ?: 'boolval')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function find($value) |
94
|
|
|
{ |
95
|
|
|
return array_search($value, $this->array, true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function first() |
102
|
|
|
{ |
103
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
104
|
|
|
throw new UnderflowException(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->array[0]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function get(int $index) |
114
|
|
|
{ |
115
|
|
|
if ( ! $this->validIndex($index)) { |
116
|
|
|
throw new OutOfRangeException(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->array[$index]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function insert(int $index, ...$values) |
126
|
|
|
{ |
127
|
|
|
if ( ! $this->validIndex($index) && $index !== count($this)) { |
128
|
|
|
throw new OutOfRangeException(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
array_splice($this->array, $index, 0, $values); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
|
|
public function join(string $glue = null): string |
138
|
|
|
{ |
139
|
|
|
return implode($glue, $this->array); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
public function last() |
146
|
|
|
{ |
147
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
148
|
|
|
throw new UnderflowException(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->array[count($this) - 1]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
|
|
public function map(callable $callback): Sequence |
158
|
|
|
{ |
159
|
|
|
return new self(array_map($callback, $this->array)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
|
|
public function pop() |
166
|
|
|
{ |
167
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
168
|
|
|
throw new UnderflowException(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$value = array_pop($this->array); |
172
|
|
|
$this->checkCapacity(); |
|
|
|
|
173
|
|
|
|
174
|
|
|
return $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Pushes all values of either an array or traversable object. |
179
|
|
|
*/ |
180
|
|
|
private function pushAll($values) |
181
|
|
|
{ |
182
|
|
|
foreach ($values as $value) { |
183
|
|
|
$this->array[] = $value; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->checkCapacity(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @inheritDoc |
191
|
|
|
*/ |
192
|
|
|
public function push(...$values) |
193
|
|
|
{ |
194
|
|
|
$this->pushAll($values); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritDoc |
199
|
|
|
*/ |
200
|
|
|
public function reduce(callable $callback, $initial = null) |
201
|
|
|
{ |
202
|
|
|
return array_reduce($this->array, $callback, $initial); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritDoc |
207
|
|
|
*/ |
208
|
|
|
public function remove(int $index) |
209
|
|
|
{ |
210
|
|
|
if ( ! $this->validIndex($index)) { |
211
|
|
|
throw new OutOfRangeException(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$value = array_splice($this->array, $index, 1, null)[0]; |
215
|
|
|
$this->checkCapacity(); |
|
|
|
|
216
|
|
|
|
217
|
|
|
return $value; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @inheritDoc |
222
|
|
|
*/ |
223
|
|
|
public function reverse() |
224
|
|
|
{ |
225
|
|
|
$this->array = array_reverse($this->array); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritDoc |
230
|
|
|
*/ |
231
|
|
|
public function reversed(): Sequence |
232
|
|
|
{ |
233
|
|
|
return new self(array_reverse($this->array)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Converts negative or large rotations into the minimum positive number |
238
|
|
|
* of rotations required to rotate the sequence by a given $r. |
239
|
|
|
*/ |
240
|
|
|
private function normalizeRotations(int $r) |
241
|
|
|
{ |
242
|
|
|
$n = count($this); |
243
|
|
|
|
244
|
|
|
if ($n < 2) return 0; |
245
|
|
|
if ($r < 0) return $n - (abs($r) % $n); |
246
|
|
|
|
247
|
|
|
return $r % $n; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritDoc |
252
|
|
|
*/ |
253
|
|
|
public function rotate(int $rotations) |
254
|
|
|
{ |
255
|
|
|
for ($r = $this->normalizeRotations($rotations); $r > 0; $r--) { |
256
|
|
|
array_push($this->array, array_shift($this->array)); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @inheritDoc |
262
|
|
|
*/ |
263
|
|
|
public function set(int $index, $value) |
264
|
|
|
{ |
265
|
|
|
if ( ! $this->validIndex($index)) { |
266
|
|
|
throw new OutOfRangeException(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$this->array[$index] = $value; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @inheritDoc |
274
|
|
|
*/ |
275
|
|
|
public function shift() |
276
|
|
|
{ |
277
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
278
|
|
|
throw new UnderflowException(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$value = array_shift($this->array); |
282
|
|
|
$this->checkCapacity(); |
|
|
|
|
283
|
|
|
|
284
|
|
|
return $value; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @inheritDoc |
289
|
|
|
*/ |
290
|
|
|
public function slice(int $offset, int $length = null): Sequence |
291
|
|
|
{ |
292
|
|
|
if (func_num_args() === 1) { |
293
|
|
|
$length = count($this); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return new self(array_slice($this->array, $offset, $length)); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @inheritDoc |
301
|
|
|
*/ |
302
|
|
|
public function sort(callable $comparator = null) |
303
|
|
|
{ |
304
|
|
|
if ($comparator) { |
305
|
|
|
usort($this->array, $comparator); |
306
|
|
|
} else { |
307
|
|
|
sort($this->array); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @inheritDoc |
313
|
|
|
*/ |
314
|
|
|
public function sorted(callable $comparator = null): Sequence |
315
|
|
|
{ |
316
|
|
|
$copy = $this->copy(); |
|
|
|
|
317
|
|
|
$copy->sort($comparator); |
318
|
|
|
return $copy; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @inheritDoc |
323
|
|
|
*/ |
324
|
|
|
public function sum() |
325
|
|
|
{ |
326
|
|
|
return array_sum($this->array); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @inheritDoc |
331
|
|
|
*/ |
332
|
|
|
public function unshift(...$values) |
333
|
|
|
{ |
334
|
|
|
if ($values) { |
|
|
|
|
335
|
|
|
$this->array = array_merge($values, $this->array); |
336
|
|
|
$this->checkCapacity(); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* |
342
|
|
|
*/ |
343
|
|
|
private function validIndex(int $index) |
344
|
|
|
{ |
345
|
|
|
return $index >= 0 && $index < count($this); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* |
350
|
|
|
*/ |
351
|
|
|
public function getIterator() |
352
|
|
|
{ |
353
|
|
|
foreach ($this->array as $value) { |
354
|
|
|
yield $value; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @inheritdoc |
360
|
|
|
*/ |
361
|
|
|
public function clear() |
362
|
|
|
{ |
363
|
|
|
$this->array = []; |
364
|
|
|
$this->capacity = self::MIN_CAPACITY; |
|
|
|
|
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @inheritdoc |
369
|
|
|
*/ |
370
|
|
|
public function offsetSet($offset, $value) |
371
|
|
|
{ |
372
|
|
|
if ($offset === null) { |
373
|
|
|
$this->push($value); |
374
|
|
|
} else { |
375
|
|
|
$this->set($offset, $value); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @inheritdoc |
381
|
|
|
*/ |
382
|
|
|
public function &offsetGet($offset) |
383
|
|
|
{ |
384
|
|
|
if ( ! $this->validIndex($offset)) { |
385
|
|
|
throw new OutOfRangeException(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $this->array[$offset]; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @inheritdoc |
393
|
|
|
*/ |
394
|
|
|
public function offsetUnset($offset) |
395
|
|
|
{ |
396
|
|
|
if (is_integer($offset) && $this->validIndex($offset)) { |
397
|
|
|
$this->remove($offset); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @inheritdoc |
403
|
|
|
*/ |
404
|
|
|
public function offsetExists($offset) |
405
|
|
|
{ |
406
|
|
|
return is_integer($offset) |
407
|
|
|
&& $this->validIndex($offset) |
408
|
|
|
&& $this->get($offset) !== null; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.