| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support\Traits; |
| 4 |
|
| 5 |
use BackedEnum; |
| 6 |
use CachingIterator; |
| 7 |
use Closure; |
| 8 |
use Exception; |
| 9 |
use Illuminate\Contracts\Support\Arrayable; |
| 10 |
use Illuminate\Contracts\Support\Jsonable; |
| 11 |
use Illuminate\Support\Arr; |
| 12 |
use Illuminate\Support\Collection; |
| 13 |
use Illuminate\Support\Enumerable; |
| 14 |
use Illuminate\Support\HigherOrderCollectionProxy; |
| 15 |
use JsonSerializable; |
| 16 |
use UnexpectedValueException; |
| 17 |
use UnitEnum; |
| 18 |
|
| 19 |
use function Illuminate\Support\enum_value; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
trait EnumeratesValues |
| 60 |
{ |
| 61 |
use Conditionable; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected $escapeWhenCastingToString = false; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected static $proxies = [ |
| 76 |
'average', |
| 77 |
'avg', |
| 78 |
'contains', |
| 79 |
'doesntContain', |
| 80 |
'each', |
| 81 |
'every', |
| 82 |
'filter', |
| 83 |
'first', |
| 84 |
'flatMap', |
| 85 |
'groupBy', |
| 86 |
'hasMany', |
| 87 |
'hasSole', |
| 88 |
'keyBy', |
| 89 |
'last', |
| 90 |
'map', |
| 91 |
'max', |
| 92 |
'min', |
| 93 |
'partition', |
| 94 |
'percentage', |
| 95 |
'reject', |
| 96 |
'skipUntil', |
| 97 |
'skipWhile', |
| 98 |
'some', |
| 99 |
'sortBy', |
| 100 |
'sortByDesc', |
| 101 |
'sum', |
| 102 |
'takeUntil', |
| 103 |
'takeWhile', |
| 104 |
'unique', |
| 105 |
'unless', |
| 106 |
'until', |
| 107 |
'when', |
| 108 |
]; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static function make($items = []) |
| 120 |
{ |
| 121 |
return new static($items); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static function wrap($value) |
| 133 |
{ |
| 134 |
return $value instanceof Enumerable |
| 135 |
? new static($value) |
| 136 |
: new static(Arr::wrap($value)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static function unwrap($value) |
| 149 |
{ |
| 150 |
return $value instanceof Enumerable ? $value->all() : $value; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static function empty() |
| 159 |
{ |
| 160 |
return new static([]); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static function times($number, ?callable $callback = null) |
| 173 |
{ |
| 174 |
if ($number < 1) { |
| 175 |
return new static; |
| 176 |
} |
| 177 |
|
| 178 |
return static::range(1, $number) |
| 179 |
->unless($callback == null) |
| 180 |
->map($callback); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static function fromJson($json, $depth = 512, $flags = 0) |
| 192 |
{ |
| 193 |
return new static(json_decode($json, true, $depth, $flags)); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function avg($callback = null) |
| 203 |
{ |
| 204 |
$callback = $this->valueRetriever($callback); |
| 205 |
|
| 206 |
$reduced = $this->reduce(static function (&$reduce, $value) use ($callback) { |
| 207 |
if (! is_null($resolved = $callback($value))) { |
| 208 |
$reduce[0] += $resolved; |
| 209 |
$reduce[1]++; |
| 210 |
} |
| 211 |
|
| 212 |
return $reduce; |
| 213 |
}, [0, 0]); |
| 214 |
|
| 215 |
return $reduced[1] ? $reduced[0] / $reduced[1] : null; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function average($callback = null) |
| 225 |
{ |
| 226 |
return $this->avg($callback); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function some($key, $operator = null, $value = null) |
| 238 |
{ |
| 239 |
return $this->contains(...func_get_args()); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function dd(...$args) |
| 249 |
{ |
| 250 |
dd($this->all(), ...$args); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function dump(...$args) |
| 260 |
{ |
| 261 |
dump($this->all(), ...$args); |
| 262 |
|
| 263 |
return $this; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public function each(callable $callback) |
| 273 |
{ |
| 274 |
foreach ($this as $key => $item) { |
| 275 |
if ($callback($item, $key) === false) { |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return $this; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function eachSpread(callable $callback) |
| 290 |
{ |
| 291 |
return $this->each(function ($chunk, $key) use ($callback) { |
| 292 |
$chunk[] = $key; |
| 293 |
|
| 294 |
return $callback(...$chunk); |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function every($key, $operator = null, $value = null) |
| 307 |
{ |
| 308 |
if (func_num_args() === 1) { |
| 309 |
$callback = $this->valueRetriever($key); |
| 310 |
|
| 311 |
foreach ($this as $k => $v) { |
| 312 |
if (! $callback($v, $k)) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
return $this->every($this->operatorForWhere(...func_get_args())); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function firstWhere($key, $operator = null, $value = null) |
| 332 |
{ |
| 333 |
return $this->first($this->operatorForWhere(...func_get_args())); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public function hasMany($key = null, $operator = null, $value = null): bool |
| 345 |
{ |
| 346 |
$filter = func_num_args() > 1 |
| 347 |
? $this->operatorForWhere(...func_get_args()) |
| 348 |
: $key; |
| 349 |
|
| 350 |
return $this |
| 351 |
->unless($filter == null) |
| 352 |
->filter($filter) |
| 353 |
->take(2) |
| 354 |
->count() === 2; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function value($key, $default = null) |
| 367 |
{ |
| 368 |
$value = $this->first(function ($target) use ($key) { |
| 369 |
return data_has($target, $key); |
| 370 |
}); |
| 371 |
|
| 372 |
return data_get($value, $key, $default); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function ensure($type) |
| 386 |
{ |
| 387 |
$allowedTypes = is_array($type) ? $type : [$type]; |
| 388 |
|
| 389 |
return $this->each(function ($item, $index) use ($allowedTypes) { |
| 390 |
$itemType = get_debug_type($item); |
| 391 |
|
| 392 |
foreach ($allowedTypes as $allowedType) { |
| 393 |
if ($itemType === $allowedType || $item instanceof $allowedType) { |
| 394 |
return true; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
throw new UnexpectedValueException( |
| 399 |
sprintf("Collection should only include [%s] items, but '%s' found at position %d.", implode(', ', $allowedTypes), $itemType, $index) |
| 400 |
); |
| 401 |
}); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function isNotEmpty() |
| 416 |
{ |
| 417 |
return ! $this->isEmpty(); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function mapSpread(callable $callback) |
| 429 |
{ |
| 430 |
return $this->map(function ($chunk, $key) use ($callback) { |
| 431 |
$chunk[] = $key; |
| 432 |
|
| 433 |
return $callback(...$chunk); |
| 434 |
}); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function mapToGroups(callable $callback) |
| 449 |
{ |
| 450 |
$groups = $this->mapToDictionary($callback); |
| 451 |
|
| 452 |
return $groups->map($this->make(...)); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public function flatMap(callable $callback) |
| 465 |
{ |
| 466 |
return $this->map($callback)->collapse(); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public function mapInto($class) |
| 478 |
{ |
| 479 |
if (is_subclass_of($class, BackedEnum::class)) { |
| 480 |
return $this->map(fn ($value, $key) => $class::from($value)); |
| 481 |
} |
| 482 |
|
| 483 |
return $this->map(fn ($value, $key) => new $class($value, $key)); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function min($callback = null) |
| 493 |
{ |
| 494 |
$callback = $this->valueRetriever($callback); |
| 495 |
|
| 496 |
return $this->map(fn ($value) => $callback($value)) |
| 497 |
->reject(fn ($value) => is_null($value)) |
| 498 |
->reduce(fn ($result, $value) => is_null($result) || $value < $result ? $value : $result); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public function max($callback = null) |
| 508 |
{ |
| 509 |
$callback = $this->valueRetriever($callback); |
| 510 |
|
| 511 |
return $this->reject(fn ($value) => is_null($value))->reduce(function ($result, $item) use ($callback) { |
| 512 |
$value = $callback($item); |
| 513 |
|
| 514 |
return is_null($result) || $value > $result ? $value : $result; |
| 515 |
}); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public function forPage($page, $perPage) |
| 526 |
{ |
| 527 |
$offset = max(0, ($page - 1) * $perPage); |
| 528 |
|
| 529 |
return $this->slice($offset, $perPage); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public function partition($key, $operator = null, $value = null) |
| 541 |
{ |
| 542 |
$callback = func_num_args() === 1 |
| 543 |
? $this->valueRetriever($key) |
| 544 |
: $this->operatorForWhere(...func_get_args()); |
| 545 |
|
| 546 |
[$passed, $failed] = Arr::partition($this->getIterator(), $callback); |
| 547 |
|
| 548 |
return new static([new static($passed), new static($failed)]); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public function percentage(callable $callback, int $precision = 2) |
| 559 |
{ |
| 560 |
if ($this->isEmpty()) { |
| 561 |
return null; |
| 562 |
} |
| 563 |
|
| 564 |
return round( |
| 565 |
$this->filter($callback)->count() / $this->count() * 100, |
| 566 |
$precision |
| 567 |
); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public function sum($callback = null) |
| 579 |
{ |
| 580 |
$callback = is_null($callback) |
| 581 |
? $this->identity() |
| 582 |
: $this->valueRetriever($callback); |
| 583 |
|
| 584 |
return $this->reduce(fn ($result, $item) => $result + $callback($item), 0); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function whenEmpty(callable $callback, ?callable $default = null) |
| 597 |
{ |
| 598 |
return $this->when($this->isEmpty(), $callback, $default); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public function whenNotEmpty(callable $callback, ?callable $default = null) |
| 611 |
{ |
| 612 |
return $this->when($this->isNotEmpty(), $callback, $default); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public function unlessEmpty(callable $callback, ?callable $default = null) |
| 625 |
{ |
| 626 |
return $this->whenNotEmpty($callback, $default); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public function unlessNotEmpty(callable $callback, ?callable $default = null) |
| 639 |
{ |
| 640 |
return $this->whenEmpty($callback, $default); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public function where($key, $operator = null, $value = null) |
| 652 |
{ |
| 653 |
return $this->filter($this->operatorForWhere(...func_get_args())); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function whereNull($key = null) |
| 663 |
{ |
| 664 |
return $this->whereStrict($key, null); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
public function whereNotNull($key = null) |
| 674 |
{ |
| 675 |
return $this->where($key, '!==', null); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public function whereStrict($key, $value) |
| 686 |
{ |
| 687 |
return $this->where($key, '===', $value); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
public function whereIn($key, $values, $strict = false) |
| 699 |
{ |
| 700 |
$values = $this->getArrayableItems($values); |
| 701 |
|
| 702 |
return $this->filter(fn ($item) => in_array(data_get($item, $key), $values, $strict)); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public function whereInStrict($key, $values) |
| 713 |
{ |
| 714 |
return $this->whereIn($key, $values, true); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public function whereBetween($key, $values) |
| 725 |
{ |
| 726 |
return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public function whereNotBetween($key, $values) |
| 737 |
{ |
| 738 |
return $this->filter( |
| 739 |
fn ($item) => data_get($item, $key) < reset($values) || data_get($item, $key) > end($values) |
| 740 |
); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
public function whereNotIn($key, $values, $strict = false) |
| 752 |
{ |
| 753 |
$values = $this->getArrayableItems($values); |
| 754 |
|
| 755 |
return $this->reject(fn ($item) => in_array(data_get($item, $key), $values, $strict)); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
public function whereNotInStrict($key, $values) |
| 766 |
{ |
| 767 |
return $this->whereNotIn($key, $values, true); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
public function whereInstanceOf($type) |
| 779 |
{ |
| 780 |
return $this->filter(function ($value) use ($type) { |
| 781 |
if (is_array($type)) { |
| 782 |
foreach ($type as $classType) { |
| 783 |
if ($value instanceof $classType) { |
| 784 |
return true; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
return false; |
| 789 |
} |
| 790 |
|
| 791 |
return $value instanceof $type; |
| 792 |
}); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public function pipe(callable $callback) |
| 804 |
{ |
| 805 |
return $callback($this); |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
public function pipeInto($class) |
| 817 |
{ |
| 818 |
return new $class($this); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
public function pipeThrough($callbacks) |
| 828 |
{ |
| 829 |
return (new Collection($callbacks))->reduce( |
| 830 |
fn ($carry, $callback) => $callback($carry), |
| 831 |
$this, |
| 832 |
); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
public function reduce(callable $callback, $initial = null) |
| 846 |
{ |
| 847 |
$result = $initial; |
| 848 |
|
| 849 |
foreach ($this as $key => $value) { |
| 850 |
$result = $callback($result, $value, $key); |
| 851 |
} |
| 852 |
|
| 853 |
return $result; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
public function reduceSpread(callable $callback, ...$initial) |
| 866 |
{ |
| 867 |
$result = $initial; |
| 868 |
|
| 869 |
foreach ($this as $key => $value) { |
| 870 |
$result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
| 871 |
|
| 872 |
if (! is_array($result)) { |
| 873 |
throw new UnexpectedValueException(sprintf( |
| 874 |
"%s::reduceSpread expects reducer to return an array, but got a '%s' instead.", |
| 875 |
class_basename(static::class), gettype($result) |
| 876 |
)); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
return $result; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
public function reduceWithKeys(callable $callback, $initial = null) |
| 894 |
{ |
| 895 |
return $this->reduce($callback, $initial); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
public function reject($callback = true) |
| 905 |
{ |
| 906 |
$useAsCallable = $this->useAsCallable($callback); |
| 907 |
|
| 908 |
return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
| 909 |
return $useAsCallable |
| 910 |
? ! $callback($value, $key) |
| 911 |
: $value != $callback; |
| 912 |
}); |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public function tap(callable $callback) |
| 922 |
{ |
| 923 |
$callback($this); |
| 924 |
|
| 925 |
return $this; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
public function unique($key = null, $strict = false) |
| 936 |
{ |
| 937 |
$callback = $this->valueRetriever($key); |
| 938 |
|
| 939 |
$exists = []; |
| 940 |
|
| 941 |
return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { |
| 942 |
if (in_array($id = $callback($item, $key), $exists, $strict)) { |
| 943 |
return true; |
| 944 |
} |
| 945 |
|
| 946 |
$exists[] = $id; |
| 947 |
}); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
public function uniqueStrict($key = null) |
| 957 |
{ |
| 958 |
return $this->unique($key, true); |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
public function collect() |
| 967 |
{ |
| 968 |
return new Collection($this->all()); |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
public function toArray() |
| 977 |
{ |
| 978 |
return $this->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value)->all(); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
public function jsonSerialize(): array |
| 987 |
{ |
| 988 |
return array_map(function ($value) { |
| 989 |
return match (true) { |
| 990 |
$value instanceof JsonSerializable => $value->jsonSerialize(), |
| 991 |
$value instanceof Jsonable => json_decode($value->toJson(), true), |
| 992 |
$value instanceof Arrayable => $value->toArray(), |
| 993 |
default => $value, |
| 994 |
}; |
| 995 |
}, $this->all()); |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
public function toJson($options = 0) |
| 1005 |
{ |
| 1006 |
return json_encode($this->jsonSerialize(), $options); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
public function toPrettyJson(int $options = 0) |
| 1016 |
{ |
| 1017 |
return $this->toJson(JSON_PRETTY_PRINT | $options); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
| 1027 |
{ |
| 1028 |
return new CachingIterator($this->getIterator(), $flags); |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
public function __toString() |
| 1037 |
{ |
| 1038 |
return $this->escapeWhenCastingToString |
| 1039 |
? e($this->toJson()) |
| 1040 |
: $this->toJson(); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public function escapeWhenCastingToString($escape = true) |
| 1050 |
{ |
| 1051 |
$this->escapeWhenCastingToString = $escape; |
| 1052 |
|
| 1053 |
return $this; |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
public static function proxy($method) |
| 1063 |
{ |
| 1064 |
static::$proxies[] = $method; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
public function __get($key) |
| 1076 |
{ |
| 1077 |
if (! in_array($key, static::$proxies)) { |
| 1078 |
throw new Exception("Property [{$key}] does not exist on this collection instance."); |
| 1079 |
} |
| 1080 |
|
| 1081 |
return new HigherOrderCollectionProxy($this, $key); |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
protected function getArrayableItems($items) |
| 1091 |
{ |
| 1092 |
return is_null($items) || is_scalar($items) || $items instanceof UnitEnum |
| 1093 |
? Arr::wrap($items) |
| 1094 |
: Arr::from($items); |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
protected function operatorForWhere($key, $operator = null, $value = null) |
| 1106 |
{ |
| 1107 |
if ($this->useAsCallable($key)) { |
| 1108 |
return $key; |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (func_num_args() === 1) { |
| 1112 |
$value = true; |
| 1113 |
|
| 1114 |
$operator = '='; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (func_num_args() === 2) { |
| 1118 |
$value = $operator; |
| 1119 |
|
| 1120 |
$operator = '='; |
| 1121 |
} |
| 1122 |
|
| 1123 |
return function ($item) use ($key, $operator, $value) { |
| 1124 |
$retrieved = enum_value(data_get($item, $key)); |
| 1125 |
$value = enum_value($value); |
| 1126 |
|
| 1127 |
$strings = array_filter([$retrieved, $value], function ($value) { |
| 1128 |
return match (true) { |
| 1129 |
is_string($value) => true, |
| 1130 |
$value instanceof \Stringable => true, |
| 1131 |
default => false, |
| 1132 |
}; |
| 1133 |
}); |
| 1134 |
|
| 1135 |
if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
| 1136 |
return in_array($operator, ['!=', '<>', '!==']); |
| 1137 |
} |
| 1138 |
|
| 1139 |
switch ($operator) { |
| 1140 |
default: |
| 1141 |
case '=': |
| 1142 |
case '==': return $retrieved == $value; |
| 1143 |
case '!=': |
| 1144 |
case '<>': return $retrieved != $value; |
| 1145 |
case '<': return $retrieved < $value; |
| 1146 |
case '>': return $retrieved > $value; |
| 1147 |
case '<=': return $retrieved <= $value; |
| 1148 |
case '>=': return $retrieved >= $value; |
| 1149 |
case '===': return $retrieved === $value; |
| 1150 |
case '!==': return $retrieved !== $value; |
| 1151 |
case '<=>': return $retrieved <=> $value; |
| 1152 |
} |
| 1153 |
}; |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
protected function useAsCallable($value) |
| 1163 |
{ |
| 1164 |
return ! is_string($value) && is_callable($value); |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
protected function valueRetriever($value) |
| 1174 |
{ |
| 1175 |
if ($this->useAsCallable($value)) { |
| 1176 |
return $value; |
| 1177 |
} |
| 1178 |
|
| 1179 |
return fn ($item) => data_get($item, $value); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
protected function equality($value) |
| 1189 |
{ |
| 1190 |
return fn ($item) => $item === $value; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
protected function negate(Closure $callback) |
| 1200 |
{ |
| 1201 |
return fn (...$params) => ! $callback(...$params); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
protected function identity() |
| 1210 |
{ |
| 1211 |
return fn ($value) => $value; |
| 1212 |
} |
| 1213 |
} |