|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Novactive Collection. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Luke Visinoni <[email protected], [email protected]> |
|
6
|
|
|
* @author Sébastien Morel <[email protected], [email protected]> |
|
7
|
|
|
* @copyright 2017 Novactive |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Novactive\Collection; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayAccess; |
|
14
|
|
|
use Countable; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use Iterator; |
|
17
|
|
|
use JsonSerializable; |
|
18
|
|
|
use Novactive\Collection\Selector\Range; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
use Traversable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Collection. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $items; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Collection constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $items |
|
36
|
|
|
*/ |
|
37
|
148 |
|
public function __construct(array $items = []) |
|
38
|
|
|
{ |
|
39
|
148 |
|
$this->items = $items; |
|
40
|
148 |
|
$this->rewind(); |
|
41
|
148 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
52 |
|
public function toArray() |
|
47
|
|
|
{ |
|
48
|
52 |
|
return $this->items; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set the value to the key no matter what. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $key |
|
55
|
|
|
* @param mixed $value |
|
56
|
|
|
* |
|
57
|
|
|
* @return $this |
|
58
|
|
|
*/ |
|
59
|
46 |
|
public function set($key, $value) |
|
60
|
|
|
{ |
|
61
|
46 |
|
$this->items[$key] = $value; |
|
62
|
|
|
|
|
63
|
46 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the value related to the key. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @param mixed $default |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed|null |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function get($key, $default = null) |
|
75
|
|
|
{ |
|
76
|
3 |
|
if ($this->containsKey($key)) { |
|
77
|
3 |
|
return $this->items[$key]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $default; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the item at the given index (numerically). |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $index |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed|null |
|
89
|
|
|
*/ |
|
90
|
11 |
|
public function atIndex($index) |
|
91
|
|
|
{ |
|
92
|
11 |
|
$i = 0; |
|
93
|
11 |
|
foreach ($this->items as $key => $value) { |
|
94
|
11 |
|
if ($i++ == $index) { |
|
95
|
10 |
|
return $value; |
|
96
|
|
|
} |
|
97
|
7 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the key of a value if exists. |
|
104
|
|
|
* |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed|null |
|
108
|
|
|
*/ |
|
109
|
10 |
|
public function keyOf($value) |
|
110
|
|
|
{ |
|
111
|
10 |
|
foreach ($this->items as $key => $iValue) { |
|
112
|
10 |
|
if ($value === $iValue) { |
|
113
|
8 |
|
return $key; |
|
114
|
|
|
} |
|
115
|
9 |
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the index of a value if exists (numerically). |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $value |
|
124
|
|
|
* |
|
125
|
|
|
* @return int|null |
|
126
|
|
|
*/ |
|
127
|
6 |
|
public function indexOf($value) |
|
128
|
|
|
{ |
|
129
|
6 |
|
$i = 0; |
|
130
|
6 |
|
foreach ($this->items as $key => $iValue) { |
|
131
|
6 |
|
if ($value === $iValue) { |
|
132
|
4 |
|
return $i; |
|
133
|
|
|
} |
|
134
|
5 |
|
$i++; |
|
135
|
5 |
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
return null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Test is the key exists. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $key |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
42 |
|
public function containsKey($key) |
|
148
|
|
|
{ |
|
149
|
42 |
|
return isset($this->items[$key]) || array_key_exists($key, $this->items); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Test if this values exists. |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $value |
|
156
|
|
|
* |
|
157
|
|
|
* @performanceCompared true |
|
158
|
|
|
* |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function contains($value) |
|
162
|
|
|
{ |
|
163
|
1 |
|
return in_array($value, $this->items, true); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Add a new value to the collection, next numeric index will be used. |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $item |
|
170
|
|
|
* |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
29 |
|
public function add($item) |
|
174
|
|
|
{ |
|
175
|
29 |
|
$this->items[] = $item; |
|
176
|
|
|
|
|
177
|
29 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Append the items at the end of the collection not regarding the keys. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Traversable|array $values $items |
|
184
|
|
|
* |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
27 |
|
public function append($values) |
|
188
|
|
|
{ |
|
189
|
27 |
|
if (!is_array($values) && !($values instanceof Traversable)) { |
|
190
|
1 |
|
$this->doThrow('Invalid input type for append.', $values); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
26 |
|
foreach ($values as $value) { |
|
194
|
26 |
|
$this->add($value); |
|
195
|
26 |
|
} |
|
196
|
|
|
|
|
197
|
26 |
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Clear the collection of all its items. |
|
202
|
|
|
* |
|
203
|
|
|
* @return $this |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function clear() |
|
206
|
|
|
{ |
|
207
|
1 |
|
$this->items = []; |
|
208
|
|
|
|
|
209
|
1 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Remove the $key/value in the collection. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $key |
|
216
|
|
|
* |
|
217
|
|
|
* @return $this |
|
218
|
|
|
*/ |
|
219
|
8 |
|
public function remove($key) |
|
220
|
|
|
{ |
|
221
|
8 |
|
unset($this->items[$key]); |
|
222
|
|
|
|
|
223
|
8 |
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Remove the $key/value in the collection and return the removed value. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $key |
|
230
|
|
|
* |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
*/ |
|
233
|
2 |
|
public function pull($key) |
|
234
|
|
|
{ |
|
235
|
2 |
|
if (!$this->containsKey($key)) { |
|
236
|
1 |
|
return null; |
|
237
|
|
|
} |
|
238
|
2 |
|
$removed = $this->items[$key]; |
|
239
|
2 |
|
unset($this->items[$key]); |
|
240
|
|
|
|
|
241
|
2 |
|
return $removed; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get the first time and reset and rewind. |
|
246
|
|
|
* |
|
247
|
|
|
* @param callable|null $callback |
|
248
|
|
|
* |
|
249
|
|
|
* @return mixed |
|
250
|
|
|
*/ |
|
251
|
27 |
|
public function first(callable $callback = null) |
|
252
|
|
|
{ |
|
253
|
27 |
|
if ($callback === null) { |
|
254
|
27 |
|
return reset($this->items); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
2 |
|
return $this->filter($callback)->first(); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Shift an element off the beginning of the collection(in-place). |
|
262
|
|
|
* |
|
263
|
|
|
* @performanceCompared true |
|
264
|
|
|
*/ |
|
265
|
1 |
|
public function shift() |
|
266
|
|
|
{ |
|
267
|
1 |
|
reset($this->items); |
|
268
|
|
|
|
|
269
|
1 |
|
return $this->pull($this->key()); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Shift an element off the end of the collection(in-place). |
|
274
|
|
|
* |
|
275
|
|
|
* @performanceCompared true |
|
276
|
|
|
*/ |
|
277
|
1 |
|
public function pop() |
|
278
|
|
|
{ |
|
279
|
1 |
|
return array_pop($this->items); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Get the last item. |
|
284
|
|
|
* |
|
285
|
|
|
* @param callable|null $callback |
|
286
|
|
|
* |
|
287
|
|
|
* @return mixed |
|
288
|
|
|
*/ |
|
289
|
27 |
|
public function last(callable $callback = null) |
|
290
|
|
|
{ |
|
291
|
27 |
|
if ($callback === null) { |
|
292
|
27 |
|
return end($this->items); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
2 |
|
return $this->filter($callback)->last(); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Map and return a new Collection. |
|
300
|
|
|
* |
|
301
|
|
|
* @param callable $callback |
|
302
|
|
|
* |
|
303
|
|
|
* @performanceCompared true |
|
304
|
|
|
* |
|
305
|
|
|
* @return Collection |
|
306
|
|
|
*/ |
|
307
|
26 |
|
public function map(callable $callback) |
|
308
|
|
|
{ |
|
309
|
26 |
|
$collection = Factory::create(); |
|
310
|
26 |
|
$index = 0; |
|
311
|
26 |
|
foreach ($this->items as $key => $value) { |
|
312
|
26 |
|
$collection->set($key, $callback($value, $key, $index++)); |
|
313
|
26 |
|
} |
|
314
|
|
|
|
|
315
|
26 |
|
return $collection; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Map (in-place). |
|
320
|
|
|
* |
|
321
|
|
|
* @param callable $callback |
|
322
|
|
|
* |
|
323
|
|
|
* @return $this |
|
324
|
|
|
*/ |
|
325
|
2 |
|
public function transform(callable $callback) |
|
326
|
|
|
{ |
|
327
|
2 |
|
$index = 0; |
|
328
|
2 |
|
foreach ($this->items as $key => $value) { |
|
329
|
2 |
|
$this->set($key, $callback($value, $key, $index++)); |
|
330
|
2 |
|
} |
|
331
|
|
|
|
|
332
|
2 |
|
return $this; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Filter and return a new Collection. |
|
337
|
|
|
* |
|
338
|
|
|
* @param callable $callback |
|
339
|
|
|
* |
|
340
|
|
|
* @performanceCompared true |
|
341
|
|
|
* |
|
342
|
|
|
* @return Collection |
|
343
|
|
|
*/ |
|
344
|
5 |
|
public function filter(callable $callback) |
|
345
|
|
|
{ |
|
346
|
5 |
|
$collection = Factory::create(); |
|
347
|
5 |
|
$index = 0; |
|
348
|
5 |
|
foreach ($this->items as $key => $value) { |
|
349
|
5 |
|
if ($callback($value, $key, $index++)) { |
|
350
|
5 |
|
$collection->set($key, $value); |
|
351
|
5 |
|
} |
|
352
|
5 |
|
} |
|
353
|
|
|
|
|
354
|
5 |
|
return $collection; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Filter (in-place). |
|
359
|
|
|
* |
|
360
|
|
|
* @param callable $callback |
|
361
|
|
|
* |
|
362
|
|
|
* @return $this |
|
363
|
|
|
*/ |
|
364
|
4 |
|
public function prune(callable $callback) |
|
365
|
|
|
{ |
|
366
|
4 |
|
$index = 0; |
|
367
|
4 |
|
foreach ($this->items as $key => $value) { |
|
368
|
4 |
|
if (!$callback($value, $key, $index++)) { |
|
369
|
4 |
|
$this->remove($key); |
|
370
|
4 |
|
} |
|
371
|
4 |
|
} |
|
372
|
|
|
|
|
373
|
4 |
|
return $this; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Combine and return a new Collection. |
|
378
|
|
|
* It takes the keys of the current Collection and assign the values. |
|
379
|
|
|
* |
|
380
|
|
|
* @param Traversable|array $values |
|
381
|
|
|
* @param bool $inPlace |
|
382
|
|
|
* |
|
383
|
|
|
* @performanceCompared true |
|
384
|
|
|
* |
|
385
|
|
|
* @return mixed |
|
386
|
|
|
*/ |
|
387
|
7 |
|
public function combine($values, $inPlace = false) |
|
388
|
|
|
{ |
|
389
|
7 |
|
if (!is_array($values) && !($values instanceof Traversable)) { |
|
390
|
2 |
|
$this->doThrow('Invalid input type for '.($inPlace ? 'replace' : 'combine').'.', $values); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
5 |
|
if (count($values) != count($this->items)) { |
|
394
|
2 |
|
$this->doThrow( |
|
395
|
2 |
|
'Invalid input for '.($inPlace ? 'replace' : 'combine').', number of items does not match.', |
|
396
|
|
|
$values |
|
397
|
2 |
|
); |
|
398
|
|
|
} |
|
399
|
3 |
|
$values = Factory::getArrayForItems($values); |
|
400
|
|
|
|
|
401
|
3 |
|
return Factory::create(array_combine($this->items, $values)); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Combine (in-place). |
|
406
|
|
|
* |
|
407
|
|
|
* @param Traversable|array $values |
|
408
|
|
|
* |
|
409
|
|
|
* @return $this |
|
410
|
|
|
*/ |
|
411
|
3 |
|
public function replace($values) |
|
412
|
|
|
{ |
|
413
|
3 |
|
$this->items = $this->combine($values, true)->toArray(); |
|
414
|
|
|
|
|
415
|
1 |
|
return $this; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Opposite of Combine |
|
420
|
|
|
* It keeps the values of the current Collection and assign new keys. |
|
421
|
|
|
* |
|
422
|
|
|
* @param Traversable|array $keys |
|
423
|
|
|
* |
|
424
|
|
|
* @return Collection |
|
425
|
|
|
*/ |
|
426
|
5 |
|
public function combineKeys($keys) |
|
427
|
|
|
{ |
|
428
|
5 |
|
if (!is_array($keys) && !($keys instanceof Traversable)) { |
|
429
|
1 |
|
$this->doThrow('Invalid input type for combineKeys.', $keys); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
4 |
|
if (count($keys) != count($this->items)) { |
|
433
|
1 |
|
$this->doThrow('Invalid input for combineKeys, number of items does not match.', $keys); |
|
434
|
|
|
} |
|
435
|
3 |
|
$collection = Factory::create(); |
|
436
|
3 |
|
$this->rewind(); |
|
437
|
3 |
|
foreach ($keys as $key) { |
|
438
|
3 |
|
$collection->set($key, $this->current()); |
|
439
|
3 |
|
$this->next(); |
|
440
|
3 |
|
} |
|
441
|
3 |
|
$this->rewind(); |
|
442
|
|
|
|
|
443
|
3 |
|
return $collection; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* CombineKeys (in-place). |
|
448
|
|
|
* |
|
449
|
|
|
* @param Traversable|array $keys |
|
450
|
|
|
* |
|
451
|
|
|
* @return $this |
|
452
|
|
|
*/ |
|
453
|
1 |
|
public function reindex($keys) |
|
454
|
|
|
{ |
|
455
|
1 |
|
$this->items = $this->combineKeys($keys)->toArray(); |
|
456
|
|
|
|
|
457
|
1 |
|
return $this; |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Reduce. |
|
462
|
|
|
* |
|
463
|
|
|
* @param callable $callback |
|
464
|
|
|
* @param null $initial |
|
465
|
|
|
* |
|
466
|
|
|
* @performanceCompared true |
|
467
|
|
|
* |
|
468
|
|
|
* @return mixed |
|
469
|
|
|
*/ |
|
470
|
25 |
|
public function reduce(callable $callback, $initial = null) |
|
471
|
|
|
{ |
|
472
|
25 |
|
$accumulator = $initial; |
|
473
|
25 |
|
$index = 0; |
|
474
|
25 |
|
foreach ($this->items as $key => $value) { |
|
475
|
25 |
|
$accumulator = $callback($accumulator, $value, $key, $index++); |
|
476
|
25 |
|
} |
|
477
|
|
|
|
|
478
|
25 |
|
return $accumulator; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* Run the callback on each element (passive). |
|
483
|
|
|
* |
|
484
|
|
|
* @param callable $callback |
|
485
|
|
|
* |
|
486
|
|
|
* @performanceCompared true |
|
487
|
|
|
* |
|
488
|
|
|
* @return $this |
|
489
|
|
|
*/ |
|
490
|
1 |
|
public function each(callable $callback) |
|
491
|
|
|
{ |
|
492
|
1 |
|
$index = 0; |
|
493
|
1 |
|
foreach ($this->items as $key => $value) { |
|
494
|
1 |
|
$callback($value, $key, $index++); |
|
495
|
1 |
|
} |
|
496
|
|
|
|
|
497
|
1 |
|
return $this; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Flip the keys and the values and return a new Collection. |
|
502
|
|
|
* |
|
503
|
|
|
* @performanceCompared true |
|
504
|
|
|
* |
|
505
|
|
|
* @return Collection |
|
506
|
|
|
*/ |
|
507
|
3 |
|
public function flip() |
|
508
|
|
|
{ |
|
509
|
3 |
|
$collection = Factory::create(); |
|
510
|
3 |
|
foreach ($this->items as $key => $value) { |
|
511
|
3 |
|
$collection->set($value, $key); |
|
512
|
3 |
|
} |
|
513
|
|
|
|
|
514
|
3 |
|
return $collection; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* Flip (in-place). |
|
519
|
|
|
* |
|
520
|
|
|
* @return $this |
|
521
|
|
|
*/ |
|
522
|
2 |
|
public function invert() |
|
523
|
|
|
{ |
|
524
|
2 |
|
$this->items = $this->flip()->toArray(); |
|
525
|
|
|
|
|
526
|
2 |
|
return $this; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Merge the items and the collections and return a new Collection. |
|
531
|
|
|
* |
|
532
|
|
|
* Note that is a real merge, numerical keys are merged too.(unlike array_merge) |
|
533
|
|
|
* |
|
534
|
|
|
* @param Traversable|array $items |
|
535
|
|
|
* @param bool $inPlace |
|
536
|
|
|
* |
|
537
|
|
|
* @performanceCompared true |
|
538
|
|
|
* |
|
539
|
|
|
* @return Collection |
|
540
|
|
|
*/ |
|
541
|
8 |
|
public function merge($items, $inPlace = false) |
|
542
|
|
|
{ |
|
543
|
8 |
|
if (!is_array($items) && !($items instanceof Traversable)) { |
|
544
|
6 |
|
$this->doThrow('Invalid input type for '.($inPlace ? 'coalesce' : 'merge').'.', $items); |
|
545
|
|
|
} |
|
546
|
2 |
|
$collection = $inPlace ? $this : clone $this; |
|
547
|
2 |
|
foreach ($items as $key => $value) { |
|
548
|
2 |
|
$collection->set($key, $value); |
|
549
|
2 |
|
} |
|
550
|
|
|
|
|
551
|
2 |
|
return $collection; |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
/** |
|
555
|
|
|
* Merge (in-place). |
|
556
|
|
|
* |
|
557
|
|
|
* @param Traversable|array $items |
|
558
|
|
|
* |
|
559
|
|
|
* @return $this |
|
560
|
|
|
*/ |
|
561
|
4 |
|
public function coalesce($items) |
|
562
|
|
|
{ |
|
563
|
4 |
|
return $this->merge($items, true); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* Union the collection with Items. |
|
568
|
|
|
* |
|
569
|
|
|
* @param Traversable|array $items |
|
570
|
|
|
* @param bool $inPlace |
|
571
|
|
|
* |
|
572
|
|
|
* @performanceCompared true |
|
573
|
|
|
* |
|
574
|
|
|
* @return Collection |
|
575
|
|
|
*/ |
|
576
|
8 |
|
public function union($items, $inPlace = false) |
|
577
|
|
|
{ |
|
578
|
8 |
|
if (!is_array($items) && !($items instanceof Traversable)) { |
|
579
|
6 |
|
$this->doThrow('Invalid input type for '.($inPlace ? 'absorb' : 'union'), $items); |
|
580
|
|
|
} |
|
581
|
2 |
|
$collection = $inPlace ? $this : clone $this; |
|
582
|
2 |
|
foreach ($items as $key => $value) { |
|
583
|
2 |
|
if (!$collection->containsKey($key)) { |
|
584
|
2 |
|
$collection->set($key, $value); |
|
585
|
2 |
|
} |
|
586
|
2 |
|
} |
|
587
|
|
|
|
|
588
|
2 |
|
return $collection; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* Union (in-place). |
|
593
|
|
|
* |
|
594
|
|
|
* @param Traversable|array $items |
|
595
|
|
|
* |
|
596
|
|
|
* @return $this |
|
597
|
|
|
*/ |
|
598
|
4 |
|
public function absorb($items) |
|
599
|
|
|
{ |
|
600
|
4 |
|
$this->union($items, true); |
|
601
|
|
|
|
|
602
|
1 |
|
return $this; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* Assert that the callback result is $expected for all. |
|
607
|
|
|
* |
|
608
|
|
|
* @param callable $callback |
|
609
|
|
|
* @param bool $expected |
|
610
|
|
|
* |
|
611
|
|
|
* @return bool |
|
612
|
|
|
*/ |
|
613
|
26 |
|
public function assert(callable $callback, $expected) |
|
614
|
|
|
{ |
|
615
|
26 |
|
$index = 0; |
|
616
|
26 |
|
foreach ($this->items as $key => $value) { |
|
617
|
26 |
|
if ($callback($value, $key, $index++) !== $expected) { |
|
618
|
3 |
|
return false; |
|
619
|
|
|
} |
|
620
|
24 |
|
} |
|
621
|
|
|
|
|
622
|
24 |
|
return true; |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
/** |
|
626
|
|
|
* Return all the values. |
|
627
|
|
|
* |
|
628
|
|
|
* @performanceCompared true |
|
629
|
|
|
* |
|
630
|
|
|
* @return Collection |
|
631
|
|
|
*/ |
|
632
|
9 |
|
public function values() |
|
633
|
|
|
{ |
|
634
|
9 |
|
return Factory::create(array_values($this->items)); |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* Return all the keys. |
|
639
|
|
|
* |
|
640
|
|
|
* @performanceCompared true |
|
641
|
|
|
* |
|
642
|
|
|
* @return Collection |
|
643
|
|
|
*/ |
|
644
|
2 |
|
public function keys() |
|
645
|
|
|
{ |
|
646
|
2 |
|
return Factory::create(array_keys($this->items)); |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* Pass the collection to the given callback and return the result. |
|
651
|
|
|
* |
|
652
|
|
|
* @param callable $callback |
|
653
|
|
|
* |
|
654
|
|
|
* @return mixed |
|
655
|
|
|
*/ |
|
656
|
2 |
|
public function pipe(callable $callback) |
|
657
|
|
|
{ |
|
658
|
2 |
|
return $callback($this); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Shuffle. (random in-place). |
|
663
|
|
|
* |
|
664
|
|
|
* @return $this |
|
665
|
|
|
*/ |
|
666
|
1 |
|
public function shuffle() |
|
667
|
|
|
{ |
|
668
|
1 |
|
shuffle($this->items); |
|
669
|
|
|
|
|
670
|
1 |
|
return $this; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Shuffle and return a new Collection. |
|
675
|
|
|
* |
|
676
|
|
|
* @return Collection |
|
677
|
|
|
*/ |
|
678
|
1 |
|
public function random() |
|
679
|
|
|
{ |
|
680
|
1 |
|
$array = $this->items; |
|
681
|
1 |
|
shuffle($array); |
|
682
|
|
|
|
|
683
|
1 |
|
return Factory::create($array); |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
/** |
|
687
|
|
|
* Deduplicate the collection and return a new Collection. |
|
688
|
|
|
* |
|
689
|
|
|
* @performanceCompared true |
|
690
|
|
|
* |
|
691
|
|
|
* @return Collection |
|
692
|
|
|
*/ |
|
693
|
1 |
|
public function unique() |
|
694
|
|
|
{ |
|
695
|
1 |
|
return Factory::create(array_unique($this->items)); |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
/** |
|
699
|
|
|
* @param string $separator |
|
700
|
|
|
* |
|
701
|
|
|
* @return string |
|
702
|
|
|
*/ |
|
703
|
24 |
|
public function implode($separator) |
|
704
|
|
|
{ |
|
705
|
24 |
|
return implode($this->items, $separator); |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
/** |
|
709
|
|
|
* Unique (in-place). |
|
710
|
|
|
* |
|
711
|
|
|
* @return $this |
|
712
|
|
|
*/ |
|
713
|
1 |
|
public function distinct() |
|
714
|
|
|
{ |
|
715
|
1 |
|
$this->items = array_unique($this->items); |
|
716
|
|
|
|
|
717
|
1 |
|
return $this; |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
/** |
|
721
|
|
|
* Merge the values items by items. |
|
722
|
|
|
* |
|
723
|
|
|
* @param Traversable|array $items |
|
724
|
|
|
* |
|
725
|
|
|
* @return Collection |
|
726
|
|
|
*/ |
|
727
|
2 |
|
public function zip($items) |
|
728
|
|
|
{ |
|
729
|
2 |
|
$collection = Factory::create($items); |
|
730
|
|
|
|
|
731
|
2 |
|
return $this->map( |
|
732
|
|
|
function ($value, $key, $index) use ($collection) { |
|
733
|
2 |
|
return [$value, $collection->atIndex($index)]; |
|
734
|
|
|
} |
|
735
|
2 |
|
); |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
/** |
|
739
|
|
|
* Reverse the collection and return a new Collection. |
|
740
|
|
|
* |
|
741
|
|
|
* @performanceCompared true |
|
742
|
|
|
* |
|
743
|
|
|
* @return Collection |
|
744
|
|
|
*/ |
|
745
|
1 |
|
public function reverse() |
|
746
|
|
|
{ |
|
747
|
1 |
|
return Factory::create(array_reverse($this->items, true)); |
|
748
|
|
|
} |
|
749
|
|
|
|
|
750
|
|
|
/** |
|
751
|
|
|
* Reverse (in-place). |
|
752
|
|
|
* |
|
753
|
|
|
* @return $this |
|
754
|
|
|
*/ |
|
755
|
8 |
|
public function inverse() |
|
756
|
|
|
{ |
|
757
|
8 |
|
$this->items = array_reverse($this->items); |
|
758
|
|
|
|
|
759
|
8 |
|
return $this; |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
/** |
|
763
|
|
|
* @return bool |
|
764
|
|
|
*/ |
|
765
|
1 |
|
public function isEmpty() |
|
766
|
|
|
{ |
|
767
|
1 |
|
return $this->count() == 0; |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
|
|
/** |
|
771
|
|
|
* Split in the collection in $count parts. |
|
772
|
|
|
* |
|
773
|
|
|
* @param int $count |
|
774
|
|
|
* |
|
775
|
|
|
* @return Collection |
|
776
|
|
|
*/ |
|
777
|
1 |
|
public function split($count = 1) |
|
778
|
|
|
{ |
|
779
|
1 |
|
return $this->chunk(ceil($this->count() / $count)); |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
|
|
/** |
|
783
|
|
|
* Chunk of $size sub collection. |
|
784
|
|
|
* |
|
785
|
|
|
* @param int $size |
|
786
|
|
|
* |
|
787
|
|
|
* @performanceCompared true |
|
788
|
|
|
* |
|
789
|
|
|
* @return Collection |
|
790
|
|
|
*/ |
|
791
|
2 |
|
public function chunk($size) |
|
792
|
|
|
{ |
|
793
|
2 |
|
return Factory::create(array_chunk($this->items, $size, true)); |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
|
|
/** |
|
797
|
|
|
* Get a slice of the collection and inject it in a new one. |
|
798
|
|
|
* |
|
799
|
|
|
* @param int $offset |
|
800
|
|
|
* @param int|null $length |
|
801
|
|
|
* |
|
802
|
|
|
* @performanceCompared true |
|
803
|
|
|
* |
|
804
|
|
|
* @return Collection |
|
805
|
|
|
*/ |
|
806
|
30 |
|
public function slice($offset, $length = null) |
|
807
|
|
|
{ |
|
808
|
30 |
|
return Factory::create(array_slice($this->items, $offset, $length, true)); |
|
809
|
|
|
} |
|
810
|
|
|
|
|
811
|
|
|
/** |
|
812
|
|
|
* Keep a slice of the collection (in-place). |
|
813
|
|
|
* |
|
814
|
|
|
* @param int $offset |
|
815
|
|
|
* @param int|null $length |
|
816
|
|
|
* |
|
817
|
|
|
* @return $this |
|
818
|
|
|
*/ |
|
819
|
6 |
|
public function keep($offset, $length = null) |
|
820
|
|
|
{ |
|
821
|
6 |
|
$this->items = $this->slice($offset, $length)->toArray(); |
|
822
|
|
|
|
|
823
|
6 |
|
return $this; |
|
824
|
|
|
} |
|
825
|
|
|
|
|
826
|
|
|
/** |
|
827
|
|
|
* Cut a slice of the collection (in-place). |
|
828
|
|
|
* |
|
829
|
|
|
* @param int $offset |
|
830
|
|
|
* @param int|null $length |
|
831
|
|
|
* |
|
832
|
|
|
* @return $this |
|
833
|
|
|
*/ |
|
834
|
3 |
|
public function cut($offset, $length = null) |
|
835
|
|
|
{ |
|
836
|
3 |
|
if ($length === null) { |
|
837
|
1 |
|
$length = PHP_INT_MAX; |
|
838
|
1 |
|
} |
|
839
|
3 |
|
if ($offset < 0) { |
|
840
|
1 |
|
$offset = $this->count() + $offset; |
|
841
|
1 |
|
} |
|
842
|
|
|
|
|
843
|
3 |
|
return $this->prune( |
|
844
|
3 |
|
function ($value, $key, $index) use ($offset, $length) { |
|
845
|
3 |
|
return !(($index >= $offset) && ($index < $offset + $length)); |
|
846
|
|
|
} |
|
847
|
3 |
|
); |
|
848
|
|
|
} |
|
849
|
|
|
|
|
850
|
|
|
/** |
|
851
|
|
|
* Compares the collection against $items and returns the values that are not present in the collection. |
|
852
|
|
|
* |
|
853
|
|
|
* @param Traversable|array $items |
|
854
|
|
|
* |
|
855
|
|
|
* @performanceCompared true |
|
856
|
|
|
* |
|
857
|
|
|
* @return Collection |
|
858
|
|
|
*/ |
|
859
|
1 |
|
public function diff($items) |
|
860
|
|
|
{ |
|
861
|
1 |
|
$items = Factory::getArrayForItems($items); |
|
862
|
|
|
|
|
863
|
1 |
|
return Factory::create(array_diff($this->items, $items)); |
|
864
|
|
|
} |
|
865
|
|
|
|
|
866
|
|
|
/** |
|
867
|
|
|
* Compares the collection against $items and returns the keys that are not present in the collection. |
|
868
|
|
|
* |
|
869
|
|
|
* @param Traversable|array $items |
|
870
|
|
|
* |
|
871
|
|
|
* @performanceCompared true |
|
872
|
|
|
* |
|
873
|
|
|
* @return Collection |
|
874
|
|
|
*/ |
|
875
|
1 |
|
public function diffKeys($items) |
|
876
|
|
|
{ |
|
877
|
1 |
|
$items = Factory::getArrayForItems($items); |
|
878
|
|
|
|
|
879
|
1 |
|
return Factory::create(array_diff_key($this->items, $items)); |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
/** |
|
883
|
|
|
* Compares the collection against $items and returns the values that exist in the collection. |
|
884
|
|
|
* |
|
885
|
|
|
* @param Traversable|array $items |
|
886
|
|
|
* |
|
887
|
|
|
* @performanceCompared true |
|
888
|
|
|
* |
|
889
|
|
|
* @return Collection |
|
890
|
|
|
*/ |
|
891
|
1 |
|
public function intersect($items) |
|
892
|
|
|
{ |
|
893
|
1 |
|
$items = Factory::getArrayForItems($items); |
|
894
|
|
|
|
|
895
|
1 |
|
return Factory::create(array_intersect($this->items, $items)); |
|
896
|
|
|
} |
|
897
|
|
|
|
|
898
|
|
|
/** |
|
899
|
|
|
* Compares the collection against $items and returns the keys that exist in the collection. |
|
900
|
|
|
* |
|
901
|
|
|
* @param Traversable|array $items |
|
902
|
|
|
* |
|
903
|
|
|
* @performanceCompared true |
|
904
|
|
|
* |
|
905
|
|
|
* @return Collection |
|
906
|
|
|
*/ |
|
907
|
1 |
|
public function intersectKeys($items) |
|
908
|
|
|
{ |
|
909
|
1 |
|
$items = Factory::getArrayForItems($items); |
|
910
|
|
|
|
|
911
|
1 |
|
return Factory::create(array_intersect_key($this->items, $items)); |
|
912
|
|
|
} |
|
913
|
|
|
|
|
914
|
|
|
/** |
|
915
|
|
|
* Return true if one item return true to the callback. |
|
916
|
|
|
* |
|
917
|
|
|
* @param callable $callback |
|
918
|
|
|
* |
|
919
|
|
|
* @return bool |
|
920
|
|
|
*/ |
|
921
|
1 |
|
public function exists(callable $callback) |
|
922
|
|
|
{ |
|
923
|
1 |
|
$index = 0; |
|
924
|
1 |
|
foreach ($this->items as $key => $item) { |
|
925
|
1 |
|
if ($callback($item, $key, $index++) === true) { |
|
926
|
1 |
|
return true; |
|
927
|
|
|
} |
|
928
|
1 |
|
} |
|
929
|
|
|
|
|
930
|
1 |
|
return false; |
|
931
|
|
|
} |
|
932
|
|
|
|
|
933
|
|
|
/* -- --- -- */ |
|
934
|
|
|
/* -- INTERFACE COMPLIANCE -- */ |
|
935
|
|
|
/* -- --- -- */ |
|
936
|
|
|
|
|
937
|
|
|
/* -- -- */ |
|
938
|
|
|
/* -- JsonSerializable -- */ |
|
939
|
|
|
/* -- -- */ |
|
940
|
|
|
|
|
941
|
|
|
/** |
|
942
|
|
|
* @return array |
|
943
|
|
|
*/ |
|
944
|
2 |
|
public function jsonSerialize() |
|
945
|
|
|
{ |
|
946
|
2 |
|
return $this->toArray(); |
|
947
|
|
|
} |
|
948
|
|
|
|
|
949
|
|
|
/* -- -- */ |
|
950
|
|
|
/* -- Array Access Methods -- */ |
|
951
|
|
|
/* -- -- */ |
|
952
|
|
|
|
|
953
|
|
|
/** |
|
954
|
|
|
* {@inheritDoc} |
|
955
|
|
|
*/ |
|
956
|
1 |
|
public function offsetExists($offset) |
|
957
|
|
|
{ |
|
958
|
1 |
|
return $this->containsKey($offset); |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
/** |
|
962
|
|
|
* {@inheritDoc} |
|
963
|
|
|
*/ |
|
964
|
2 |
|
public function offsetGet($offset) |
|
965
|
|
|
{ |
|
966
|
2 |
|
if (!$this->containsKey($offset)) { |
|
967
|
1 |
|
throw new RuntimeException("Unknown offset: {$offset}"); |
|
968
|
|
|
} |
|
969
|
|
|
|
|
970
|
1 |
|
return $this->get($offset); |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
/** |
|
974
|
|
|
* {@inheritDoc} |
|
975
|
|
|
*/ |
|
976
|
1 |
|
public function offsetUnset($offset) |
|
977
|
|
|
{ |
|
978
|
1 |
|
$this->remove($offset); |
|
979
|
1 |
|
} |
|
980
|
|
|
|
|
981
|
|
|
/** |
|
982
|
|
|
* {@inheritDoc} |
|
983
|
|
|
*/ |
|
984
|
2 |
|
public function offsetSet($offset, $value) |
|
985
|
|
|
{ |
|
986
|
2 |
|
if (!isset($offset)) { |
|
987
|
1 |
|
$this->add($value); |
|
988
|
1 |
|
} |
|
989
|
|
|
|
|
990
|
2 |
|
$this->set($offset, $value); |
|
991
|
2 |
|
} |
|
992
|
|
|
|
|
993
|
|
|
/* -- -- */ |
|
994
|
|
|
/* -- Iterator Methods -- */ |
|
995
|
|
|
/* -- -- */ |
|
996
|
|
|
|
|
997
|
|
|
/** |
|
998
|
|
|
* {@inheritDoc} |
|
999
|
|
|
*/ |
|
1000
|
36 |
|
public function current() |
|
1001
|
|
|
{ |
|
1002
|
36 |
|
return current($this->items); |
|
1003
|
|
|
} |
|
1004
|
|
|
|
|
1005
|
|
|
/** |
|
1006
|
|
|
* {@inheritDoc} |
|
1007
|
|
|
*/ |
|
1008
|
6 |
|
public function key() |
|
1009
|
|
|
{ |
|
1010
|
6 |
|
return key($this->items); |
|
1011
|
|
|
} |
|
1012
|
|
|
|
|
1013
|
|
|
/** |
|
1014
|
|
|
* {@inheritDoc} |
|
1015
|
|
|
*/ |
|
1016
|
36 |
|
public function next() |
|
1017
|
|
|
{ |
|
1018
|
36 |
|
return next($this->items); |
|
1019
|
|
|
} |
|
1020
|
|
|
|
|
1021
|
|
|
/** |
|
1022
|
|
|
* {@inheritDoc} |
|
1023
|
|
|
*/ |
|
1024
|
148 |
|
public function rewind() |
|
1025
|
|
|
{ |
|
1026
|
148 |
|
reset($this->items); |
|
1027
|
148 |
|
} |
|
1028
|
|
|
|
|
1029
|
|
|
/** |
|
1030
|
|
|
* {@inheritDoc} |
|
1031
|
|
|
*/ |
|
1032
|
31 |
|
public function valid() |
|
1033
|
|
|
{ |
|
1034
|
31 |
|
return $this->containsKey(key($this->items)); |
|
1035
|
|
|
} |
|
1036
|
|
|
|
|
1037
|
|
|
/* -- -- */ |
|
1038
|
|
|
/* -- Countable Method -- */ |
|
1039
|
|
|
/* -- -- */ |
|
1040
|
|
|
|
|
1041
|
|
|
/** |
|
1042
|
|
|
* {@inheritDoc} |
|
1043
|
|
|
*/ |
|
1044
|
17 |
|
public function count() |
|
1045
|
|
|
{ |
|
1046
|
17 |
|
return count($this->items); |
|
1047
|
|
|
} |
|
1048
|
|
|
|
|
1049
|
|
|
/* -- --- -- */ |
|
1050
|
|
|
/* -- OTHERS -- */ |
|
1051
|
|
|
/* -- --- -- */ |
|
1052
|
|
|
|
|
1053
|
|
|
/** |
|
1054
|
|
|
* @param string $message |
|
1055
|
|
|
* @param mixed $arguments |
|
1056
|
|
|
* |
|
1057
|
|
|
* @throws InvalidArgumentException |
|
1058
|
|
|
*/ |
|
1059
|
21 |
|
protected function doThrow($message, $arguments) |
|
1060
|
|
|
{ |
|
1061
|
21 |
|
unset($arguments); |
|
1062
|
21 |
|
throw new InvalidArgumentException($message); |
|
1063
|
|
|
} |
|
1064
|
|
|
|
|
1065
|
|
|
/** |
|
1066
|
|
|
* @return $this |
|
1067
|
|
|
*/ |
|
1068
|
2 |
|
public function dump() |
|
1069
|
|
|
{ |
|
1070
|
2 |
|
return $this; |
|
1071
|
|
|
} |
|
1072
|
|
|
|
|
1073
|
|
|
/** |
|
1074
|
|
|
* @param array ...$params |
|
1075
|
|
|
* |
|
1076
|
|
|
* @throws InvalidArgumentException |
|
1077
|
|
|
* |
|
1078
|
|
|
* @return Collection |
|
1079
|
|
|
*/ |
|
1080
|
25 |
|
public function __invoke(...$params) |
|
1081
|
|
|
{ |
|
1082
|
25 |
|
$tool = new Range(); |
|
1083
|
25 |
|
$parameters = Factory::create($params); |
|
1084
|
25 |
|
if ($tool->supports($parameters)) { |
|
1085
|
23 |
|
return $tool->convert($parameters, $this); |
|
1086
|
|
|
} |
|
1087
|
2 |
|
$this->doThrow('No Selector is able to handle this invocation.', $params); |
|
1088
|
|
|
} |
|
1089
|
|
|
} |
|
1090
|
|
|
|