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