|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\Immutable; |
|
4
|
|
|
|
|
5
|
|
|
class Collection implements CollectionInterface |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
private $values; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct(array $values) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->values = $values; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
public function toPrimitive() |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->values; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function filter(callable $filter = null) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($filter === null) { |
|
28
|
|
|
$values = array_filter($this->values); |
|
29
|
|
|
} else { |
|
30
|
|
|
$values = array_filter($this->values, $filter); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return new self($values); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function intersect(CollectionInterface $collection) |
|
40
|
|
|
{ |
|
41
|
|
|
return new self(array_intersect( |
|
42
|
|
|
$this->values, |
|
43
|
|
|
$collection->toPrimitive() |
|
44
|
|
|
)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function chunk($size) |
|
51
|
|
|
{ |
|
52
|
|
|
return new self(array_chunk($this->values, (int) $size)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function shift() |
|
59
|
|
|
{ |
|
60
|
|
|
$values = $this->values; |
|
61
|
|
|
array_shift($values); |
|
62
|
|
|
|
|
63
|
|
|
return new self($values); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function reduce(callable $reducer, $initial = null) |
|
70
|
|
|
{ |
|
71
|
|
|
return array_reduce($this->values, $reducer, $initial); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function search($needle, $strict = true) |
|
78
|
|
|
{ |
|
79
|
|
|
return array_search($needle, $this->values, (bool) $strict); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function uintersect(CollectionInterface $collection, callable $intersecter) |
|
86
|
|
|
{ |
|
87
|
|
|
return new self(array_uintersect( |
|
88
|
|
|
$this->values, |
|
89
|
|
|
$collection->toPrimitive(), |
|
90
|
|
|
$intersecter |
|
91
|
|
|
)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function keyIntersect(CollectionInterface $collection) |
|
98
|
|
|
{ |
|
99
|
|
|
return new self(array_intersect_key( |
|
100
|
|
|
$this->values, |
|
101
|
|
|
$collection->toPrimitive() |
|
102
|
|
|
)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function map(callable $mapper) |
|
109
|
|
|
{ |
|
110
|
|
|
return new self(array_map($mapper, $this->values)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function pad($size, $value) |
|
117
|
|
|
{ |
|
118
|
|
|
return new self(array_pad($this->values, (int) $size, $value)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function pop() |
|
125
|
|
|
{ |
|
126
|
|
|
$values = $this->values; |
|
127
|
|
|
array_pop($values); |
|
128
|
|
|
|
|
129
|
|
|
return new self($values); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function sum() |
|
136
|
|
|
{ |
|
137
|
|
|
return array_sum($this->values); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function diff(CollectionInterface $collection) |
|
144
|
|
|
{ |
|
145
|
|
|
return new self(array_diff($this->values, $collection->toPrimitive())); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function flip() |
|
152
|
|
|
{ |
|
153
|
|
|
return new self(array_flip($this->values)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function keys($search = null, $strict = true) |
|
160
|
|
|
{ |
|
161
|
|
|
return new self(array_keys($this->values, $search, (bool) $strict)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
|
|
public function push($value) |
|
168
|
|
|
{ |
|
169
|
|
|
$values = $this->values; |
|
170
|
|
|
array_push($values, $value); |
|
171
|
|
|
|
|
172
|
|
|
return new self($values); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
|
public function rand($num = 1) |
|
179
|
|
|
{ |
|
180
|
|
|
if ((int) $num > $this->count()->toPrimitive()) { |
|
|
|
|
|
|
181
|
|
|
throw new OutOfBoundException( |
|
182
|
|
|
'Trying to return a wider collection than the current one' |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$keys = (array) array_rand($this->values, $num); |
|
187
|
|
|
$values = []; |
|
188
|
|
|
|
|
189
|
|
|
foreach ($keys as $key) { |
|
190
|
|
|
$values[$key] = $this->values[$key]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return new self($values); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
|
|
public function merge(CollectionInterface $collection) |
|
200
|
|
|
{ |
|
201
|
|
|
return new self(array_merge( |
|
202
|
|
|
$this->values, |
|
203
|
|
|
$collection->toPrimitive() |
|
204
|
|
|
)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* {@inheritdoc} |
|
209
|
|
|
*/ |
|
210
|
|
|
public function slice($offset, $length = null, $preserveKeys = false) |
|
211
|
|
|
{ |
|
212
|
|
|
return new self(array_slice( |
|
213
|
|
|
$this->values, |
|
214
|
|
|
(int) $offset, |
|
215
|
|
|
$length === null ? null : (int) $length, |
|
216
|
|
|
(bool) $preserveKeys |
|
217
|
|
|
)); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* {@inheritdoc} |
|
222
|
|
|
*/ |
|
223
|
|
|
public function udiff(CollectionInterface $collection, callable $differ) |
|
224
|
|
|
{ |
|
225
|
|
|
return new self(array_udiff( |
|
226
|
|
|
$this->values, |
|
227
|
|
|
$collection->toPrimitive(), |
|
228
|
|
|
$differ |
|
229
|
|
|
)); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* {@inheritdoc} |
|
234
|
|
|
*/ |
|
235
|
|
|
public function column($key, $indexKey = null) |
|
236
|
|
|
{ |
|
237
|
|
|
return new self(array_column( |
|
238
|
|
|
$this->values, |
|
239
|
|
|
$key, |
|
240
|
|
|
$indexKey |
|
241
|
|
|
)); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* {@inheritdoc} |
|
246
|
|
|
*/ |
|
247
|
|
|
public function splice($offset, $length = 0, $replacement = []) |
|
248
|
|
|
{ |
|
249
|
|
|
$values = $this->values; |
|
250
|
|
|
array_splice($values, (int) $offset, (int) $length, $replacement); |
|
251
|
|
|
|
|
252
|
|
|
return new self($values); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* {@inheritdoc} |
|
257
|
|
|
*/ |
|
258
|
|
|
public function unique($flags = SORT_REGULAR) |
|
259
|
|
|
{ |
|
260
|
|
|
return new self(array_unique($this->values, (int) $flags)); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* {@inheritdoc} |
|
265
|
|
|
*/ |
|
266
|
|
|
public function values() |
|
267
|
|
|
{ |
|
268
|
|
|
return new self(array_values($this->values)); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* {@inheritdoc} |
|
273
|
|
|
*/ |
|
274
|
|
|
public function product() |
|
275
|
|
|
{ |
|
276
|
|
|
return array_product($this->values); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritdoc} |
|
281
|
|
|
*/ |
|
282
|
|
|
public function replace(CollectionInterface $collection) |
|
283
|
|
|
{ |
|
284
|
|
|
return new self(array_replace( |
|
285
|
|
|
$this->values, |
|
286
|
|
|
$collection->toPrimitive() |
|
287
|
|
|
)); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* {@inheritdoc} |
|
292
|
|
|
*/ |
|
293
|
|
|
public function reverse($preserveKeys = false) |
|
294
|
|
|
{ |
|
295
|
|
|
return new self(array_reverse($this->values, (bool) $preserveKeys)); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* {@inheritdoc} |
|
300
|
|
|
*/ |
|
301
|
|
|
public function unshift($value) |
|
302
|
|
|
{ |
|
303
|
|
|
$values = $this->values; |
|
304
|
|
|
array_unshift($values, $value); |
|
305
|
|
|
|
|
306
|
|
|
return new self($values); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* {@inheritdoc} |
|
311
|
|
|
*/ |
|
312
|
|
|
public function keyDiff(CollectionInterface $collection) |
|
313
|
|
|
{ |
|
314
|
|
|
return new self(array_diff_key( |
|
315
|
|
|
$this->values, |
|
316
|
|
|
$collection->toPrimitive() |
|
317
|
|
|
)); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* {@inheritdoc} |
|
322
|
|
|
*/ |
|
323
|
|
|
public function ukeyDiff(CollectionInterface $collection, callable $differ) |
|
324
|
|
|
{ |
|
325
|
|
|
return new self(array_diff_ukey( |
|
326
|
|
|
$this->values, |
|
327
|
|
|
$collection->toPrimitive(), |
|
328
|
|
|
$differ |
|
329
|
|
|
)); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* {@inheritdoc} |
|
334
|
|
|
*/ |
|
335
|
|
|
public function associativeDiff(CollectionInterface $collection) |
|
336
|
|
|
{ |
|
337
|
|
|
return new self(array_diff_assoc( |
|
338
|
|
|
$this->values, |
|
339
|
|
|
$collection->toPrimitive() |
|
340
|
|
|
)); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* {@inheritdoc} |
|
345
|
|
|
*/ |
|
346
|
|
|
public function hasKey($key, $strict = true) |
|
347
|
|
|
{ |
|
348
|
|
|
if ((bool) $strict === true) { |
|
349
|
|
|
$bool = array_key_exists($key, $this->values); |
|
350
|
|
|
} else { |
|
351
|
|
|
$bool = isset($this->values[$key]); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
return new BooleanPrimitive($bool); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* {@inheritdoc} |
|
359
|
|
|
*/ |
|
360
|
|
|
public function countValues() |
|
361
|
|
|
{ |
|
362
|
|
|
return new self(array_count_values($this->values)); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* {@inheritdoc} |
|
367
|
|
|
*/ |
|
368
|
|
|
public function ukeyIntersect(CollectionInterface $collection, callable $intersecter) |
|
369
|
|
|
{ |
|
370
|
|
|
return new self(array_intersect_ukey( |
|
371
|
|
|
$this->values, |
|
372
|
|
|
$collection->toPrimitive(), |
|
373
|
|
|
$intersecter |
|
374
|
|
|
)); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* {@inheritdoc} |
|
379
|
|
|
*/ |
|
380
|
|
|
public function associativeIntersect(CollectionInterface $collection) |
|
381
|
|
|
{ |
|
382
|
|
|
return new self(array_intersect_assoc( |
|
383
|
|
|
$this->values, |
|
384
|
|
|
$collection->toPrimitive() |
|
385
|
|
|
)); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* {@inheritdoc} |
|
390
|
|
|
*/ |
|
391
|
|
View Code Duplication |
public function sort($flags = SORT_REGULAR) |
|
|
|
|
|
|
392
|
|
|
{ |
|
393
|
|
|
$values = $this->values; |
|
394
|
|
|
$bool = sort($values, (int) $flags); |
|
395
|
|
|
|
|
396
|
|
|
if ($bool === false) { |
|
397
|
|
|
throw new \Exception('Sort failure'); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
return new self($values); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* {@inheritdoc} |
|
405
|
|
|
*/ |
|
406
|
|
View Code Duplication |
public function associativeSort($flags = SORT_REGULAR) |
|
|
|
|
|
|
407
|
|
|
{ |
|
408
|
|
|
$values = $this->values; |
|
409
|
|
|
$bool = asort($values, (int) $flags); |
|
410
|
|
|
|
|
411
|
|
|
if ($bool === false) { |
|
412
|
|
|
throw new \Exception('Sort failure'); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return new self($values); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* {@inheritdoc} |
|
420
|
|
|
*/ |
|
421
|
|
View Code Duplication |
public function keySort($flags = SORT_REGULAR) |
|
|
|
|
|
|
422
|
|
|
{ |
|
423
|
|
|
$values = $this->values; |
|
424
|
|
|
$bool = ksort($values, (int) $flags); |
|
425
|
|
|
|
|
426
|
|
|
if ($bool === false) { |
|
427
|
|
|
throw new \Exception('Sort failure'); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
return new self($values); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* {@inheritdoc} |
|
435
|
|
|
*/ |
|
436
|
|
|
public function ukeySort(callable $sorter) |
|
437
|
|
|
{ |
|
438
|
|
|
$values = $this->values; |
|
439
|
|
|
$bool = uksort($values, $sorter); |
|
440
|
|
|
|
|
441
|
|
|
if ($bool === false) { |
|
442
|
|
|
throw new \Exception('Sort failure'); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
return new self($values); |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
|