1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DusanKasan\Knapsack; |
4
|
|
|
|
5
|
|
|
use DusanKasan\Knapsack\Exceptions\InvalidReturnValue; |
6
|
|
|
|
7
|
|
|
trait CollectionTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Converts $collection to array. If there are multiple items with the same key, only the last will be preserved. |
11
|
|
|
* |
12
|
|
|
* @return array |
13
|
|
|
*/ |
14
|
61 |
|
public function toArray() |
15
|
|
|
{ |
16
|
61 |
|
return toArray($this->getItems()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Returns a lazy collection of items for which $function returned true. |
21
|
|
|
* |
22
|
|
|
* @param callable $function ($value, $key) |
23
|
|
|
* @return Collection |
24
|
|
|
*/ |
25
|
1 |
|
public function filter(callable $function) |
26
|
|
|
{ |
27
|
1 |
|
return filter($this->getItems(), $function); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns a lazy collection of distinct items. The comparison is the same as in in_array. |
32
|
|
|
* |
33
|
|
|
* @return Collection |
34
|
|
|
*/ |
35
|
1 |
|
public function distinct() |
36
|
|
|
{ |
37
|
1 |
|
return distinct($this->getItems()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a lazy collection with items from all $collections passed as argument appended together |
42
|
|
|
* |
43
|
|
|
* @param \Traversable[]|array ...$collections |
44
|
|
|
* @return Collection |
45
|
|
|
*/ |
46
|
1 |
|
public function concat(...$collections) |
47
|
|
|
{ |
48
|
1 |
|
return concat($this, ...$collections); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns collection where each item is changed to the output of executing $function on each key/item. |
53
|
|
|
* |
54
|
|
|
* @param callable $function |
55
|
|
|
* @return Collection |
56
|
|
|
*/ |
57
|
3 |
|
public function map(callable $function) |
58
|
|
|
{ |
59
|
3 |
|
return map($this->getItems(), $function); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Reduces the collection to single value by iterating over the collection and calling $function while |
64
|
|
|
* passing $startValue and current key/item as parameters. The output of $function is used as $startValue in |
65
|
|
|
* next iteration. The output of $function on last element is the return value of this function. If |
66
|
|
|
* $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
67
|
|
|
* is returned. |
68
|
|
|
* |
69
|
|
|
* @param callable $function ($tmpValue, $value, $key) |
70
|
|
|
* @param mixed $startValue |
71
|
|
|
* @param bool $convertToCollection |
72
|
|
|
* @return mixed|Collection |
73
|
|
|
*/ |
74
|
2 |
|
public function reduce(callable $function, $startValue, $convertToCollection = false) |
75
|
|
|
{ |
76
|
2 |
|
$result = reduce($this->getItems(), $function, $startValue); |
77
|
|
|
|
78
|
2 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value |
83
|
|
|
* is passed. |
84
|
|
|
* |
85
|
|
|
* @param int $depth How many levels should be flatten, default (-1) is infinite. |
86
|
|
|
* @return Collection |
87
|
|
|
*/ |
88
|
1 |
|
public function flatten($depth = -1) |
89
|
|
|
{ |
90
|
1 |
|
return flatten($this->getItems(), $depth); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should |
95
|
|
|
* return true if first item is larger than the second and false otherwise. |
96
|
|
|
* |
97
|
|
|
* @param callable $function ($value1, $value2, $key1, $key2) |
98
|
|
|
* @return Collection |
99
|
|
|
*/ |
100
|
1 |
|
public function sort(callable $function) |
101
|
|
|
{ |
102
|
1 |
|
return \DusanKasan\Knapsack\sort($this->getItems(), $function); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns lazy collection items of which are part of the original collection from item number $from to item |
107
|
|
|
* number $to. The items before $from are also iterated over, just not returned. |
108
|
|
|
* |
109
|
|
|
* @param int $from |
110
|
|
|
* @param int $to If omitted, will slice until end |
111
|
|
|
* @return Collection |
112
|
|
|
*/ |
113
|
1 |
|
public function slice($from, $to = -1) |
114
|
|
|
{ |
115
|
1 |
|
return slice($this->getItems(), $from, $to); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns collection which items are separated into groups indexed by the return value of $function. |
120
|
|
|
* |
121
|
|
|
* @param callable $function ($value, $key) |
122
|
|
|
* @return Collection |
123
|
|
|
*/ |
124
|
1 |
|
public function groupBy(callable $function) |
125
|
|
|
{ |
126
|
1 |
|
return groupBy($this->getItems(), $function); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns collection where items are separated into groups indexed by the value at given key. |
131
|
|
|
* |
132
|
|
|
* @param $key |
133
|
|
|
* @return Collection |
134
|
|
|
*/ |
135
|
1 |
|
public function groupByKey($key) |
136
|
|
|
{ |
137
|
1 |
|
return groupByKey($this->getItems(), $key); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns a lazy collection in which $function is executed for each item. |
142
|
|
|
* |
143
|
|
|
* @param callable $function ($value, $key) |
144
|
|
|
* @return Collection |
145
|
|
|
*/ |
146
|
1 |
|
public function each(callable $function) |
147
|
|
|
{ |
148
|
1 |
|
return \DusanKasan\Knapsack\each($this->getItems(), $function); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the number of items in this collection. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
6 |
|
public function size() |
157
|
|
|
{ |
158
|
6 |
|
return size($this->getItems()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw |
163
|
|
|
* ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable an |
164
|
|
|
* instance of Collection will be returned. |
165
|
|
|
* |
166
|
|
|
* @param mixed $key |
167
|
|
|
* @param bool $convertToCollection |
168
|
|
|
* @return Collection|mixed |
169
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
170
|
|
|
*/ |
171
|
7 |
|
public function get($key, $convertToCollection = false) |
172
|
|
|
{ |
173
|
7 |
|
$result = get($this->getItems(), $key); |
174
|
|
|
|
175
|
5 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return |
180
|
|
|
* $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value |
181
|
|
|
* is a collection (array|Traversable) an instance of Collection will be returned. |
182
|
|
|
* |
183
|
|
|
* @param mixed $key |
184
|
|
|
* @param mixed $default |
185
|
|
|
* @param bool $convertToCollection |
186
|
|
|
* @return mixed|Collection |
187
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
188
|
|
|
*/ |
189
|
1 |
|
public function getOrDefault($key, $default = null, $convertToCollection = false) |
190
|
|
|
{ |
191
|
1 |
|
$result = getOrDefault($this->getItems(), $key, $default); |
192
|
|
|
|
193
|
1 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true |
198
|
|
|
* and the return value is a collection (array|Traversable an instance of Collection will be returned. |
199
|
|
|
* |
200
|
|
|
* @param callable $function |
201
|
|
|
* @param mixed|null $default |
202
|
|
|
* @param bool $convertToCollection |
203
|
|
|
* @return Collection|mixed |
204
|
|
|
*/ |
205
|
1 |
|
public function find(callable $function, $default = null, $convertToCollection = false) |
206
|
|
|
{ |
207
|
1 |
|
$result = find($this->getItems(), $function, $default); |
208
|
|
|
|
209
|
1 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |
214
|
|
|
* items in this collection for which the $function returned this value. |
215
|
|
|
* |
216
|
|
|
* @param callable $function |
217
|
|
|
* @return Collection |
218
|
|
|
*/ |
219
|
1 |
|
public function countBy(callable $function) |
220
|
|
|
{ |
221
|
1 |
|
return countBy($this->getItems(), $function); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns a lazy collection by changing keys of this collection for each item to the result of $function for |
226
|
|
|
* that item. |
227
|
|
|
* |
228
|
|
|
* @param callable $function |
229
|
|
|
* @return Collection |
230
|
|
|
*/ |
231
|
1 |
|
public function indexBy(callable $function) |
232
|
|
|
{ |
233
|
1 |
|
return indexBy($this->getItems(), $function); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns true if $function returns true for every item in this collection, false otherwise. |
238
|
|
|
* |
239
|
|
|
* @param callable $function |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
1 |
|
public function every(callable $function) |
243
|
|
|
{ |
244
|
1 |
|
return every($this->getItems(), $function); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns true if $function returns true for at least one item in this collection, false otherwise. |
249
|
|
|
* |
250
|
|
|
* @param callable $function |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
1 |
|
public function some(callable $function) |
254
|
|
|
{ |
255
|
1 |
|
return some($this->getItems(), $function); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns true if $value is present in the collection. |
260
|
|
|
* |
261
|
|
|
* @param mixed $value |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
1 |
|
public function contains($value) |
265
|
|
|
{ |
266
|
1 |
|
return contains($this->getItems(), $value); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns collection of items in this collection in reverse order. |
271
|
|
|
* |
272
|
|
|
* @return Collection |
273
|
|
|
*/ |
274
|
1 |
|
public function reverse() |
275
|
|
|
{ |
276
|
1 |
|
return reverse($this->getItems()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Reduce the collection to single value. Walks from right to left. |
281
|
|
|
* |
282
|
|
|
* @param callable $function Must take 2 arguments, intermediate value and item from the iterator. If |
283
|
|
|
* $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
284
|
|
|
* is returned. |
285
|
|
|
* @param mixed $startValue |
286
|
|
|
* @param bool $convertToCollection |
287
|
|
|
* @return mixed|Collection |
288
|
|
|
*/ |
289
|
1 |
|
public function reduceRight(callable $function, $startValue, $convertToCollection = false) |
290
|
|
|
{ |
291
|
1 |
|
$result = reduceRight($this->getItems(), $function, $startValue); |
292
|
|
|
|
293
|
1 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* A form of slice that returns first $numberOfItems items. |
298
|
|
|
* |
299
|
|
|
* @param int $numberOfItems |
300
|
|
|
* @return Collection |
301
|
|
|
*/ |
302
|
9 |
|
public function take($numberOfItems) |
303
|
|
|
{ |
304
|
9 |
|
return take($this->getItems(), $numberOfItems); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* A form of slice that returns all but first $numberOfItems items. |
309
|
|
|
* |
310
|
|
|
* @param int $numberOfItems |
311
|
|
|
* @return Collection |
312
|
|
|
*/ |
313
|
2 |
|
public function drop($numberOfItems) |
314
|
|
|
{ |
315
|
2 |
|
return drop($this->getItems(), $numberOfItems); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns collection of values from this collection but with keys being numerical from 0 upwards. |
320
|
|
|
* |
321
|
|
|
* @return Collection |
322
|
|
|
*/ |
323
|
12 |
|
public function values() |
324
|
|
|
{ |
325
|
12 |
|
return values($this->getItems()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Returns a lazy collection without elements matched by $function. |
330
|
|
|
* |
331
|
|
|
* @param callable $function |
332
|
|
|
* @return Collection |
333
|
|
|
*/ |
334
|
1 |
|
public function reject(callable $function) |
335
|
|
|
{ |
336
|
1 |
|
return reject($this->getItems(), $function); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns a lazy collection of the keys of this collection. |
341
|
|
|
* |
342
|
|
|
* @return Collection |
343
|
|
|
*/ |
344
|
1 |
|
public function keys() |
345
|
|
|
{ |
346
|
1 |
|
return keys($this->getItems()); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Returns a lazy collection of items of this collection separated by $separator |
351
|
|
|
* |
352
|
|
|
* @param mixed $separator |
353
|
|
|
* @return Collection |
354
|
|
|
*/ |
355
|
1 |
|
public function interpose($separator) |
356
|
|
|
{ |
357
|
1 |
|
return interpose($this->getItems(), $separator); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped. |
362
|
|
|
* |
363
|
|
|
* @param int $numberOfItems |
364
|
|
|
* @return Collection |
365
|
|
|
*/ |
366
|
1 |
|
public function dropLast($numberOfItems = 1) |
367
|
|
|
{ |
368
|
1 |
|
return dropLast($this->getItems(), $numberOfItems); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Returns a lazy collection of first item from first collection, first item from second, second from first and |
373
|
|
|
* so on. Accepts any number of collections. |
374
|
|
|
* |
375
|
|
|
* @param array|\Traversable ...$collections |
376
|
|
|
* @return Collection |
377
|
|
|
*/ |
378
|
1 |
|
public function interleave(...$collections) |
379
|
|
|
{ |
380
|
1 |
|
return interleave($this->getItems(), ...$collections); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Returns an infinite lazy collection of items in this collection repeated infinitely. |
385
|
|
|
* |
386
|
|
|
* @return Collection |
387
|
|
|
*/ |
388
|
1 |
|
public function cycle() |
389
|
|
|
{ |
390
|
1 |
|
return cycle($this->getItems()); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided |
395
|
|
|
* it will be next integer index. |
396
|
|
|
* |
397
|
|
|
* @param mixed $value |
398
|
|
|
* @param mixed|null $key |
399
|
|
|
* @return Collection |
400
|
|
|
*/ |
401
|
2 |
|
public function prepend($value, $key = null) |
402
|
1 |
|
{ |
403
|
2 |
|
return prepend($this->getItems(), $value, $key); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided |
408
|
|
|
* it will be next integer index. |
409
|
|
|
* |
410
|
|
|
* @param mixed $value |
411
|
|
|
* @param mixed $key |
412
|
|
|
* @return Collection |
413
|
|
|
*/ |
414
|
8 |
|
public function append($value, $key = null) |
415
|
|
|
{ |
416
|
8 |
|
return append($this->getItems(), $value, $key); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Returns a lazy collection by removing items from this collection until first item for which $function returns |
421
|
|
|
* false. |
422
|
|
|
* |
423
|
|
|
* @param callable $function |
424
|
|
|
* @return Collection |
425
|
|
|
*/ |
426
|
1 |
|
public function dropWhile(callable $function) |
427
|
|
|
{ |
428
|
1 |
|
return dropWhile($this->getItems(), $function); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Returns a lazy collection which is a result of calling map($function) and then flatten(1) |
433
|
|
|
* |
434
|
|
|
* @param callable $function |
435
|
|
|
* @return Collection |
436
|
|
|
*/ |
437
|
1 |
|
public function mapcat(callable $function) |
438
|
|
|
{ |
439
|
1 |
|
return mapcat($this->getItems(), $function); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Returns a lazy collection of items from the start of the ollection until the first item for which $function |
444
|
|
|
* returns false. |
445
|
|
|
* |
446
|
|
|
* @param callable $function |
447
|
|
|
* @return Collection |
448
|
|
|
*/ |
449
|
1 |
|
public function takeWhile(callable $function) |
450
|
|
|
{ |
451
|
1 |
|
return takeWhile($this->getItems(), $function); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Returns a collection of [take($position), drop($position)] |
456
|
|
|
* |
457
|
|
|
* @param int $position |
458
|
|
|
* @return Collection |
459
|
|
|
*/ |
460
|
1 |
|
public function splitAt($position) |
461
|
|
|
{ |
462
|
1 |
|
return splitAt($this->getItems(), $position); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Returns a collection of [takeWhile($predicament), dropWhile($predicament] |
467
|
|
|
* |
468
|
|
|
* @param callable $function |
469
|
|
|
* @return Collection |
470
|
|
|
*/ |
471
|
1 |
|
public function splitWith(callable $function) |
472
|
|
|
{ |
473
|
1 |
|
return splitWith($this->getItems(), $function); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap |
478
|
|
|
* are replaced by their values. |
479
|
|
|
* |
480
|
|
|
* @param \Traversable|array $replacementMap |
481
|
|
|
* @return Collection |
482
|
|
|
*/ |
483
|
1 |
|
public function replace($replacementMap) |
484
|
|
|
{ |
485
|
1 |
|
return replace($this->getItems(), $replacementMap); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Returns a lazy collection of reduction steps. |
490
|
|
|
* |
491
|
|
|
* @param callable $function |
492
|
|
|
* @param mixed $startValue |
493
|
|
|
* @return Collection |
494
|
|
|
*/ |
495
|
1 |
|
public function reductions(callable $function, $startValue) |
496
|
|
|
{ |
497
|
1 |
|
return reductions($this->getItems(), $function, $startValue); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Returns a lazy collection of every nth item in this collection |
502
|
|
|
* |
503
|
|
|
* @param int $step |
504
|
|
|
* @return Collection |
505
|
|
|
*/ |
506
|
1 |
|
public function takeNth($step) |
507
|
|
|
{ |
508
|
1 |
|
return takeNth($this->getItems(), $step); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Returns a non-collection of shuffled items from this collection |
513
|
|
|
* |
514
|
|
|
* @return Collection |
515
|
|
|
*/ |
516
|
1 |
|
public function shuffle() |
517
|
|
|
{ |
518
|
1 |
|
return \DusanKasan\Knapsack\shuffle($this->getItems()); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Returns a lazy collection of collections of $numberOfItems items each, at $step step |
523
|
|
|
* apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions |
524
|
|
|
* do not overlap. If a $padding collection is supplied, use its elements as |
525
|
|
|
* necessary to complete last partition up to $numberOfItems items. In case there are |
526
|
|
|
* not enough padding elements, return a partition with less than $numberOfItems items. |
527
|
|
|
* |
528
|
|
|
* @param int $numberOfItems |
529
|
|
|
* @param int $step |
530
|
|
|
* @param array|\Traversable $padding |
531
|
|
|
* @return Collection |
532
|
|
|
*/ |
533
|
1 |
|
public function partition($numberOfItems, $step = 0, $padding = []) |
534
|
|
|
{ |
535
|
1 |
|
return partition($this->getItems(), $numberOfItems, $step, $padding); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Creates a lazy collection of collections created by partitioning this collection every time $function will |
540
|
|
|
* return different result. |
541
|
|
|
* |
542
|
|
|
* @param callable $function |
543
|
|
|
* @return Collection |
544
|
|
|
*/ |
545
|
1 |
|
public function partitionBy(callable $function) |
546
|
|
|
{ |
547
|
1 |
|
return partitionBy($this->getItems(), $function); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Returns true if this collection is empty. False otherwise. |
552
|
|
|
* |
553
|
|
|
* @return bool |
554
|
|
|
*/ |
555
|
2 |
|
public function isEmpty() |
556
|
|
|
{ |
557
|
2 |
|
return isEmpty($this->getItems()); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Opposite of isEmpty. |
562
|
|
|
* |
563
|
|
|
* @return bool |
564
|
|
|
*/ |
565
|
2 |
|
public function isNotEmpty() |
566
|
|
|
{ |
567
|
2 |
|
return isNotEmpty($this->getItems()); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Returns a collection where keys are distinct items from this collection and their values are number of |
572
|
|
|
* occurrences of each value. |
573
|
|
|
* |
574
|
|
|
* @return Collection |
575
|
|
|
*/ |
576
|
1 |
|
public function frequencies() |
577
|
1 |
|
{ |
578
|
1 |
|
return frequencies($this->getItems()); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
583
|
|
|
* is true and the return value is a collection (array|Traversable an instance of Collection is returned. |
584
|
|
|
* |
585
|
|
|
* @param bool $convertToCollection |
586
|
|
|
* @return mixed|Collection |
587
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
588
|
|
|
*/ |
589
|
11 |
|
public function first($convertToCollection = false) |
590
|
|
|
{ |
591
|
11 |
|
$result = first($this->getItems()); |
592
|
10 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
597
|
|
|
* is true and the return value is a collection (array|Traversable) it is converted to Collection. |
598
|
|
|
* |
599
|
|
|
* @param bool $convertToCollection |
600
|
|
|
* @return mixed|Collection |
601
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
602
|
|
|
*/ |
603
|
2 |
|
public function last($convertToCollection = false) |
604
|
|
|
{ |
605
|
2 |
|
$result = last($this->getItems()); |
606
|
1 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values. |
611
|
|
|
* |
612
|
|
|
* @return Collection |
613
|
|
|
*/ |
614
|
1 |
|
public function realize() |
615
|
|
|
{ |
616
|
1 |
|
return realize($this->getItems()); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item. If |
621
|
|
|
* $convertToCollection is true and the return value is a collection (array|Traversable) it is converted to |
622
|
|
|
* Collection. |
623
|
|
|
* |
624
|
|
|
* @param bool $convertToCollection |
625
|
|
|
* @return Collection|mixed |
626
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
627
|
|
|
*/ |
628
|
6 |
|
public function second($convertToCollection = false) |
629
|
|
|
{ |
630
|
6 |
|
$result = second($this->getItems()); |
631
|
5 |
|
return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Combines the values of this collection as keys, with values of $collection as values. The resulting collection |
636
|
|
|
* has length equal to the size of smaller collection. |
637
|
|
|
* |
638
|
|
|
* @param array|\Traversable $collection |
639
|
|
|
* @return Collection |
640
|
|
|
* @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
641
|
|
|
*/ |
642
|
1 |
|
public function combine($collection) |
643
|
|
|
{ |
644
|
1 |
|
return combine($this->getItems(), $collection); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Returns a lazy collection without the items associated to any of the keys from $keys. |
649
|
|
|
* |
650
|
|
|
* @param array|\Traversable $keys |
651
|
|
|
* @return Collection |
652
|
|
|
*/ |
653
|
1 |
|
public function except($keys) |
654
|
|
|
{ |
655
|
1 |
|
return except($this->getItems(), $keys); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Returns a lazy collection of items associated to any of the keys from $keys. |
660
|
|
|
* |
661
|
|
|
* @param array|\Traversable $keys |
662
|
|
|
* @return Collection |
663
|
|
|
*/ |
664
|
1 |
|
public function only($keys) |
665
|
|
|
{ |
666
|
1 |
|
return only($this->getItems(), $keys); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the |
671
|
|
|
* ...$collections are iterated non-lazily. |
672
|
|
|
* |
673
|
|
|
* @param array|\Traversable ...$collections |
674
|
|
|
* @return Collection |
675
|
|
|
*/ |
676
|
1 |
|
public function diff(...$collections) |
677
|
|
|
{ |
678
|
1 |
|
return diff($this->getItems(), ...$collections); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Returns a lazy collection where keys and values are flipped. |
684
|
|
|
* |
685
|
|
|
* @return Collection |
686
|
|
|
*/ |
687
|
1 |
|
public function flip() |
688
|
|
|
{ |
689
|
1 |
|
return flip($this->getItems()); |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Checks for the existence of an item with$key in this collection. |
694
|
|
|
* |
695
|
|
|
* @param mixed $key |
696
|
|
|
* @return bool |
697
|
|
|
*/ |
698
|
1 |
|
public function has($key) |
699
|
|
|
{ |
700
|
1 |
|
return has($this->getItems(), $key); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Returns a lazy collection of non-lazy collections of items from nth position from this collection and each |
706
|
|
|
* passed collection. Stops when any of the collections don't have an item at the nth position. |
707
|
|
|
* |
708
|
|
|
* @param array|\Traversable[] ...$collections |
709
|
|
|
* @return Collection |
710
|
|
|
*/ |
711
|
1 |
|
public function zip(...$collections) |
712
|
|
|
{ |
713
|
1 |
|
array_unshift($collections, $this->getItems()); |
714
|
1 |
|
return zip(...$collections); |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Uses a $transformer callable that takes a Collection and returns Collection on itself. |
719
|
|
|
* |
720
|
|
|
* @param callable $transformer Collection => Collection |
721
|
|
|
* @return Collection |
722
|
|
|
* @throws InvalidReturnValue |
723
|
|
|
*/ |
724
|
1 |
|
public function transform(callable $transformer) |
725
|
|
|
{ |
726
|
1 |
|
$items = $this->getItems(); |
727
|
|
|
|
728
|
1 |
|
$transformed = $transformer($items instanceof Collection ? $items : new Collection($items)); |
729
|
|
|
|
730
|
1 |
|
if (!($transformed instanceof Collection)) { |
731
|
1 |
|
throw new InvalidReturnValue; |
732
|
|
|
} |
733
|
|
|
|
734
|
1 |
|
return $transformed; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* Extracts data from collection items by dot separated key path. Supports the * wildcard. If a key contains \ or |
739
|
|
|
* it must be escaped using \ character. |
740
|
|
|
* |
741
|
|
|
* @param mixed $keyPath |
742
|
|
|
* @return Collection |
743
|
|
|
*/ |
744
|
1 |
|
public function extract($keyPath) |
745
|
|
|
{ |
746
|
1 |
|
return \DusanKasan\Knapsack\extract($this->getItems(), $keyPath); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Returns a lazy collection of items that are in $collection and all the other arguments, indexed by the keys from |
751
|
|
|
* the first collection. Note that the ...$collections are iterated non-lazily. |
752
|
|
|
* |
753
|
|
|
* @param array|\Traversable[] ...$collections |
754
|
|
|
* @return Collection |
755
|
|
|
*/ |
756
|
1 |
|
public function intersect(...$collections) |
757
|
|
|
{ |
758
|
1 |
|
return intersect($this->getItems(), ...$collections); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* @return array|\Traversable |
763
|
|
|
*/ |
764
|
79 |
|
protected function getItems() |
765
|
|
|
{ |
766
|
79 |
|
return $this; |
767
|
|
|
} |
768
|
|
|
} |
769
|
|
|
|