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