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->push(...$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->push(...$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
|
|
View Code Duplication |
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
|
|
|
$this->checkCapacity(); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function join(string $glue = null): string |
139
|
|
|
{ |
140
|
|
|
return implode($glue, $this->array); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
|
|
public function last() |
147
|
|
|
{ |
148
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
149
|
|
|
throw new UnderflowException(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->array[count($this) - 1]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritDoc |
157
|
|
|
*/ |
158
|
|
|
public function map(callable $callback): Sequence |
159
|
|
|
{ |
160
|
|
|
return new self(array_map($callback, $this->array)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritDoc |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function pop() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
169
|
|
|
throw new UnderflowException(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$value = array_pop($this->array); |
173
|
|
|
$this->checkCapacity(); |
|
|
|
|
174
|
|
|
|
175
|
|
|
return $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritDoc |
180
|
|
|
*/ |
181
|
|
|
public function push(...$values) |
182
|
|
|
{ |
183
|
|
|
$this->ensureCapacity($this->count() + count($values)); |
|
|
|
|
184
|
|
|
|
185
|
|
|
foreach ($values as $value) { |
186
|
|
|
$this->array[] = $value; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @inheritDoc |
192
|
|
|
*/ |
193
|
|
|
public function reduce(callable $callback, $initial = null) |
194
|
|
|
{ |
195
|
|
|
return array_reduce($this->array, $callback, $initial); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritDoc |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function remove(int $index) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
if ( ! $this->validIndex($index)) { |
204
|
|
|
throw new OutOfRangeException(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$value = array_splice($this->array, $index, 1, null)[0]; |
208
|
|
|
$this->checkCapacity(); |
|
|
|
|
209
|
|
|
|
210
|
|
|
return $value; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritDoc |
215
|
|
|
*/ |
216
|
|
|
public function reverse() |
217
|
|
|
{ |
218
|
|
|
$this->array = array_reverse($this->array); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @inheritDoc |
223
|
|
|
*/ |
224
|
|
|
public function reversed(): Sequence |
225
|
|
|
{ |
226
|
|
|
return new self(array_reverse($this->array)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Converts negative or large rotations into the minimum positive number |
231
|
|
|
* of rotations required to rotate the sequence by a given $r. |
232
|
|
|
*/ |
233
|
|
|
private function normalizeRotations(int $r) |
234
|
|
|
{ |
235
|
|
|
$n = count($this); |
236
|
|
|
|
237
|
|
|
if ($n < 2) return 0; |
238
|
|
|
if ($r < 0) return $n - (abs($r) % $n); |
239
|
|
|
|
240
|
|
|
return $r % $n; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @inheritDoc |
245
|
|
|
*/ |
246
|
|
|
public function rotate(int $rotations) |
247
|
|
|
{ |
248
|
|
|
for ($r = $this->normalizeRotations($rotations); $r > 0; $r--) { |
249
|
|
|
array_push($this->array, array_shift($this->array)); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritDoc |
255
|
|
|
*/ |
256
|
|
|
public function set(int $index, $value) |
257
|
|
|
{ |
258
|
|
|
if ( ! $this->validIndex($index)) { |
259
|
|
|
throw new OutOfRangeException(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$this->array[$index] = $value; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @inheritDoc |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
public function shift() |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
271
|
|
|
throw new UnderflowException(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$value = array_shift($this->array); |
275
|
|
|
$this->checkCapacity(); |
|
|
|
|
276
|
|
|
|
277
|
|
|
return $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @inheritDoc |
282
|
|
|
*/ |
283
|
|
|
public function slice(int $offset, int $length = null): Sequence |
284
|
|
|
{ |
285
|
|
|
if (func_num_args() === 1) { |
286
|
|
|
$length = count($this); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return new self(array_slice($this->array, $offset, $length)); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @inheritDoc |
294
|
|
|
*/ |
295
|
|
|
public function sort(callable $comparator = null) |
296
|
|
|
{ |
297
|
|
|
if ($comparator) { |
298
|
|
|
usort($this->array, $comparator); |
299
|
|
|
} else { |
300
|
|
|
sort($this->array); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @inheritDoc |
306
|
|
|
*/ |
307
|
|
|
public function sorted(callable $comparator = null): Sequence |
308
|
|
|
{ |
309
|
|
|
$copy = $this->copy(); |
|
|
|
|
310
|
|
|
$copy->sort($comparator); |
311
|
|
|
return $copy; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @inheritDoc |
316
|
|
|
*/ |
317
|
|
|
public function sum() |
318
|
|
|
{ |
319
|
|
|
return array_sum($this->array); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @inheritDoc |
324
|
|
|
*/ |
325
|
|
|
public function unshift(...$values) |
326
|
|
|
{ |
327
|
|
|
if ($values) { |
|
|
|
|
328
|
|
|
$this->array = array_merge($values, $this->array); |
329
|
|
|
$this->checkCapacity(); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* |
335
|
|
|
*/ |
336
|
|
|
private function validIndex(int $index) |
337
|
|
|
{ |
338
|
|
|
return $index >= 0 && $index < count($this); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* |
343
|
|
|
*/ |
344
|
|
|
public function getIterator() |
345
|
|
|
{ |
346
|
|
|
foreach ($this->array as $value) { |
347
|
|
|
yield $value; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @inheritdoc |
353
|
|
|
*/ |
354
|
|
|
public function clear() |
355
|
|
|
{ |
356
|
|
|
$this->array = []; |
357
|
|
|
$this->capacity = self::MIN_CAPACITY; |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @inheritdoc |
362
|
|
|
*/ |
363
|
|
|
public function offsetSet($offset, $value) |
364
|
|
|
{ |
365
|
|
|
if ($offset === null) { |
366
|
|
|
$this->push($value); |
367
|
|
|
} else { |
368
|
|
|
$this->set($offset, $value); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @inheritdoc |
374
|
|
|
*/ |
375
|
|
|
public function &offsetGet($offset) |
376
|
|
|
{ |
377
|
|
|
if ( ! $this->validIndex($offset)) { |
378
|
|
|
throw new OutOfRangeException(); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this->array[$offset]; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @inheritdoc |
386
|
|
|
*/ |
387
|
|
|
public function offsetUnset($offset) |
388
|
|
|
{ |
389
|
|
|
if (is_integer($offset) && $this->validIndex($offset)) { |
390
|
|
|
$this->remove($offset); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @inheritdoc |
396
|
|
|
*/ |
397
|
|
|
public function offsetExists($offset) |
398
|
|
|
{ |
399
|
|
|
return is_integer($offset) |
400
|
|
|
&& $this->validIndex($offset) |
401
|
|
|
&& $this->get($offset) !== null; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
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.