| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use Closure; |
| 7 |
use DateInterval; |
| 8 |
use DateTimeImmutable; |
| 9 |
use DateTimeInterface; |
| 10 |
use Generator; |
| 11 |
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; |
| 12 |
use Illuminate\Support\Traits\EnumeratesValues; |
| 13 |
use Illuminate\Support\Traits\Macroable; |
| 14 |
use InvalidArgumentException; |
| 15 |
use IteratorAggregate; |
| 16 |
use stdClass; |
| 17 |
use Traversable; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use EnumeratesValues, Macroable; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public $source; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function __construct($source = null) |
| 46 |
{ |
| 47 |
if ($source instanceof Closure || $source instanceof self) { |
| 48 |
$this->source = $source; |
| 49 |
} elseif (is_null($source)) { |
| 50 |
$this->source = static::empty(); |
| 51 |
} elseif ($source instanceof Generator) { |
| 52 |
throw new InvalidArgumentException( |
| 53 |
'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.' |
| 54 |
); |
| 55 |
} else { |
| 56 |
$this->source = $this->getArrayableItems($source); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static function make($items = []) |
| 70 |
{ |
| 71 |
return new static($items); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function range($from, $to, $step = 1) |
| 85 |
{ |
| 86 |
if ($step == 0) { |
| 87 |
throw new InvalidArgumentException('Step value cannot be zero.'); |
| 88 |
} |
| 89 |
|
| 90 |
return new static(function () use ($from, $to, $step) { |
| 91 |
if ($from <= $to) { |
| 92 |
for (; $from <= $to; $from += abs($step)) { |
| 93 |
yield $from; |
| 94 |
} |
| 95 |
} else { |
| 96 |
for (; $from >= $to; $from -= abs($step)) { |
| 97 |
yield $from; |
| 98 |
} |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function all() |
| 109 |
{ |
| 110 |
if (is_array($this->source)) { |
| 111 |
return $this->source; |
| 112 |
} |
| 113 |
|
| 114 |
return iterator_to_array($this->getIterator()); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function eager() |
| 123 |
{ |
| 124 |
return new static($this->all()); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function remember() |
| 133 |
{ |
| 134 |
$iterator = $this->getIterator(); |
| 135 |
|
| 136 |
$iteratorIndex = 0; |
| 137 |
|
| 138 |
$cache = []; |
| 139 |
|
| 140 |
return new static(function () use ($iterator, &$iteratorIndex, &$cache) { |
| 141 |
for ($index = 0; true; $index++) { |
| 142 |
if (array_key_exists($index, $cache)) { |
| 143 |
yield $cache[$index][0] => $cache[$index][1]; |
| 144 |
|
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
if ($iteratorIndex < $index) { |
| 149 |
$iterator->next(); |
| 150 |
|
| 151 |
$iteratorIndex++; |
| 152 |
} |
| 153 |
|
| 154 |
if (! $iterator->valid()) { |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
$cache[$index] = [$iterator->key(), $iterator->current()]; |
| 159 |
|
| 160 |
yield $cache[$index][0] => $cache[$index][1]; |
| 161 |
} |
| 162 |
}); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function median($key = null) |
| 172 |
{ |
| 173 |
return $this->collect()->median($key); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function mode($key = null) |
| 183 |
{ |
| 184 |
return $this->collect()->mode($key); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function collapse() |
| 193 |
{ |
| 194 |
return new static(function () { |
| 195 |
foreach ($this as $values) { |
| 196 |
if (is_array($values) || $values instanceof Enumerable) { |
| 197 |
foreach ($values as $value) { |
| 198 |
yield $value; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
}); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function collapseWithKeys() |
| 211 |
{ |
| 212 |
return new static(function () { |
| 213 |
foreach ($this as $values) { |
| 214 |
if (is_array($values) || $values instanceof Enumerable) { |
| 215 |
foreach ($values as $key => $value) { |
| 216 |
yield $key => $value; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
}); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public function contains($key, $operator = null, $value = null) |
| 232 |
{ |
| 233 |
if (func_num_args() === 1 && $this->useAsCallable($key)) { |
| 234 |
$placeholder = new stdClass; |
| 235 |
|
| 236 |
|
| 237 |
return $this->first($key, $placeholder) !== $placeholder; |
| 238 |
} |
| 239 |
|
| 240 |
if (func_num_args() === 1) { |
| 241 |
$needle = $key; |
| 242 |
|
| 243 |
foreach ($this as $value) { |
| 244 |
if ($value == $needle) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
return $this->contains($this->operatorForWhere(...func_get_args())); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function containsStrict($key, $value = null) |
| 263 |
{ |
| 264 |
if (func_num_args() === 2) { |
| 265 |
return $this->contains(fn ($item) => data_get($item, $key) === $value); |
| 266 |
} |
| 267 |
|
| 268 |
if ($this->useAsCallable($key)) { |
| 269 |
return ! is_null($this->first($key)); |
| 270 |
} |
| 271 |
|
| 272 |
foreach ($this as $item) { |
| 273 |
if ($item === $key) { |
| 274 |
return true; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function doesntContain($key, $operator = null, $value = null) |
| 290 |
{ |
| 291 |
return ! $this->contains(...func_get_args()); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function doesntContainStrict($key, $operator = null, $value = null) |
| 303 |
{ |
| 304 |
return ! $this->containsStrict(...func_get_args()); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#[\Override] |
| 311 |
public function crossJoin(...$arrays) |
| 312 |
{ |
| 313 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function countBy($countBy = null) |
| 323 |
{ |
| 324 |
$countBy = is_null($countBy) |
| 325 |
? $this->identity() |
| 326 |
: $this->valueRetriever($countBy); |
| 327 |
|
| 328 |
return new static(function () use ($countBy) { |
| 329 |
$counts = []; |
| 330 |
|
| 331 |
foreach ($this as $key => $value) { |
| 332 |
$group = enum_value($countBy($value, $key)); |
| 333 |
|
| 334 |
if (empty($counts[$group])) { |
| 335 |
$counts[$group] = 0; |
| 336 |
} |
| 337 |
|
| 338 |
$counts[$group]++; |
| 339 |
} |
| 340 |
|
| 341 |
yield from $counts; |
| 342 |
}); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#[\Override] |
| 349 |
public function diff($items) |
| 350 |
{ |
| 351 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
#[\Override] |
| 358 |
public function diffUsing($items, callable $callback) |
| 359 |
{ |
| 360 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
#[\Override] |
| 367 |
public function diffAssoc($items) |
| 368 |
{ |
| 369 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
#[\Override] |
| 376 |
public function diffAssocUsing($items, callable $callback) |
| 377 |
{ |
| 378 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
#[\Override] |
| 385 |
public function diffKeys($items) |
| 386 |
{ |
| 387 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
#[\Override] |
| 394 |
public function diffKeysUsing($items, callable $callback) |
| 395 |
{ |
| 396 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#[\Override] |
| 403 |
public function duplicates($callback = null, $strict = false) |
| 404 |
{ |
| 405 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
#[\Override] |
| 412 |
public function duplicatesStrict($callback = null) |
| 413 |
{ |
| 414 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
#[\Override] |
| 421 |
public function except($keys) |
| 422 |
{ |
| 423 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function filter(?callable $callback = null) |
| 433 |
{ |
| 434 |
if (is_null($callback)) { |
| 435 |
$callback = fn ($value) => (bool) $value; |
| 436 |
} |
| 437 |
|
| 438 |
return new static(function () use ($callback) { |
| 439 |
foreach ($this as $key => $value) { |
| 440 |
if ($callback($value, $key)) { |
| 441 |
yield $key => $value; |
| 442 |
} |
| 443 |
} |
| 444 |
}); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public function first(?callable $callback = null, $default = null) |
| 457 |
{ |
| 458 |
$iterator = $this->getIterator(); |
| 459 |
|
| 460 |
if (is_null($callback)) { |
| 461 |
if (! $iterator->valid()) { |
| 462 |
return value($default); |
| 463 |
} |
| 464 |
|
| 465 |
return $iterator->current(); |
| 466 |
} |
| 467 |
|
| 468 |
foreach ($iterator as $key => $value) { |
| 469 |
if ($callback($value, $key)) { |
| 470 |
return $value; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return value($default); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public function flatten($depth = INF) |
| 484 |
{ |
| 485 |
$instance = new static(function () use ($depth) { |
| 486 |
foreach ($this as $item) { |
| 487 |
if (! is_array($item) && ! $item instanceof Enumerable) { |
| 488 |
yield $item; |
| 489 |
} elseif ($depth === 1) { |
| 490 |
yield from $item; |
| 491 |
} else { |
| 492 |
yield from (new static($item))->flatten($depth - 1); |
| 493 |
} |
| 494 |
} |
| 495 |
}); |
| 496 |
|
| 497 |
return $instance->values(); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public function flip() |
| 506 |
{ |
| 507 |
return new static(function () { |
| 508 |
foreach ($this as $key => $value) { |
| 509 |
yield $value => $key; |
| 510 |
} |
| 511 |
}); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public function get($key, $default = null) |
| 524 |
{ |
| 525 |
if (is_null($key)) { |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
foreach ($this as $outerKey => $outerValue) { |
| 530 |
if ($outerKey == $key) { |
| 531 |
return $outerValue; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
return value($default); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
#[\Override] |
| 552 |
public function groupBy($groupBy, $preserveKeys = false) |
| 553 |
{ |
| 554 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public function keyBy($keyBy) |
| 566 |
{ |
| 567 |
return new static(function () use ($keyBy) { |
| 568 |
$keyBy = $this->valueRetriever($keyBy); |
| 569 |
|
| 570 |
foreach ($this as $key => $item) { |
| 571 |
$resolvedKey = $keyBy($item, $key); |
| 572 |
|
| 573 |
if (is_object($resolvedKey)) { |
| 574 |
$resolvedKey = (string) $resolvedKey; |
| 575 |
} |
| 576 |
|
| 577 |
yield $resolvedKey => $item; |
| 578 |
} |
| 579 |
}); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public function has($key) |
| 589 |
{ |
| 590 |
$keys = array_flip(is_array($key) ? $key : func_get_args()); |
| 591 |
$count = count($keys); |
| 592 |
|
| 593 |
foreach ($this as $key => $value) { |
| 594 |
if (array_key_exists($key, $keys) && --$count == 0) { |
| 595 |
return true; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
return false; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
public function hasAny($key) |
| 609 |
{ |
| 610 |
$keys = array_flip(is_array($key) ? $key : func_get_args()); |
| 611 |
|
| 612 |
foreach ($this as $key => $value) { |
| 613 |
if (array_key_exists($key, $keys)) { |
| 614 |
return true; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public function implode($value, $glue = null) |
| 629 |
{ |
| 630 |
return $this->collect()->implode(...func_get_args()); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
#[\Override] |
| 637 |
public function intersect($items) |
| 638 |
{ |
| 639 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
#[\Override] |
| 646 |
public function intersectUsing($items, callable $callback) |
| 647 |
{ |
| 648 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
#[\Override] |
| 655 |
public function intersectAssoc($items) |
| 656 |
{ |
| 657 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
#[\Override] |
| 664 |
public function intersectAssocUsing($items, callable $callback) |
| 665 |
{ |
| 666 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
#[\Override] |
| 673 |
public function intersectByKeys($items) |
| 674 |
{ |
| 675 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
public function isEmpty() |
| 684 |
{ |
| 685 |
return ! $this->getIterator()->valid(); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public function containsOneItem(?callable $callback = null): bool |
| 697 |
{ |
| 698 |
return $this->hasSole($callback); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
public function containsManyItems(): bool |
| 709 |
{ |
| 710 |
return $this->hasMany(); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
public function join($glue, $finalGlue = '') |
| 721 |
{ |
| 722 |
return $this->collect()->join(...func_get_args()); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public function keys() |
| 731 |
{ |
| 732 |
return new static(function () { |
| 733 |
foreach ($this as $key => $value) { |
| 734 |
yield $key; |
| 735 |
} |
| 736 |
}); |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public function last(?callable $callback = null, $default = null) |
| 749 |
{ |
| 750 |
$needle = $placeholder = new stdClass; |
| 751 |
|
| 752 |
foreach ($this as $key => $value) { |
| 753 |
if (is_null($callback) || $callback($value, $key)) { |
| 754 |
$needle = $value; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
return $needle === $placeholder ? value($default) : $needle; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public function pluck($value, $key = null) |
| 769 |
{ |
| 770 |
return new static(function () use ($value, $key) { |
| 771 |
[$value, $key] = $this->explodePluckParameters($value, $key); |
| 772 |
|
| 773 |
foreach ($this as $item) { |
| 774 |
$itemValue = $value instanceof Closure |
| 775 |
? $value($item) |
| 776 |
: data_get($item, $value); |
| 777 |
|
| 778 |
if (is_null($key)) { |
| 779 |
yield $itemValue; |
| 780 |
} else { |
| 781 |
$itemKey = $key instanceof Closure |
| 782 |
? $key($item) |
| 783 |
: data_get($item, $key); |
| 784 |
|
| 785 |
if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
| 786 |
$itemKey = (string) $itemKey; |
| 787 |
} |
| 788 |
|
| 789 |
yield $itemKey => $itemValue; |
| 790 |
} |
| 791 |
} |
| 792 |
}); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public function map(callable $callback) |
| 804 |
{ |
| 805 |
return new static(function () use ($callback) { |
| 806 |
foreach ($this as $key => $value) { |
| 807 |
yield $key => $callback($value, $key); |
| 808 |
} |
| 809 |
}); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
#[\Override] |
| 816 |
public function mapToDictionary(callable $callback) |
| 817 |
{ |
| 818 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
public function mapWithKeys(callable $callback) |
| 833 |
{ |
| 834 |
return new static(function () use ($callback) { |
| 835 |
foreach ($this as $key => $value) { |
| 836 |
yield from $callback($value, $key); |
| 837 |
} |
| 838 |
}); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
#[\Override] |
| 845 |
public function merge($items) |
| 846 |
{ |
| 847 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
#[\Override] |
| 854 |
public function mergeRecursive($items) |
| 855 |
{ |
| 856 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
public function multiply(int $multiplier) |
| 866 |
{ |
| 867 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
public function combine($values) |
| 879 |
{ |
| 880 |
return new static(function () use ($values) { |
| 881 |
$values = $this->makeIterator($values); |
| 882 |
|
| 883 |
$errorMessage = 'Both parameters should have an equal number of elements'; |
| 884 |
|
| 885 |
foreach ($this as $key) { |
| 886 |
if (! $values->valid()) { |
| 887 |
trigger_error($errorMessage, E_USER_WARNING); |
| 888 |
|
| 889 |
break; |
| 890 |
} |
| 891 |
|
| 892 |
yield $key => $values->current(); |
| 893 |
|
| 894 |
$values->next(); |
| 895 |
} |
| 896 |
|
| 897 |
if ($values->valid()) { |
| 898 |
trigger_error($errorMessage, E_USER_WARNING); |
| 899 |
} |
| 900 |
}); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
#[\Override] |
| 907 |
public function union($items) |
| 908 |
{ |
| 909 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public function nth($step, $offset = 0) |
| 922 |
{ |
| 923 |
if ($step < 1) { |
| 924 |
throw new InvalidArgumentException('Step value must be at least 1.'); |
| 925 |
} |
| 926 |
|
| 927 |
return new static(function () use ($step, $offset) { |
| 928 |
$position = 0; |
| 929 |
|
| 930 |
foreach ($this->slice($offset) as $item) { |
| 931 |
if ($position % $step === 0) { |
| 932 |
yield $item; |
| 933 |
} |
| 934 |
|
| 935 |
$position++; |
| 936 |
} |
| 937 |
}); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
public function only($keys) |
| 947 |
{ |
| 948 |
if ($keys instanceof Enumerable) { |
| 949 |
$keys = $keys->all(); |
| 950 |
} elseif (! is_null($keys)) { |
| 951 |
$keys = is_array($keys) ? $keys : func_get_args(); |
| 952 |
} |
| 953 |
|
| 954 |
return new static(function () use ($keys) { |
| 955 |
if (is_null($keys)) { |
| 956 |
yield from $this; |
| 957 |
} else { |
| 958 |
$keys = array_flip($keys); |
| 959 |
|
| 960 |
foreach ($this as $key => $value) { |
| 961 |
if (array_key_exists($key, $keys)) { |
| 962 |
yield $key => $value; |
| 963 |
|
| 964 |
unset($keys[$key]); |
| 965 |
|
| 966 |
if (empty($keys)) { |
| 967 |
break; |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
}); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
public function select($keys) |
| 982 |
{ |
| 983 |
if ($keys instanceof Enumerable) { |
| 984 |
$keys = $keys->all(); |
| 985 |
} elseif (! is_null($keys)) { |
| 986 |
$keys = is_array($keys) ? $keys : func_get_args(); |
| 987 |
} |
| 988 |
|
| 989 |
return new static(function () use ($keys) { |
| 990 |
if (is_null($keys)) { |
| 991 |
yield from $this; |
| 992 |
} else { |
| 993 |
foreach ($this as $item) { |
| 994 |
$result = []; |
| 995 |
|
| 996 |
foreach ($keys as $key) { |
| 997 |
if (Arr::accessible($item) && Arr::exists($item, $key)) { |
| 998 |
$result[$key] = $item[$key]; |
| 999 |
} elseif (is_object($item) && isset($item->{$key})) { |
| 1000 |
$result[$key] = $item->{$key}; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
yield $result; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
}); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
public function concat($source) |
| 1020 |
{ |
| 1021 |
return (new static(function () use ($source) { |
| 1022 |
yield from $this; |
| 1023 |
yield from $source; |
| 1024 |
}))->values(); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
public function random($number = null, $preserveKeys = false) |
| 1037 |
{ |
| 1038 |
$result = $this->collect()->random(...func_get_args()); |
| 1039 |
|
| 1040 |
return is_null($number) ? $result : new static($result); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public function replace($items) |
| 1050 |
{ |
| 1051 |
return new static(function () use ($items) { |
| 1052 |
$items = $this->getArrayableItems($items); |
| 1053 |
|
| 1054 |
foreach ($this as $key => $value) { |
| 1055 |
if (array_key_exists($key, $items)) { |
| 1056 |
yield $key => $items[$key]; |
| 1057 |
|
| 1058 |
unset($items[$key]); |
| 1059 |
} else { |
| 1060 |
yield $key => $value; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
foreach ($items as $key => $value) { |
| 1065 |
yield $key => $value; |
| 1066 |
} |
| 1067 |
}); |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
#[\Override] |
| 1074 |
public function replaceRecursive($items) |
| 1075 |
{ |
| 1076 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
#[\Override] |
| 1083 |
public function reverse() |
| 1084 |
{ |
| 1085 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
public function search($value, $strict = false) |
| 1096 |
{ |
| 1097 |
|
| 1098 |
$predicate = $this->useAsCallable($value) |
| 1099 |
? $value |
| 1100 |
: function ($item) use ($value, $strict) { |
| 1101 |
return $strict ? $item === $value : $item == $value; |
| 1102 |
}; |
| 1103 |
|
| 1104 |
foreach ($this as $key => $item) { |
| 1105 |
if ($predicate($item, $key)) { |
| 1106 |
return $key; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
return false; |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
public function before($value, $strict = false) |
| 1121 |
{ |
| 1122 |
$previous = null; |
| 1123 |
|
| 1124 |
|
| 1125 |
$predicate = $this->useAsCallable($value) |
| 1126 |
? $value |
| 1127 |
: function ($item) use ($value, $strict) { |
| 1128 |
return $strict ? $item === $value : $item == $value; |
| 1129 |
}; |
| 1130 |
|
| 1131 |
foreach ($this as $key => $item) { |
| 1132 |
if ($predicate($item, $key)) { |
| 1133 |
return $previous; |
| 1134 |
} |
| 1135 |
|
| 1136 |
$previous = $item; |
| 1137 |
} |
| 1138 |
|
| 1139 |
return null; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
public function after($value, $strict = false) |
| 1150 |
{ |
| 1151 |
$found = false; |
| 1152 |
|
| 1153 |
|
| 1154 |
$predicate = $this->useAsCallable($value) |
| 1155 |
? $value |
| 1156 |
: function ($item) use ($value, $strict) { |
| 1157 |
return $strict ? $item === $value : $item == $value; |
| 1158 |
}; |
| 1159 |
|
| 1160 |
foreach ($this as $key => $item) { |
| 1161 |
if ($found) { |
| 1162 |
return $item; |
| 1163 |
} |
| 1164 |
|
| 1165 |
if ($predicate($item, $key)) { |
| 1166 |
$found = true; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
return null; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
#[\Override] |
| 1177 |
public function shuffle() |
| 1178 |
{ |
| 1179 |
return $this->passthru(__FUNCTION__, []); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
public function sliding($size = 2, $step = 1) |
| 1192 |
{ |
| 1193 |
if ($size < 1) { |
| 1194 |
throw new InvalidArgumentException('Size value must be at least 1.'); |
| 1195 |
} elseif ($step < 1) { |
| 1196 |
throw new InvalidArgumentException('Step value must be at least 1.'); |
| 1197 |
} |
| 1198 |
|
| 1199 |
return new static(function () use ($size, $step) { |
| 1200 |
$iterator = $this->getIterator(); |
| 1201 |
|
| 1202 |
$chunk = []; |
| 1203 |
|
| 1204 |
while ($iterator->valid()) { |
| 1205 |
$chunk[$iterator->key()] = $iterator->current(); |
| 1206 |
|
| 1207 |
if (count($chunk) == $size) { |
| 1208 |
yield (new static($chunk))->tap(function () use (&$chunk, $step) { |
| 1209 |
$chunk = array_slice($chunk, $step, null, true); |
| 1210 |
}); |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
if ($step > $size) { |
| 1216 |
$skip = $step - $size; |
| 1217 |
|
| 1218 |
for ($i = 0; $i < $skip && $iterator->valid(); $i++) { |
| 1219 |
$iterator->next(); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
$iterator->next(); |
| 1225 |
} |
| 1226 |
}); |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
public function skip($count) |
| 1236 |
{ |
| 1237 |
return new static(function () use ($count) { |
| 1238 |
$iterator = $this->getIterator(); |
| 1239 |
|
| 1240 |
while ($iterator->valid() && $count--) { |
| 1241 |
$iterator->next(); |
| 1242 |
} |
| 1243 |
|
| 1244 |
while ($iterator->valid()) { |
| 1245 |
yield $iterator->key() => $iterator->current(); |
| 1246 |
|
| 1247 |
$iterator->next(); |
| 1248 |
} |
| 1249 |
}); |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
public function skipUntil($value) |
| 1259 |
{ |
| 1260 |
$callback = $this->useAsCallable($value) ? $value : $this->equality($value); |
| 1261 |
|
| 1262 |
return $this->skipWhile($this->negate($callback)); |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
public function skipWhile($value) |
| 1272 |
{ |
| 1273 |
$callback = $this->useAsCallable($value) ? $value : $this->equality($value); |
| 1274 |
|
| 1275 |
return new static(function () use ($callback) { |
| 1276 |
$iterator = $this->getIterator(); |
| 1277 |
|
| 1278 |
while ($iterator->valid() && $callback($iterator->current(), $iterator->key())) { |
| 1279 |
$iterator->next(); |
| 1280 |
} |
| 1281 |
|
| 1282 |
while ($iterator->valid()) { |
| 1283 |
yield $iterator->key() => $iterator->current(); |
| 1284 |
|
| 1285 |
$iterator->next(); |
| 1286 |
} |
| 1287 |
}); |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
#[\Override] |
| 1294 |
public function slice($offset, $length = null) |
| 1295 |
{ |
| 1296 |
if ($offset < 0 || $length < 0) { |
| 1297 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1298 |
} |
| 1299 |
|
| 1300 |
$instance = $this->skip($offset); |
| 1301 |
|
| 1302 |
return is_null($length) ? $instance : $instance->take($length); |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
#[\Override] |
| 1311 |
public function split($numberOfGroups) |
| 1312 |
{ |
| 1313 |
if ($numberOfGroups < 1) { |
| 1314 |
throw new InvalidArgumentException('Number of groups must be at least 1.'); |
| 1315 |
} |
| 1316 |
|
| 1317 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
public function sole($key = null, $operator = null, $value = null) |
| 1332 |
{ |
| 1333 |
$filter = func_num_args() > 1 |
| 1334 |
? $this->operatorForWhere(...func_get_args()) |
| 1335 |
: $key; |
| 1336 |
|
| 1337 |
return $this |
| 1338 |
->unless($filter == null) |
| 1339 |
->filter($filter) |
| 1340 |
->take(2) |
| 1341 |
->collect() |
| 1342 |
->sole(); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
public function hasSole($key = null, $operator = null, $value = null): bool |
| 1354 |
{ |
| 1355 |
$filter = func_num_args() > 1 |
| 1356 |
? $this->operatorForWhere(...func_get_args()) |
| 1357 |
: $key; |
| 1358 |
|
| 1359 |
return $this |
| 1360 |
->unless($filter == null) |
| 1361 |
->filter($filter) |
| 1362 |
->take(2) |
| 1363 |
->count() === 1; |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
public function firstOrFail($key = null, $operator = null, $value = null) |
| 1377 |
{ |
| 1378 |
$filter = func_num_args() > 1 |
| 1379 |
? $this->operatorForWhere(...func_get_args()) |
| 1380 |
: $key; |
| 1381 |
|
| 1382 |
return $this |
| 1383 |
->unless($filter == null) |
| 1384 |
->filter($filter) |
| 1385 |
->take(1) |
| 1386 |
->collect() |
| 1387 |
->firstOrFail(); |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
public function chunk($size, $preserveKeys = true) |
| 1398 |
{ |
| 1399 |
if ($size <= 0) { |
| 1400 |
return static::empty(); |
| 1401 |
} |
| 1402 |
|
| 1403 |
$add = match ($preserveKeys) { |
| 1404 |
true => fn (array &$chunk, Traversable $iterator) => $chunk[$iterator->key()] = $iterator->current(), |
| 1405 |
false => fn (array &$chunk, Traversable $iterator) => $chunk[] = $iterator->current(), |
| 1406 |
}; |
| 1407 |
|
| 1408 |
return new static(function () use ($size, $add) { |
| 1409 |
$iterator = $this->getIterator(); |
| 1410 |
|
| 1411 |
while ($iterator->valid()) { |
| 1412 |
$chunk = []; |
| 1413 |
|
| 1414 |
while (true) { |
| 1415 |
$add($chunk, $iterator); |
| 1416 |
|
| 1417 |
if (count($chunk) < $size) { |
| 1418 |
$iterator->next(); |
| 1419 |
|
| 1420 |
if (! $iterator->valid()) { |
| 1421 |
break; |
| 1422 |
} |
| 1423 |
} else { |
| 1424 |
break; |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
yield new static($chunk); |
| 1429 |
|
| 1430 |
$iterator->next(); |
| 1431 |
} |
| 1432 |
}); |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
public function splitIn($numberOfGroups) |
| 1444 |
{ |
| 1445 |
if ($numberOfGroups < 1) { |
| 1446 |
throw new InvalidArgumentException('Number of groups must be at least 1.'); |
| 1447 |
} |
| 1448 |
|
| 1449 |
return $this->chunk((int) ceil($this->count() / $numberOfGroups)); |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
public function chunkWhile(callable $callback) |
| 1459 |
{ |
| 1460 |
return new static(function () use ($callback) { |
| 1461 |
$iterator = $this->getIterator(); |
| 1462 |
|
| 1463 |
$chunk = new Collection; |
| 1464 |
|
| 1465 |
if ($iterator->valid()) { |
| 1466 |
$chunk[$iterator->key()] = $iterator->current(); |
| 1467 |
|
| 1468 |
$iterator->next(); |
| 1469 |
} |
| 1470 |
|
| 1471 |
while ($iterator->valid()) { |
| 1472 |
if (! $callback($iterator->current(), $iterator->key(), $chunk)) { |
| 1473 |
yield new static($chunk); |
| 1474 |
|
| 1475 |
$chunk = new Collection; |
| 1476 |
} |
| 1477 |
|
| 1478 |
$chunk[$iterator->key()] = $iterator->current(); |
| 1479 |
|
| 1480 |
$iterator->next(); |
| 1481 |
} |
| 1482 |
|
| 1483 |
if ($chunk->isNotEmpty()) { |
| 1484 |
yield new static($chunk); |
| 1485 |
} |
| 1486 |
}); |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
#[\Override] |
| 1493 |
public function sort($callback = null) |
| 1494 |
{ |
| 1495 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
#[\Override] |
| 1502 |
public function sortDesc($options = SORT_REGULAR) |
| 1503 |
{ |
| 1504 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
#[\Override] |
| 1511 |
public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
| 1512 |
{ |
| 1513 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
#[\Override] |
| 1520 |
public function sortByDesc($callback, $options = SORT_REGULAR) |
| 1521 |
{ |
| 1522 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
#[\Override] |
| 1529 |
public function sortKeys($options = SORT_REGULAR, $descending = false) |
| 1530 |
{ |
| 1531 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
#[\Override] |
| 1538 |
public function sortKeysDesc($options = SORT_REGULAR) |
| 1539 |
{ |
| 1540 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1541 |
} |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
#[\Override] |
| 1547 |
public function sortKeysUsing(callable $callback) |
| 1548 |
{ |
| 1549 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1550 |
} |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
public function take($limit) |
| 1559 |
{ |
| 1560 |
if ($limit < 0) { |
| 1561 |
return new static(function () use ($limit) { |
| 1562 |
$limit = abs($limit); |
| 1563 |
$ringBuffer = []; |
| 1564 |
$position = 0; |
| 1565 |
|
| 1566 |
foreach ($this as $key => $value) { |
| 1567 |
$ringBuffer[$position] = [$key, $value]; |
| 1568 |
$position = ($position + 1) % $limit; |
| 1569 |
} |
| 1570 |
|
| 1571 |
for ($i = 0, $end = min($limit, count($ringBuffer)); $i < $end; $i++) { |
| 1572 |
$pointer = ($position + $i) % $limit; |
| 1573 |
yield $ringBuffer[$pointer][0] => $ringBuffer[$pointer][1]; |
| 1574 |
} |
| 1575 |
}); |
| 1576 |
} |
| 1577 |
|
| 1578 |
return new static(function () use ($limit) { |
| 1579 |
$iterator = $this->getIterator(); |
| 1580 |
|
| 1581 |
while ($limit--) { |
| 1582 |
if (! $iterator->valid()) { |
| 1583 |
break; |
| 1584 |
} |
| 1585 |
|
| 1586 |
yield $iterator->key() => $iterator->current(); |
| 1587 |
|
| 1588 |
if ($limit) { |
| 1589 |
$iterator->next(); |
| 1590 |
} |
| 1591 |
} |
| 1592 |
}); |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
public function takeUntil($value) |
| 1602 |
{ |
| 1603 |
|
| 1604 |
$callback = $this->useAsCallable($value) ? $value : $this->equality($value); |
| 1605 |
|
| 1606 |
return new static(function () use ($callback) { |
| 1607 |
foreach ($this as $key => $item) { |
| 1608 |
if ($callback($item, $key)) { |
| 1609 |
break; |
| 1610 |
} |
| 1611 |
|
| 1612 |
yield $key => $item; |
| 1613 |
} |
| 1614 |
}); |
| 1615 |
} |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
public function takeUntilTimeout(DateTimeInterface $timeout, ?callable $callback = null) |
| 1625 |
{ |
| 1626 |
$timeout = $timeout->getTimestamp(); |
| 1627 |
|
| 1628 |
return new static(function () use ($timeout, $callback) { |
| 1629 |
if ($this->now() >= $timeout) { |
| 1630 |
if ($callback) { |
| 1631 |
$callback(null, null); |
| 1632 |
} |
| 1633 |
|
| 1634 |
return; |
| 1635 |
} |
| 1636 |
|
| 1637 |
foreach ($this as $key => $value) { |
| 1638 |
yield $key => $value; |
| 1639 |
|
| 1640 |
if ($this->now() >= $timeout) { |
| 1641 |
if ($callback) { |
| 1642 |
$callback($value, $key); |
| 1643 |
} |
| 1644 |
|
| 1645 |
break; |
| 1646 |
} |
| 1647 |
} |
| 1648 |
}); |
| 1649 |
} |
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
public function takeWhile($value) |
| 1658 |
{ |
| 1659 |
|
| 1660 |
$callback = $this->useAsCallable($value) ? $value : $this->equality($value); |
| 1661 |
|
| 1662 |
return $this->takeUntil(fn ($item, $key) => ! $callback($item, $key)); |
| 1663 |
} |
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
public function tapEach(callable $callback) |
| 1672 |
{ |
| 1673 |
return new static(function () use ($callback) { |
| 1674 |
foreach ($this as $key => $value) { |
| 1675 |
$callback($value, $key); |
| 1676 |
|
| 1677 |
yield $key => $value; |
| 1678 |
} |
| 1679 |
}); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
public function throttle(float $seconds) |
| 1688 |
{ |
| 1689 |
return new static(function () use ($seconds) { |
| 1690 |
$microseconds = $seconds * 1_000_000; |
| 1691 |
|
| 1692 |
foreach ($this as $key => $value) { |
| 1693 |
$fetchedAt = $this->preciseNow(); |
| 1694 |
|
| 1695 |
yield $key => $value; |
| 1696 |
|
| 1697 |
$sleep = $microseconds - ($this->preciseNow() - $fetchedAt); |
| 1698 |
|
| 1699 |
$this->usleep((int) $sleep); |
| 1700 |
} |
| 1701 |
}); |
| 1702 |
} |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
public function dot($depth = INF) |
| 1711 |
{ |
| 1712 |
return $this->passthru(__FUNCTION__, [$depth]); |
| 1713 |
} |
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
#[\Override] |
| 1719 |
public function undot() |
| 1720 |
{ |
| 1721 |
return $this->passthru(__FUNCTION__, []); |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
public function unique($key = null, $strict = false) |
| 1732 |
{ |
| 1733 |
$callback = $this->valueRetriever($key); |
| 1734 |
|
| 1735 |
return new static(function () use ($callback, $strict) { |
| 1736 |
$exists = []; |
| 1737 |
|
| 1738 |
foreach ($this as $key => $item) { |
| 1739 |
if (! in_array($id = $callback($item, $key), $exists, $strict)) { |
| 1740 |
yield $key => $item; |
| 1741 |
|
| 1742 |
$exists[] = $id; |
| 1743 |
} |
| 1744 |
} |
| 1745 |
}); |
| 1746 |
} |
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
public function values() |
| 1754 |
{ |
| 1755 |
return new static(function () { |
| 1756 |
foreach ($this as $item) { |
| 1757 |
yield $item; |
| 1758 |
} |
| 1759 |
}); |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
public function withHeartbeat(DateInterval|int $interval, callable $callback) |
| 1768 |
{ |
| 1769 |
$seconds = is_int($interval) ? $interval : $this->intervalSeconds($interval); |
| 1770 |
|
| 1771 |
return new static(function () use ($seconds, $callback) { |
| 1772 |
$start = $this->now(); |
| 1773 |
|
| 1774 |
foreach ($this as $key => $value) { |
| 1775 |
$now = $this->now(); |
| 1776 |
|
| 1777 |
if (($now - $start) >= $seconds) { |
| 1778 |
$callback(); |
| 1779 |
|
| 1780 |
$start = $now; |
| 1781 |
} |
| 1782 |
|
| 1783 |
yield $key => $value; |
| 1784 |
} |
| 1785 |
}); |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
protected function intervalSeconds(DateInterval $interval): int |
| 1792 |
{ |
| 1793 |
$start = new DateTimeImmutable(); |
| 1794 |
|
| 1795 |
return $start->add($interval)->getTimestamp() - $start->getTimestamp(); |
| 1796 |
} |
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
public function zip($items) |
| 1810 |
{ |
| 1811 |
$iterables = func_get_args(); |
| 1812 |
|
| 1813 |
return new static(function () use ($iterables) { |
| 1814 |
$iterators = (new Collection($iterables)) |
| 1815 |
->map(fn ($iterable) => $this->makeIterator($iterable)) |
| 1816 |
->prepend($this->getIterator()); |
| 1817 |
|
| 1818 |
while ($iterators->contains->valid()) { |
| 1819 |
yield new static($iterators->map->current()); |
| 1820 |
|
| 1821 |
$iterators->each->next(); |
| 1822 |
} |
| 1823 |
}); |
| 1824 |
} |
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
#[\Override] |
| 1830 |
public function pad($size, $value) |
| 1831 |
{ |
| 1832 |
if ($size < 0) { |
| 1833 |
return $this->passthru(__FUNCTION__, func_get_args()); |
| 1834 |
} |
| 1835 |
|
| 1836 |
return new static(function () use ($size, $value) { |
| 1837 |
$yielded = 0; |
| 1838 |
|
| 1839 |
foreach ($this as $index => $item) { |
| 1840 |
yield $index => $item; |
| 1841 |
|
| 1842 |
$yielded++; |
| 1843 |
} |
| 1844 |
|
| 1845 |
while ($yielded++ < $size) { |
| 1846 |
yield $value; |
| 1847 |
} |
| 1848 |
}); |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
public function getIterator(): Traversable |
| 1857 |
{ |
| 1858 |
return $this->makeIterator($this->source); |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
public function count(): int |
| 1867 |
{ |
| 1868 |
if (is_array($this->source)) { |
| 1869 |
return count($this->source); |
| 1870 |
} |
| 1871 |
|
| 1872 |
return iterator_count($this->getIterator()); |
| 1873 |
} |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
protected function makeIterator($source) |
| 1885 |
{ |
| 1886 |
if ($source instanceof IteratorAggregate) { |
| 1887 |
return $source->getIterator(); |
| 1888 |
} |
| 1889 |
|
| 1890 |
if (is_array($source)) { |
| 1891 |
return new ArrayIterator($source); |
| 1892 |
} |
| 1893 |
|
| 1894 |
if (is_callable($source)) { |
| 1895 |
$maybeTraversable = $source(); |
| 1896 |
|
| 1897 |
return $maybeTraversable instanceof Traversable |
| 1898 |
? $maybeTraversable |
| 1899 |
: new ArrayIterator(Arr::wrap($maybeTraversable)); |
| 1900 |
} |
| 1901 |
|
| 1902 |
return new ArrayIterator((array) $source); |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
protected function explodePluckParameters($value, $key) |
| 1913 |
{ |
| 1914 |
$value = is_string($value) ? explode('.', $value) : $value; |
| 1915 |
|
| 1916 |
$key = is_null($key) || is_array($key) || $key instanceof Closure ? $key : explode('.', $key); |
| 1917 |
|
| 1918 |
return [$value, $key]; |
| 1919 |
} |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
protected function passthru($method, array $params) |
| 1929 |
{ |
| 1930 |
return new static(function () use ($method, $params) { |
| 1931 |
yield from $this->collect()->$method(...$params); |
| 1932 |
}); |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
protected function now() |
| 1941 |
{ |
| 1942 |
return class_exists(Carbon::class) |
| 1943 |
? Carbon::now()->timestamp |
| 1944 |
: time(); |
| 1945 |
} |
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
protected function preciseNow() |
| 1953 |
{ |
| 1954 |
return class_exists(Carbon::class) |
| 1955 |
? Carbon::now()->getPreciseTimestamp() |
| 1956 |
: microtime(true) * 1_000_000; |
| 1957 |
} |
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
protected function usleep(int $microseconds) |
| 1965 |
{ |
| 1966 |
if ($microseconds <= 0) { |
| 1967 |
return; |
| 1968 |
} |
| 1969 |
|
| 1970 |
class_exists(Sleep::class) |
| 1971 |
? Sleep::usleep($microseconds) |
| 1972 |
: usleep($microseconds); |
| 1973 |
} |
| 1974 |
} |