1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\collection; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Countable; |
9
|
|
|
use Doctrine\Common\Collections\Criteria; |
10
|
|
|
use Generator; |
11
|
|
|
use JsonSerializable; |
12
|
|
|
use loophp\collection\Contract\Collection as CollectionInterface; |
13
|
|
|
use loophp\collection\Contract\Operation as OperationInterface; |
14
|
|
|
use loophp\collection\Utils\CallbacksArrayReducer; |
15
|
|
|
use loophp\iterators\ClosureIteratorAggregate; |
16
|
|
|
use loophp\iterators\IterableIteratorAggregate; |
17
|
|
|
use loophp\iterators\ResourceIteratorAggregate; |
18
|
|
|
use loophp\iterators\StringIteratorAggregate; |
19
|
|
|
use NoRewindIterator; |
20
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
21
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
22
|
|
|
use Traversable; |
23
|
|
|
|
24
|
|
|
use const INF; |
25
|
|
|
use const PHP_INT_MAX; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @immutable |
29
|
|
|
* |
30
|
|
|
* @template TKey |
31
|
|
|
* @template T |
32
|
|
|
* |
33
|
|
|
* @implements \loophp\collection\Contract\Collection<TKey, T> |
34
|
|
|
*/ |
35
|
|
|
final class Collection implements CollectionInterface, JsonSerializable, Countable |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var ClosureIteratorAggregate<TKey, T> |
39
|
|
|
*/ |
40
|
|
|
private ClosureIteratorAggregate $innerIterator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param callable(mixed ...$parameters): iterable<TKey, T> $callable |
44
|
|
|
* @param iterable<int, mixed> $parameters |
45
|
|
|
*/ |
46
|
717 |
|
private function __construct(callable $callable, iterable $parameters = []) |
47
|
|
|
{ |
48
|
717 |
|
$this->innerIterator = new ClosureIteratorAggregate($callable, $parameters); |
49
|
|
|
} |
50
|
|
|
|
51
|
25 |
|
public function all(bool $normalize = true): array |
52
|
|
|
{ |
53
|
25 |
|
return iterator_to_array((new Operation\All())()($normalize)($this)); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function append(mixed ...$items): CollectionInterface |
57
|
|
|
{ |
58
|
2 |
|
return new self((new Operation\Append())()($items), [$this]); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function apply(callable ...$callbacks): CollectionInterface |
62
|
|
|
{ |
63
|
4 |
|
return new self((new Operation\Apply())()(...$callbacks), [$this]); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function associate( |
67
|
|
|
?callable $callbackForKeys = null, |
68
|
|
|
?callable $callbackForValues = null |
69
|
|
|
): CollectionInterface { |
70
|
4 |
|
$defaultCallback = static fn (mixed $carry): mixed => $carry; |
71
|
|
|
|
72
|
4 |
|
return new self((new Operation\Associate())()($callbackForKeys ?? $defaultCallback)($callbackForValues ?? $defaultCallback), [$this]); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function asyncMap(callable $callback): CollectionInterface |
76
|
|
|
{ |
77
|
2 |
|
return new self((new Operation\AsyncMap())()($callback), [$this]); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function asyncMapN(callable ...$callbacks): CollectionInterface |
81
|
|
|
{ |
82
|
2 |
|
return new self((new Operation\AsyncMapN())()(...$callbacks), [$this]); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
public function averages(): CollectionInterface |
86
|
|
|
{ |
87
|
4 |
|
return new self((new Operation\Averages())(), [$this]); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
public function cache(?CacheItemPoolInterface $cache = null): CollectionInterface |
91
|
|
|
{ |
92
|
3 |
|
return new self((new Operation\Cache())()($cache ?? new ArrayAdapter()), [$this]); |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
public function chunk(int ...$sizes): CollectionInterface |
96
|
|
|
{ |
97
|
8 |
|
return new self((new Operation\Chunk())()(...$sizes), [$this]); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
public function coalesce(): CollectionInterface |
101
|
|
|
{ |
102
|
4 |
|
return new self((new Operation\Coalesce())(), [$this]); |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
public function collapse(): CollectionInterface |
106
|
|
|
{ |
107
|
4 |
|
return new self((new Operation\Collapse())(), [$this]); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
public function column(mixed $column): CollectionInterface |
111
|
|
|
{ |
112
|
6 |
|
return new self((new Operation\Column())()($column), [$this]); |
113
|
|
|
} |
114
|
|
|
|
115
|
6 |
|
public function combinate(?int $length = null): CollectionInterface |
116
|
|
|
{ |
117
|
6 |
|
return new self((new Operation\Combinate())()($length), [$this]); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
public function combine(mixed ...$keys): CollectionInterface |
121
|
|
|
{ |
122
|
6 |
|
return new self((new Operation\Combine())()($keys), [$this]); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
public function compact(mixed ...$values): CollectionInterface |
126
|
|
|
{ |
127
|
4 |
|
return new self((new Operation\Compact())()($values), [$this]); |
128
|
|
|
} |
129
|
|
|
|
130
|
10 |
|
public function compare(callable $comparator, $default = null) |
131
|
|
|
{ |
132
|
10 |
|
return (new self((new Operation\Compare())()($comparator), [$this]))->current(0, $default); |
133
|
|
|
} |
134
|
|
|
|
135
|
10 |
|
public function contains(mixed ...$values): bool |
136
|
|
|
{ |
137
|
10 |
|
return (new Operation\Contains())()($values)($this)->current(); |
138
|
|
|
} |
139
|
|
|
|
140
|
11 |
|
public function count(): int |
141
|
|
|
{ |
142
|
11 |
|
return iterator_count($this); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
public function countIn(int &$counter): CollectionInterface |
146
|
|
|
{ |
147
|
2 |
|
return new self( |
148
|
2 |
|
(new Operation\Apply())()( |
149
|
2 |
|
static function () use (&$counter): void { |
150
|
1 |
|
++$counter; |
151
|
2 |
|
} |
152
|
2 |
|
), |
153
|
2 |
|
[$this] |
154
|
2 |
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
100 |
|
public function current(int $index = 0, $default = null) |
158
|
|
|
{ |
159
|
100 |
|
return (new Operation\Current())()($index)($default)($this)->current(); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
public function cycle(): CollectionInterface |
163
|
|
|
{ |
164
|
4 |
|
return new self((new Operation\Cycle())(), [$this]); |
165
|
|
|
} |
166
|
|
|
|
167
|
14 |
|
public function diff(mixed ...$values): CollectionInterface |
168
|
|
|
{ |
169
|
14 |
|
return new self((new Operation\Diff())()($values), [$this]); |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
public function diffKeys(mixed ...$keys): CollectionInterface |
173
|
|
|
{ |
174
|
4 |
|
return new self((new Operation\DiffKeys())()($keys), [$this]); |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
public function dispersion(): CollectionInterface |
178
|
|
|
{ |
179
|
4 |
|
return new self((new Operation\Dispersion())(), [$this]); |
180
|
|
|
} |
181
|
|
|
|
182
|
12 |
|
public function distinct(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface |
183
|
|
|
{ |
184
|
12 |
|
$accessorCallback ??= |
185
|
|
|
/** |
186
|
|
|
* @param T $value |
|
|
|
|
187
|
|
|
* @param TKey $key |
|
|
|
|
188
|
|
|
* |
189
|
|
|
* @return T |
190
|
|
|
*/ |
191
|
12 |
|
static fn (mixed $value, mixed $key): mixed => $value; |
|
|
|
|
192
|
|
|
|
193
|
12 |
|
$comparatorCallback ??= |
194
|
|
|
/** |
195
|
|
|
* @param T $left |
196
|
|
|
* |
197
|
|
|
* @return Closure(T): bool |
198
|
|
|
*/ |
199
|
12 |
|
static fn (mixed $left): Closure => |
200
|
|
|
/** |
201
|
|
|
* @param T $right |
202
|
|
|
*/ |
203
|
8 |
|
static fn (mixed $right): bool => $left === $right; |
204
|
|
|
|
205
|
12 |
|
return new self((new Operation\Distinct())()($comparatorCallback)($accessorCallback), [$this]); |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
public function drop(int $count): CollectionInterface |
209
|
|
|
{ |
210
|
4 |
|
return new self((new Operation\Drop())()($count), [$this]); |
211
|
|
|
} |
212
|
|
|
|
213
|
4 |
|
public function dropWhile(callable ...$callbacks): CollectionInterface |
214
|
|
|
{ |
215
|
4 |
|
return new self((new Operation\DropWhile())()(...$callbacks), [$this]); |
216
|
|
|
} |
217
|
|
|
|
218
|
4 |
|
public function dump(string $name = '', int $size = 1, ?Closure $closure = null): CollectionInterface |
219
|
|
|
{ |
220
|
4 |
|
return new self((new Operation\Dump())()($name)($size)($closure), [$this]); |
221
|
|
|
} |
222
|
|
|
|
223
|
10 |
|
public function duplicate(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface |
224
|
|
|
{ |
225
|
10 |
|
$accessorCallback ??= |
226
|
|
|
/** |
227
|
|
|
* @param T $value |
228
|
|
|
* @param TKey $key |
229
|
|
|
* |
230
|
|
|
* @return T |
231
|
|
|
*/ |
232
|
10 |
|
static fn (mixed $value, mixed $key): mixed => $value; |
|
|
|
|
233
|
|
|
|
234
|
10 |
|
$comparatorCallback ??= |
235
|
|
|
/** |
236
|
|
|
* @param T $left |
237
|
|
|
* |
238
|
|
|
* @return Closure(T): bool |
239
|
|
|
*/ |
240
|
10 |
|
static fn (mixed $left): Closure => |
241
|
|
|
/** |
242
|
|
|
* @param T $right |
243
|
|
|
*/ |
244
|
6 |
|
static fn (mixed $right): bool => $left === $right; |
245
|
|
|
|
246
|
10 |
|
return new self((new Operation\Duplicate())()($comparatorCallback)($accessorCallback), [$this]); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @template UKey |
251
|
|
|
* @template U |
252
|
|
|
* |
253
|
|
|
* @return CollectionInterface<UKey, U>&self<UKey, U> |
254
|
|
|
*/ |
255
|
4 |
|
public static function empty(): CollectionInterface |
256
|
|
|
{ |
257
|
4 |
|
return self::fromIterable([]); |
258
|
|
|
} |
259
|
|
|
|
260
|
4 |
|
public function entropy(): CollectionInterface |
261
|
|
|
{ |
262
|
4 |
|
return new self((new Operation\Entropy())(), [$this]); |
263
|
|
|
} |
264
|
|
|
|
265
|
36 |
|
public function equals(iterable $other): bool |
266
|
|
|
{ |
267
|
36 |
|
return (new Operation\Equals())()($other)($this)->current(); |
268
|
|
|
} |
269
|
|
|
|
270
|
16 |
|
public function every(callable ...$callbacks): bool |
271
|
|
|
{ |
272
|
16 |
|
return (new Operation\Every())()(static fn (int $index, mixed $value, mixed $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))($this) |
273
|
16 |
|
->current(); |
274
|
|
|
} |
275
|
|
|
|
276
|
2 |
|
public function explode(mixed ...$explodes): CollectionInterface |
277
|
|
|
{ |
278
|
2 |
|
return new self((new Operation\Explode())()($explodes), [$this]); |
279
|
|
|
} |
280
|
|
|
|
281
|
8 |
|
public function falsy(): bool |
282
|
|
|
{ |
283
|
8 |
|
return (new Operation\Falsy())()($this)->current(); |
284
|
|
|
} |
285
|
|
|
|
286
|
8 |
|
public function filter(callable ...$callbacks): CollectionInterface |
287
|
|
|
{ |
288
|
8 |
|
return new self((new Operation\Filter())()(...$callbacks), [$this]); |
289
|
|
|
} |
290
|
|
|
|
291
|
10 |
|
public function find(mixed $default = null, callable ...$callbacks) |
292
|
|
|
{ |
293
|
10 |
|
return (new Operation\Find())()($default)(...$callbacks)($this)->current(); |
294
|
|
|
} |
295
|
|
|
|
296
|
10 |
|
public function first(mixed $default = null) |
297
|
|
|
{ |
298
|
10 |
|
return (new self((new Operation\First())(), [$this]))->current(0, $default); |
299
|
|
|
} |
300
|
|
|
|
301
|
14 |
|
public function flatMap(callable $callback): CollectionInterface |
302
|
|
|
{ |
303
|
14 |
|
return new self((new Operation\FlatMap())()($callback), [$this]); |
304
|
|
|
} |
305
|
|
|
|
306
|
6 |
|
public function flatten(int $depth = PHP_INT_MAX): CollectionInterface |
307
|
|
|
{ |
308
|
6 |
|
return new self((new Operation\Flatten())()($depth), [$this]); |
309
|
|
|
} |
310
|
|
|
|
311
|
4 |
|
public function flip(): CollectionInterface |
312
|
|
|
{ |
313
|
4 |
|
return new self((new Operation\Flip())(), [$this]); |
314
|
|
|
} |
315
|
|
|
|
316
|
8 |
|
public function foldLeft(callable $callback, mixed $initial) |
317
|
|
|
{ |
318
|
8 |
|
return (new self((new Operation\FoldLeft())()($callback)($initial), [$this]))->current(); |
319
|
|
|
} |
320
|
|
|
|
321
|
4 |
|
public function foldLeft1(callable $callback): mixed |
322
|
|
|
{ |
323
|
4 |
|
return (new self((new Operation\FoldLeft1())()($callback), [$this]))->current(); |
324
|
|
|
} |
325
|
|
|
|
326
|
2 |
|
public function foldRight(callable $callback, mixed $initial): mixed |
327
|
|
|
{ |
328
|
2 |
|
return (new self((new Operation\FoldRight())()($callback)($initial), [$this]))->current(); |
329
|
|
|
} |
330
|
|
|
|
331
|
4 |
|
public function foldRight1(callable $callback): mixed |
332
|
|
|
{ |
333
|
4 |
|
return (new self((new Operation\FoldRight1())()($callback), [$this]))->current(); |
334
|
|
|
} |
335
|
|
|
|
336
|
2 |
|
public function forget(mixed ...$keys): CollectionInterface |
337
|
|
|
{ |
338
|
2 |
|
return new self((new Operation\Forget())()($keys), [$this]); |
339
|
|
|
} |
340
|
|
|
|
341
|
5 |
|
public function frequency(): CollectionInterface |
342
|
|
|
{ |
343
|
5 |
|
return new self((new Operation\Frequency())(), [$this]); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @template NewTKey |
348
|
|
|
* @template NewT |
349
|
|
|
* |
350
|
|
|
* @param callable(mixed ...$parameters): iterable<NewTKey, NewT> $callable |
351
|
|
|
* @param iterable<int, mixed> $parameters |
352
|
|
|
* |
353
|
|
|
* @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT> |
354
|
|
|
*/ |
355
|
3 |
|
public static function fromCallable(callable $callable, iterable $parameters = []): CollectionInterface |
356
|
|
|
{ |
357
|
3 |
|
return new self($callable, $parameters); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param null|non-negative-int $length |
|
|
|
|
362
|
|
|
* |
363
|
|
|
* @return CollectionInterface<int, string>&self<int, string> |
364
|
|
|
*/ |
365
|
2 |
|
public static function fromFile(string $filepath, ?int $length = 2): CollectionInterface |
366
|
|
|
{ |
367
|
2 |
|
return new self( |
368
|
2 |
|
static fn (): Generator => yield from new ResourceIteratorAggregate(fopen($filepath, 'rb'), true, $length), |
369
|
2 |
|
); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @template NewTKey |
374
|
|
|
* @template NewT |
375
|
|
|
* |
376
|
|
|
* @param Generator<NewTKey, NewT> $generator |
377
|
|
|
* |
378
|
|
|
* @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT> |
379
|
|
|
*/ |
380
|
2 |
|
public static function fromGenerator(Generator $generator): CollectionInterface |
381
|
|
|
{ |
382
|
2 |
|
return new self(static fn (): Generator => yield from new NoRewindIterator($generator)); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @template UKey |
387
|
|
|
* @template U |
388
|
|
|
* |
389
|
|
|
* @param iterable<UKey, U> $iterable |
390
|
|
|
* |
391
|
|
|
* @return CollectionInterface<UKey, U>&self<UKey, U> |
392
|
|
|
*/ |
393
|
700 |
|
public static function fromIterable(iterable $iterable): CollectionInterface |
394
|
|
|
{ |
395
|
700 |
|
return new self(static fn (): Generator => yield from new IterableIteratorAggregate($iterable)); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param resource $resource |
400
|
|
|
* |
401
|
|
|
* @return CollectionInterface<int, string>&self<int, string> |
402
|
|
|
*/ |
403
|
2 |
|
public static function fromResource($resource): CollectionInterface |
404
|
|
|
{ |
405
|
2 |
|
return new self(static fn (): Generator => yield from new ResourceIteratorAggregate($resource)); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @return CollectionInterface<int, string>&self<int, string> |
410
|
|
|
*/ |
411
|
2 |
|
public static function fromString(string $string, string $delimiter = ''): CollectionInterface |
412
|
|
|
{ |
413
|
2 |
|
return new self(static fn (): Generator => yield from new StringIteratorAggregate($string, $delimiter)); |
414
|
|
|
} |
415
|
|
|
|
416
|
5 |
|
public function get(mixed $key, mixed $default = null) |
417
|
|
|
{ |
418
|
5 |
|
return (new self((new Operation\Get())()($key)($default), [$this]))->current(0, $default); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @return Traversable<TKey, T> |
423
|
|
|
*/ |
424
|
717 |
|
public function getIterator(): Traversable |
425
|
|
|
{ |
426
|
717 |
|
yield from $this->innerIterator->getIterator(); |
427
|
|
|
} |
428
|
|
|
|
429
|
6 |
|
public function group(): CollectionInterface |
430
|
|
|
{ |
431
|
6 |
|
return new self((new Operation\Group())(), [$this]); |
432
|
|
|
} |
433
|
|
|
|
434
|
4 |
|
public function groupBy(callable $callback): CollectionInterface |
435
|
|
|
{ |
436
|
4 |
|
return new self((new Operation\GroupBy())()($callback), [$this]); |
437
|
|
|
} |
438
|
|
|
|
439
|
16 |
|
public function has(callable ...$callbacks): bool |
440
|
|
|
{ |
441
|
16 |
|
return (new Operation\Has())()(...$callbacks)($this)->current(); |
442
|
|
|
} |
443
|
|
|
|
444
|
6 |
|
public function head(mixed $default = null) |
445
|
|
|
{ |
446
|
6 |
|
return (new self((new Operation\Head())(), [$this]))->current(0, $default); |
447
|
|
|
} |
448
|
|
|
|
449
|
4 |
|
public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface |
450
|
|
|
{ |
451
|
4 |
|
$identity = |
452
|
|
|
/** |
453
|
|
|
* @param T $value |
454
|
|
|
* |
455
|
|
|
* @return T |
456
|
|
|
*/ |
457
|
4 |
|
static fn (mixed $value): mixed => $value; |
458
|
|
|
|
459
|
4 |
|
return new self((new Operation\IfThenElse())()($condition)($then)($else ?? $identity), [$this]); |
460
|
|
|
} |
461
|
|
|
|
462
|
6 |
|
public function implode(string $glue = ''): string |
463
|
|
|
{ |
464
|
6 |
|
return (new self((new Operation\Implode())()($glue), [$this]))->current(0, ''); |
465
|
|
|
} |
466
|
|
|
|
467
|
4 |
|
public function init(): CollectionInterface |
468
|
|
|
{ |
469
|
4 |
|
return new self((new Operation\Init())(), [$this]); |
470
|
|
|
} |
471
|
|
|
|
472
|
4 |
|
public function inits(): CollectionInterface |
473
|
|
|
{ |
474
|
4 |
|
return new self((new Operation\Inits())(), [$this]); |
475
|
|
|
} |
476
|
|
|
|
477
|
4 |
|
public function intersect(mixed ...$values): CollectionInterface |
478
|
|
|
{ |
479
|
4 |
|
return new self((new Operation\Intersect())()($values), [$this]); |
480
|
|
|
} |
481
|
|
|
|
482
|
6 |
|
public function intersectKeys(mixed ...$keys): CollectionInterface |
483
|
|
|
{ |
484
|
6 |
|
return new self((new Operation\IntersectKeys())()($keys), [$this]); |
485
|
|
|
} |
486
|
|
|
|
487
|
10 |
|
public function intersperse(mixed $element, int $every = 1, int $startAt = 0): CollectionInterface |
488
|
|
|
{ |
489
|
10 |
|
return new self((new Operation\Intersperse())()($element)($every)($startAt), [$this]); |
490
|
|
|
} |
491
|
|
|
|
492
|
14 |
|
public function isEmpty(): bool |
493
|
|
|
{ |
494
|
14 |
|
return (new Operation\IsEmpty())()($this)->current(); |
495
|
|
|
} |
496
|
|
|
|
497
|
14 |
|
public function isNotEmpty(): bool |
498
|
|
|
{ |
499
|
14 |
|
return (new Operation\IsNotEmpty())()($this)->current(); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @return array<mixed> |
504
|
|
|
*/ |
505
|
1 |
|
public function jsonSerialize(): array |
506
|
|
|
{ |
507
|
1 |
|
return $this->all(false); |
508
|
|
|
} |
509
|
|
|
|
510
|
6 |
|
public function key(int $index = 0) |
511
|
|
|
{ |
512
|
6 |
|
return (new Operation\Key())()($index)($this)->current(); |
513
|
|
|
} |
514
|
|
|
|
515
|
2 |
|
public function keys(): CollectionInterface |
516
|
|
|
{ |
517
|
2 |
|
return new self((new Operation\Keys())(), [$this]); |
518
|
|
|
} |
519
|
|
|
|
520
|
14 |
|
public function last(mixed $default = null) |
521
|
|
|
{ |
522
|
14 |
|
return (new self((new Operation\Last())(), [$this]))->current(0, $default); |
523
|
|
|
} |
524
|
|
|
|
525
|
15 |
|
public function limit(int $count = -1, int $offset = 0): CollectionInterface |
526
|
|
|
{ |
527
|
15 |
|
return new self((new Operation\Limit())()($count)($offset), [$this]); |
528
|
|
|
} |
529
|
|
|
|
530
|
2 |
|
public function lines(): CollectionInterface |
531
|
|
|
{ |
532
|
2 |
|
return new self((new Operation\Lines())(), [$this]); |
533
|
|
|
} |
534
|
|
|
|
535
|
13 |
|
public function map(callable $callback): CollectionInterface |
536
|
|
|
{ |
537
|
13 |
|
return new self((new Operation\Map())()($callback), [$this]); |
538
|
|
|
} |
539
|
|
|
|
540
|
4 |
|
public function mapN(callable ...$callbacks): CollectionInterface |
541
|
|
|
{ |
542
|
4 |
|
return new self((new Operation\MapN())()(...$callbacks), [$this]); |
543
|
|
|
} |
544
|
|
|
|
545
|
6 |
|
public function match(callable $callback, ?callable $matcher = null): bool |
546
|
|
|
{ |
547
|
6 |
|
return (new Operation\MatchOne())()($matcher ?? static fn (): bool => true)($callback)($this)->current(); |
548
|
|
|
} |
549
|
|
|
|
550
|
2 |
|
public function matching(Criteria $criteria): CollectionInterface |
551
|
|
|
{ |
552
|
2 |
|
return new self((new Operation\Matching())()($criteria), [$this]); |
553
|
|
|
} |
554
|
|
|
|
555
|
8 |
|
public function max(mixed $default = null) |
556
|
|
|
{ |
557
|
8 |
|
return (new self((new Operation\Max())(), [$this]))->current(0, $default); |
558
|
|
|
} |
559
|
|
|
|
560
|
2 |
|
public function merge(iterable ...$sources): CollectionInterface |
561
|
|
|
{ |
562
|
2 |
|
return new self((new Operation\Merge())()(...$sources), [$this]); |
563
|
|
|
} |
564
|
|
|
|
565
|
8 |
|
public function min(mixed $default = null) |
566
|
|
|
{ |
567
|
8 |
|
return (new self((new Operation\Min())(), [$this]))->current(0, $default); |
568
|
|
|
} |
569
|
|
|
|
570
|
10 |
|
public function normalize(): CollectionInterface |
571
|
|
|
{ |
572
|
10 |
|
return new self((new Operation\Normalize())(), [$this]); |
573
|
|
|
} |
574
|
|
|
|
575
|
4 |
|
public function nth(int $step, int $offset = 0): CollectionInterface |
576
|
|
|
{ |
577
|
4 |
|
return new self((new Operation\Nth())()($step)($offset), [$this]); |
578
|
|
|
} |
579
|
|
|
|
580
|
8 |
|
public function nullsy(): bool |
581
|
|
|
{ |
582
|
8 |
|
return (new Operation\Nullsy())()($this)->current(); |
583
|
|
|
} |
584
|
|
|
|
585
|
10 |
|
public function pack(): CollectionInterface |
586
|
|
|
{ |
587
|
10 |
|
return new self((new Operation\Pack())(), [$this]); |
588
|
|
|
} |
589
|
|
|
|
590
|
2 |
|
public function pad(int $size, mixed $value): CollectionInterface |
591
|
|
|
{ |
592
|
2 |
|
return new self((new Operation\Pad())()($size)($value), [$this]); |
593
|
|
|
} |
594
|
|
|
|
595
|
4 |
|
public function pair(): CollectionInterface |
596
|
|
|
{ |
597
|
4 |
|
return new self((new Operation\Pair())(), [$this]); |
598
|
|
|
} |
599
|
|
|
|
600
|
2 |
|
public function partition(callable ...$callbacks): CollectionInterface |
601
|
|
|
{ |
602
|
2 |
|
return (new self((new Operation\Partition())()(...$callbacks), [$this])) |
603
|
2 |
|
->map( |
604
|
|
|
/** |
605
|
|
|
* @param iterable<TKey, T> $iterable |
606
|
|
|
* |
607
|
|
|
* @return Collection<TKey, T> |
608
|
|
|
*/ |
609
|
2 |
|
static fn (iterable $iterable): Collection => Collection::fromIterable($iterable) |
610
|
2 |
|
); |
611
|
|
|
} |
612
|
|
|
|
613
|
2 |
|
public function permutate(): CollectionInterface |
614
|
|
|
{ |
615
|
2 |
|
return new self((new Operation\Permutate())(), [$this]); |
616
|
|
|
} |
617
|
|
|
|
618
|
2 |
|
public function pipe(callable ...$callbacks): CollectionInterface |
619
|
|
|
{ |
620
|
2 |
|
return new self((new Operation\Pipe())()(...$callbacks), [$this]); |
621
|
|
|
} |
622
|
|
|
|
623
|
12 |
|
public function pluck(mixed $pluck, mixed $default = null): CollectionInterface |
624
|
|
|
{ |
625
|
12 |
|
return new self((new Operation\Pluck())()($pluck)($default), [$this]); |
626
|
|
|
} |
627
|
|
|
|
628
|
2 |
|
public function prepend(mixed ...$items): CollectionInterface |
629
|
|
|
{ |
630
|
2 |
|
return new self((new Operation\Prepend())()($items), [$this]); |
631
|
|
|
} |
632
|
|
|
|
633
|
4 |
|
public function product(iterable ...$iterables): CollectionInterface |
634
|
|
|
{ |
635
|
4 |
|
return new self((new Operation\Product())()(...$iterables), [$this]); |
636
|
|
|
} |
637
|
|
|
|
638
|
2 |
|
public function random(int $size = 1, ?int $seed = null): CollectionInterface |
639
|
|
|
{ |
640
|
2 |
|
return new self((new Operation\Random())()($seed ?? random_int(0, 1000))($size), [$this]); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* @return self<int, float> |
645
|
|
|
*/ |
646
|
2 |
|
public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): CollectionInterface |
647
|
|
|
{ |
648
|
2 |
|
return new self((new Operation\Range())()($start)($end)($step)); |
649
|
|
|
} |
650
|
|
|
|
651
|
9 |
|
public function reduce(callable $callback, mixed $initial = null) |
652
|
|
|
{ |
653
|
9 |
|
return (new self((new Operation\Reduce())()($callback)($initial), [$this]))->current(); |
654
|
|
|
} |
655
|
|
|
|
656
|
2 |
|
public function reduction(callable $callback, mixed $initial = null): CollectionInterface |
657
|
|
|
{ |
658
|
2 |
|
return new self((new Operation\Reduction())()($callback)($initial), [$this]); |
659
|
|
|
} |
660
|
|
|
|
661
|
8 |
|
public function reject(callable ...$callbacks): CollectionInterface |
662
|
|
|
{ |
663
|
8 |
|
return new self((new Operation\Reject())()(...$callbacks), [$this]); |
664
|
|
|
} |
665
|
|
|
|
666
|
4 |
|
public function reverse(): CollectionInterface |
667
|
|
|
{ |
668
|
4 |
|
return new self((new Operation\Reverse())(), [$this]); |
669
|
|
|
} |
670
|
|
|
|
671
|
2 |
|
public function rsample(float $probability): CollectionInterface |
672
|
|
|
{ |
673
|
2 |
|
return new self((new Operation\RSample())()($probability), [$this]); |
674
|
|
|
} |
675
|
|
|
|
676
|
46 |
|
public function same(iterable $other, ?callable $comparatorCallback = null): bool |
677
|
|
|
{ |
678
|
46 |
|
$comparatorCallback ??= |
679
|
|
|
/** |
680
|
|
|
* @param T $leftValue |
681
|
|
|
* @param TKey $leftKey |
682
|
|
|
* |
683
|
|
|
* @return Closure(T, TKey): bool |
684
|
|
|
*/ |
685
|
46 |
|
static fn (mixed $leftValue, mixed $leftKey): Closure => |
686
|
|
|
/** |
687
|
|
|
* @param T $rightValue |
688
|
|
|
* @param TKey $rightKey |
689
|
|
|
*/ |
690
|
32 |
|
static fn (mixed $rightValue, mixed $rightKey): bool => $leftValue === $rightValue && $leftKey === $rightKey; |
691
|
|
|
|
692
|
46 |
|
return (new Operation\Same())()($other)($comparatorCallback)($this)->current(); |
693
|
|
|
} |
694
|
|
|
|
695
|
2 |
|
public function scale( |
696
|
|
|
float $lowerBound, |
697
|
|
|
float $upperBound, |
698
|
|
|
float $wantedLowerBound = 0.0, |
699
|
|
|
float $wantedUpperBound = 1.0, |
700
|
|
|
float $base = 0.0 |
701
|
|
|
): CollectionInterface { |
702
|
2 |
|
return new self((new Operation\Scale())()($lowerBound)($upperBound)($wantedLowerBound)($wantedUpperBound)($base), [$this]); |
703
|
|
|
} |
704
|
|
|
|
705
|
4 |
|
public function scanLeft(callable $callback, mixed $initial): CollectionInterface |
706
|
|
|
{ |
707
|
4 |
|
return new self((new Operation\ScanLeft())()($callback)($initial), [$this]); |
708
|
|
|
} |
709
|
|
|
|
710
|
6 |
|
public function scanLeft1(callable $callback): CollectionInterface |
711
|
|
|
{ |
712
|
6 |
|
return new self((new Operation\ScanLeft1())()($callback), [$this]); |
713
|
|
|
} |
714
|
|
|
|
715
|
4 |
|
public function scanRight(callable $callback, mixed $initial): CollectionInterface |
716
|
|
|
{ |
717
|
4 |
|
return new self((new Operation\ScanRight())()($callback)($initial), [$this]); |
718
|
|
|
} |
719
|
|
|
|
720
|
4 |
|
public function scanRight1(callable $callback): CollectionInterface |
721
|
|
|
{ |
722
|
4 |
|
return new self((new Operation\ScanRight1())()($callback), [$this]); |
723
|
|
|
} |
724
|
|
|
|
725
|
2 |
|
public function shuffle(?int $seed = null): CollectionInterface |
726
|
|
|
{ |
727
|
2 |
|
return new self((new Operation\Shuffle())()($seed ?? random_int(0, 1000)), [$this]); |
728
|
|
|
} |
729
|
|
|
|
730
|
8 |
|
public function since(callable ...$callbacks): CollectionInterface |
731
|
|
|
{ |
732
|
8 |
|
return new self((new Operation\Since())()(...$callbacks), [$this]); |
733
|
|
|
} |
734
|
|
|
|
735
|
6 |
|
public function slice(int $offset, int $length = -1): CollectionInterface |
736
|
|
|
{ |
737
|
6 |
|
return new self((new Operation\Slice())()($offset)($length), [$this]); |
738
|
|
|
} |
739
|
|
|
|
740
|
12 |
|
public function sort(int $type = OperationInterface\Sortable::BY_VALUES, ?callable $callback = null): CollectionInterface |
741
|
|
|
{ |
742
|
12 |
|
return new self((new Operation\Sort())()($type)($callback), [$this]); |
743
|
|
|
} |
744
|
|
|
|
745
|
2 |
|
public function span(callable ...$callbacks): CollectionInterface |
746
|
|
|
{ |
747
|
2 |
|
return (new self((new Operation\Span())()(...$callbacks), [$this])) |
748
|
2 |
|
->map( |
749
|
|
|
/** |
750
|
|
|
* @param iterable<TKey, T> $iterable |
751
|
|
|
* |
752
|
|
|
* @return Collection<TKey, T> |
753
|
|
|
*/ |
754
|
2 |
|
static fn (iterable $iterable): Collection => Collection::fromIterable($iterable) |
755
|
2 |
|
); |
756
|
|
|
} |
757
|
|
|
|
758
|
6 |
|
public function split(int $type = OperationInterface\Splitable::BEFORE, callable ...$callbacks): CollectionInterface |
759
|
|
|
{ |
760
|
6 |
|
return new self((new Operation\Split())()($type)(...$callbacks), [$this]); |
761
|
|
|
} |
762
|
|
|
|
763
|
8 |
|
public function squash(): CollectionInterface |
764
|
|
|
{ |
765
|
8 |
|
return self::fromIterable($this->pack()->all(false))->unpack(); |
766
|
|
|
} |
767
|
|
|
|
768
|
4 |
|
public function strict(?callable $callback = null): CollectionInterface |
769
|
|
|
{ |
770
|
4 |
|
return new self((new Operation\Strict())()($callback), [$this]); |
771
|
|
|
} |
772
|
|
|
|
773
|
2 |
|
public function tail(): CollectionInterface |
774
|
|
|
{ |
775
|
2 |
|
return new self((new Operation\Tail())(), [$this]); |
776
|
|
|
} |
777
|
|
|
|
778
|
4 |
|
public function tails(): CollectionInterface |
779
|
|
|
{ |
780
|
4 |
|
return new self((new Operation\Tails())(), [$this]); |
781
|
|
|
} |
782
|
|
|
|
783
|
6 |
|
public function takeWhile(callable ...$callbacks): CollectionInterface |
784
|
|
|
{ |
785
|
6 |
|
return new self((new Operation\TakeWhile())()(...$callbacks), [$this]); |
786
|
|
|
} |
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* @template U |
790
|
|
|
* |
791
|
|
|
* @param callable(int): U $callback |
792
|
|
|
* |
793
|
|
|
* @return self<int, U> |
794
|
|
|
*/ |
795
|
2 |
|
public static function times(int $number = 0, ?callable $callback = null): CollectionInterface |
796
|
|
|
{ |
797
|
2 |
|
return new self((new Operation\Times())()($number)($callback)); |
798
|
|
|
} |
799
|
|
|
|
800
|
2 |
|
public function transpose(): CollectionInterface |
801
|
|
|
{ |
802
|
2 |
|
return new self((new Operation\Transpose())(), [$this]); |
803
|
|
|
} |
804
|
|
|
|
805
|
8 |
|
public function truthy(): bool |
806
|
|
|
{ |
807
|
8 |
|
return (new Operation\Truthy())()($this)->current(); |
808
|
|
|
} |
809
|
|
|
|
810
|
2 |
|
public static function unfold(callable $callback, iterable $parameters = []): CollectionInterface |
811
|
|
|
{ |
812
|
2 |
|
return new self((new Operation\Unfold())()($parameters)($callback)); |
813
|
|
|
} |
814
|
|
|
|
815
|
2 |
|
public function unlines(): string |
816
|
|
|
{ |
817
|
2 |
|
return (new self((new Operation\Unlines())(), [$this]))->current(0, ''); |
818
|
|
|
} |
819
|
|
|
|
820
|
8 |
|
public function unpack(): CollectionInterface |
821
|
|
|
{ |
822
|
8 |
|
return new self((new Operation\Unpack())(), [$this]); |
823
|
|
|
} |
824
|
|
|
|
825
|
2 |
|
public function unpair(): CollectionInterface |
826
|
|
|
{ |
827
|
2 |
|
return new self((new Operation\Unpair())(), [$this]); |
828
|
|
|
} |
829
|
|
|
|
830
|
4 |
|
public function until(callable ...$callbacks): CollectionInterface |
831
|
|
|
{ |
832
|
4 |
|
return new self((new Operation\Until())()(...$callbacks), [$this]); |
833
|
|
|
} |
834
|
|
|
|
835
|
2 |
|
public function unwindow(): CollectionInterface |
836
|
|
|
{ |
837
|
2 |
|
return new self((new Operation\Unwindow())(), [$this]); |
838
|
|
|
} |
839
|
|
|
|
840
|
2 |
|
public function unwords(): string |
841
|
|
|
{ |
842
|
2 |
|
return (new self((new Operation\Unwords())(), [$this]))->current(0, ''); |
843
|
|
|
} |
844
|
|
|
|
845
|
6 |
|
public function unwrap(): CollectionInterface |
846
|
|
|
{ |
847
|
6 |
|
return new self((new Operation\Unwrap())(), [$this]); |
848
|
|
|
} |
849
|
|
|
|
850
|
2 |
|
public function unzip(): CollectionInterface |
851
|
|
|
{ |
852
|
2 |
|
return new self((new Operation\Unzip())(), [$this]); |
853
|
|
|
} |
854
|
|
|
|
855
|
4 |
|
public function when(callable $predicate, callable $whenTrue, ?callable $whenFalse = null): CollectionInterface |
856
|
|
|
{ |
857
|
4 |
|
$whenFalse ??= |
858
|
|
|
/** |
859
|
|
|
* @param iterable<TKey, T> $iterable |
860
|
|
|
* |
861
|
|
|
* @return iterable<TKey, T> |
862
|
|
|
*/ |
863
|
4 |
|
static fn (iterable $iterable): iterable => $iterable; |
864
|
|
|
|
865
|
4 |
|
return new self((new Operation\When())()($predicate)($whenTrue)($whenFalse), [$this]); |
866
|
|
|
} |
867
|
|
|
|
868
|
8 |
|
public function window(int $size): CollectionInterface |
869
|
|
|
{ |
870
|
8 |
|
return new self((new Operation\Window())()($size), [$this]); |
871
|
|
|
} |
872
|
|
|
|
873
|
2 |
|
public function words(): CollectionInterface |
874
|
|
|
{ |
875
|
2 |
|
return new self((new Operation\Words())(), [$this]); |
876
|
|
|
} |
877
|
|
|
|
878
|
4 |
|
public function wrap(): CollectionInterface |
879
|
|
|
{ |
880
|
4 |
|
return new self((new Operation\Wrap())(), [$this]); |
881
|
|
|
} |
882
|
|
|
|
883
|
4 |
|
public function zip(iterable ...$iterables): CollectionInterface |
884
|
|
|
{ |
885
|
4 |
|
return new self((new Operation\Zip())()(...$iterables), [$this]); |
886
|
|
|
} |
887
|
|
|
} |
888
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths