Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Arrayy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Arrayy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Arrayy extends \ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \JsonSerializable, \Countable |
||
| 28 | { |
||
| 29 | const ARRAYY_HELPER_TYPES_FOR_ALL_PROPERTIES = '!!!!Arrayy_Helper_Types_For_All_Properties!!!!'; |
||
| 30 | |||
| 31 | const ARRAYY_HELPER_WALK = '!!!!Arrayy_Helper_Walk!!!!'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | * |
||
| 36 | * @psalm-var array<mixed,mixed>|array<TKey,T> |
||
| 37 | */ |
||
| 38 | protected $array = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \Arrayy\ArrayyRewindableGenerator|null |
||
| 42 | * |
||
| 43 | * @psalm-var \Arrayy\ArrayyRewindableGenerator<TKey,T>|null |
||
| 44 | */ |
||
| 45 | protected $generator; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | * |
||
| 50 | * @psalm-var class-string<\Arrayy\ArrayyIterator> |
||
| 51 | */ |
||
| 52 | protected $iteratorClass = ArrayyIterator::class; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $pathSeparator = '.'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $checkPropertyTypes = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $checkForMissingPropertiesInConstructor = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $checkPropertiesMismatchInConstructor = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $checkPropertiesMismatch = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array<int|string,TypeCheckInterface>|mixed|TypeCheckArray<int|string,TypeCheckInterface>|TypeInterface |
||
| 81 | */ |
||
| 82 | protected $properties = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initializes |
||
| 86 | * |
||
| 87 | * @param mixed $data <p> |
||
| 88 | * Should be an array or a generator, otherwise it will try |
||
| 89 | * to convert it into an array. |
||
| 90 | * </p> |
||
| 91 | * @param string $iteratorClass optional <p> |
||
| 92 | * You can overwrite the ArrayyIterator, but mostly you don't |
||
| 93 | * need this option. |
||
| 94 | * </p> |
||
| 95 | * @param bool $checkPropertiesInConstructor optional <p> |
||
| 96 | * You need to extend the "Arrayy"-class and you need to set |
||
| 97 | * the $checkPropertiesMismatchInConstructor class property |
||
| 98 | * to |
||
| 99 | * true, otherwise this option didn't not work anyway. |
||
| 100 | * </p> |
||
| 101 | * |
||
| 102 | * @psalm-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 103 | */ |
||
| 104 | 1206 | public function __construct( |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 51 | public function __clone() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Call object as function. |
||
| 138 | * |
||
| 139 | * @param mixed $key |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | 1 | public function __invoke($key = null) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Whether or not an element exists by key. |
||
| 156 | * |
||
| 157 | * @param mixed $key |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | * <p>True is the key/index exists, otherwise false.</p> |
||
| 161 | */ |
||
| 162 | public function __isset($key): bool |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Assigns a value to the specified element. |
||
| 169 | * |
||
| 170 | * @param mixed $key |
||
| 171 | * @param mixed $value |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | 3 | public function __set($key, $value) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * magic to string |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | 15 | public function __toString(): string |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Unset element by key. |
||
| 192 | * |
||
| 193 | * @param mixed $key |
||
| 194 | */ |
||
| 195 | public function __unset($key) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get a value by key. |
||
| 202 | * |
||
| 203 | * @param mixed $key |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | * <p>Get a Value from the current array.</p> |
||
| 207 | */ |
||
| 208 | 128 | public function &__get($key) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Add new values (optional using dot-notation). |
||
| 221 | * |
||
| 222 | * @param mixed $value |
||
| 223 | * @param int|string|null $key |
||
| 224 | * |
||
| 225 | * @return static |
||
| 226 | * <p>(Immutable) Return this Arrayy object, with the appended values.</p> |
||
| 227 | * |
||
| 228 | * @psalm-param T $value |
||
| 229 | * @psalm-return static<TKey,T> |
||
| 230 | * |
||
| 231 | * @psalm-mutation-free |
||
| 232 | */ |
||
| 233 | 13 | public function add($value, $key = null) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Append a (key) + value to the current array. |
||
| 254 | * |
||
| 255 | * EXAMPLE: <code> |
||
| 256 | * a(['fòô' => 'bàř'])->append('foo'); // Arrayy['fòô' => 'bàř', 0 => 'foo'] |
||
| 257 | * </code> |
||
| 258 | * |
||
| 259 | * @param mixed $value |
||
| 260 | * @param mixed $key |
||
| 261 | * |
||
| 262 | * @return $this |
||
| 263 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 264 | * |
||
| 265 | * @psalm-return static<TKey,T> |
||
| 266 | */ |
||
| 267 | 20 | public function append($value, $key = null): self |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Append a (key) + value to the current array. |
||
| 294 | * |
||
| 295 | * EXAMPLE: <code> |
||
| 296 | * a(['fòô' => 'bàř'])->appendImmutable('foo')->getArray(); // ['fòô' => 'bàř', 0 => 'foo'] |
||
| 297 | * </code> |
||
| 298 | * |
||
| 299 | * @param mixed $value |
||
| 300 | * @param mixed $key |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | * <p>(Immutable) Return this Arrayy object, with the appended values.</p> |
||
| 304 | * |
||
| 305 | * @psalm-return static<TKey,T> |
||
| 306 | * @psalm-mutation-free |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function appendImmutable($value, $key = null): self |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Sort the entries by value. |
||
| 336 | * |
||
| 337 | * @param int $sort_flags [optional] <p> |
||
| 338 | * You may modify the behavior of the sort using the optional |
||
| 339 | * parameter sort_flags, for details |
||
| 340 | * see sort. |
||
| 341 | * </p> |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 345 | * |
||
| 346 | * @psalm-return static<TKey,T> |
||
| 347 | */ |
||
| 348 | 4 | public function asort(int $sort_flags = 0): self |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Sort the entries by value. |
||
| 359 | * |
||
| 360 | * @param int $sort_flags [optional] <p> |
||
| 361 | * You may modify the behavior of the sort using the optional |
||
| 362 | * parameter sort_flags, for details |
||
| 363 | * see sort. |
||
| 364 | * </p> |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 368 | * |
||
| 369 | * @psalm-return static<TKey,T> |
||
| 370 | * @psalm-mutation-free |
||
| 371 | */ |
||
| 372 | 4 | public function asortImmutable(int $sort_flags = 0): self |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Counts all elements in an array, or something in an object. |
||
| 386 | * |
||
| 387 | * EXAMPLE: <code> |
||
| 388 | * a([-9, -8, -7, 1.32])->count(); // 4 |
||
| 389 | * </code> |
||
| 390 | * |
||
| 391 | * <p> |
||
| 392 | * For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}. |
||
| 393 | * The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() |
||
| 394 | * function. Please see the {@see Array} section of the manual for a detailed explanation of how arrays are |
||
| 395 | * implemented and used in PHP. |
||
| 396 | * </p> |
||
| 397 | * |
||
| 398 | * @see http://php.net/manual/en/function.count.php |
||
| 399 | * |
||
| 400 | * @param int $mode [optional] If the optional mode parameter is set to |
||
| 401 | * COUNT_RECURSIVE (or 1), count |
||
| 402 | * will recursively count the array. This is particularly useful for |
||
| 403 | * counting all the elements of a multidimensional array. count does not detect infinite recursion. |
||
| 404 | * |
||
| 405 | * @return int |
||
| 406 | * <p> |
||
| 407 | * The number of elements in var, which is |
||
| 408 | * typically an array, since anything else will have one |
||
| 409 | * element. |
||
| 410 | * </p> |
||
| 411 | * <p> |
||
| 412 | * If var is not an array or an object with |
||
| 413 | * implemented Countable interface, |
||
| 414 | * 1 will be returned. |
||
| 415 | * There is one exception, if var is &null;, |
||
| 416 | * 0 will be returned. |
||
| 417 | * </p> |
||
| 418 | * <p> |
||
| 419 | * Caution: count may return 0 for a variable that isn't set, |
||
| 420 | * but it may also return 0 for a variable that has been initialized with an |
||
| 421 | * empty array. Use isset to test if a variable is set. |
||
| 422 | * </p> |
||
| 423 | * @psalm-mutation-free |
||
| 424 | */ |
||
| 425 | 147 | public function count(int $mode = \COUNT_NORMAL): int |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Exchange the array for another one. |
||
| 440 | * |
||
| 441 | * @param array|static $data |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | * |
||
| 445 | * @psalm-param array<TKey,T>|self<TKey,T> $data |
||
| 446 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 447 | */ |
||
| 448 | 1 | public function exchangeArray($data): array |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Creates a copy of the ArrayyObject. |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | * |
||
| 460 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 461 | */ |
||
| 462 | 6 | public function getArrayCopy(): array |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Returns a new iterator, thus implementing the \Iterator interface. |
||
| 471 | * |
||
| 472 | * EXAMPLE: <code> |
||
| 473 | * a(['foo', 'bar'])->getIterator(); // ArrayyIterator['foo', 'bar'] |
||
| 474 | * </code> |
||
| 475 | * |
||
| 476 | * @return \Iterator<mixed, mixed> |
||
| 477 | * <p>An iterator for the values in the array.</p> |
||
| 478 | * @psalm-return \Iterator<array-key|TKey, mixed|T> |
||
| 479 | */ |
||
| 480 | 27 | public function getIterator(): \Iterator |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Gets the iterator classname for the ArrayObject. |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | * |
||
| 503 | * @psalm-return class-string |
||
| 504 | */ |
||
| 505 | 26 | public function getIteratorClass(): string |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Sort the entries by key. |
||
| 512 | * |
||
| 513 | * @param int $sort_flags [optional] <p> |
||
| 514 | * You may modify the behavior of the sort using the optional |
||
| 515 | * parameter sort_flags, for details |
||
| 516 | * see sort. |
||
| 517 | * </p> |
||
| 518 | * |
||
| 519 | * @return $this |
||
| 520 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 521 | * |
||
| 522 | * @psalm-return static<TKey,T> |
||
| 523 | */ |
||
| 524 | 4 | public function ksort(int $sort_flags = 0): self |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Sort the entries by key. |
||
| 535 | * |
||
| 536 | * @param int $sort_flags [optional] <p> |
||
| 537 | * You may modify the behavior of the sort using the optional |
||
| 538 | * parameter sort_flags, for details |
||
| 539 | * see sort. |
||
| 540 | * </p> |
||
| 541 | * |
||
| 542 | * @return $this |
||
| 543 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 544 | * |
||
| 545 | * @psalm-return static<TKey,T> |
||
| 546 | */ |
||
| 547 | 4 | public function ksortImmutable(int $sort_flags = 0): self |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 561 | * |
||
| 562 | * @return $this |
||
| 563 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 564 | * |
||
| 565 | * @psalm-return static<TKey,T> |
||
| 566 | */ |
||
| 567 | 8 | public function natcasesort(): self |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 578 | * |
||
| 579 | * @return $this |
||
| 580 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 581 | * |
||
| 582 | * @psalm-return static<TKey,T> |
||
| 583 | * @psalm-mutation-free |
||
| 584 | */ |
||
| 585 | 4 | public function natcasesortImmutable(): self |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Sort entries using a "natural order" algorithm. |
||
| 599 | * |
||
| 600 | * @return $this |
||
| 601 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 602 | * |
||
| 603 | * @psalm-return static<TKey,T> |
||
| 604 | */ |
||
| 605 | 10 | public function natsort(): self |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Sort entries using a "natural order" algorithm. |
||
| 616 | * |
||
| 617 | * @return $this |
||
| 618 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 619 | * |
||
| 620 | * @psalm-return static<TKey,T> |
||
| 621 | * @psalm-mutation-free |
||
| 622 | */ |
||
| 623 | 4 | public function natsortImmutable(): self |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Whether or not an offset exists. |
||
| 637 | * |
||
| 638 | * @param bool|int|string $offset |
||
| 639 | * |
||
| 640 | * @return bool |
||
| 641 | * |
||
| 642 | * @noinspection PhpSillyAssignmentInspection |
||
| 643 | * |
||
| 644 | * @psalm-mutation-free |
||
| 645 | */ |
||
| 646 | 159 | public function offsetExists($offset): bool |
|
| 647 | { |
||
| 648 | 159 | $this->generatorToArray(); |
|
| 649 | |||
| 650 | 159 | if ($this->array === []) { |
|
| 651 | 8 | return false; |
|
| 652 | } |
||
| 653 | |||
| 654 | // php cast "bool"-index into "int"-index |
||
| 655 | 153 | if ((bool) $offset === $offset) { |
|
| 656 | 1 | $offset = (int) $offset; |
|
| 657 | } |
||
| 658 | |||
| 659 | /** @var int|string $offset - hint for phpstan */ |
||
| 660 | 153 | $offset = $offset; |
|
| 661 | |||
| 662 | 153 | $tmpReturn = $this->keyExists($offset); |
|
| 663 | |||
| 664 | if ( |
||
| 665 | 153 | $tmpReturn === true |
|
| 666 | || |
||
| 667 | 153 | \strpos((string) $offset, $this->pathSeparator) === false |
|
| 668 | ) { |
||
| 669 | 150 | return $tmpReturn; |
|
| 670 | } |
||
| 671 | |||
| 672 | 4 | $offsetExists = false; |
|
| 673 | |||
| 674 | /** |
||
| 675 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 676 | * |
||
| 677 | * @psalm-suppress PossiblyInvalidArgument |
||
| 678 | * @psalm-suppress InvalidScalarArgument |
||
| 679 | */ |
||
| 680 | View Code Duplication | if ( |
|
| 681 | 4 | $this->pathSeparator |
|
| 682 | && |
||
| 683 | 4 | (string) $offset === $offset |
|
| 684 | && |
||
| 685 | 4 | \strpos($offset, $this->pathSeparator) !== false |
|
| 686 | ) { |
||
| 687 | 4 | $explodedPath = \explode($this->pathSeparator, (string) $offset); |
|
| 688 | 4 | if ($explodedPath !== false) { |
|
| 689 | /** @var string $lastOffset - helper for phpstan */ |
||
| 690 | 4 | $lastOffset = \array_pop($explodedPath); |
|
| 691 | 4 | $containerPath = \implode($this->pathSeparator, $explodedPath); |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @psalm-suppress MissingClosureReturnType |
||
| 695 | * @psalm-suppress MissingClosureParamType |
||
| 696 | */ |
||
| 697 | 4 | $this->callAtPath( |
|
| 698 | 4 | $containerPath, |
|
| 699 | 4 | static function ($container) use ($lastOffset, &$offsetExists) { |
|
| 700 | 4 | $offsetExists = \array_key_exists($lastOffset, $container); |
|
| 701 | 4 | } |
|
| 702 | ); |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | 4 | return $offsetExists; |
|
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Returns the value at specified offset. |
||
| 711 | * |
||
| 712 | * @param int|string $offset |
||
| 713 | * |
||
| 714 | * @return mixed |
||
| 715 | * <p>Will return null if the offset did not exists.</p> |
||
| 716 | */ |
||
| 717 | 128 | public function &offsetGet($offset) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Assigns a value to the specified offset + check the type. |
||
| 731 | * |
||
| 732 | * @param int|string|null $offset |
||
| 733 | * @param mixed $value |
||
| 734 | * |
||
| 735 | * @return void |
||
| 736 | */ |
||
| 737 | 28 | public function offsetSet($offset, $value) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Unset an offset. |
||
| 758 | * |
||
| 759 | * @param int|string $offset |
||
| 760 | * |
||
| 761 | * @return void |
||
| 762 | * <p>(Mutable) Return nothing.</p> |
||
| 763 | */ |
||
| 764 | 25 | public function offsetUnset($offset) |
|
| 765 | { |
||
| 766 | 25 | $this->generatorToArray(); |
|
| 767 | |||
| 768 | 25 | if ($this->array === []) { |
|
| 769 | 6 | return; |
|
| 770 | } |
||
| 771 | |||
| 772 | 20 | if ($this->keyExists($offset)) { |
|
| 773 | 13 | unset($this->array[$offset]); |
|
| 774 | |||
| 775 | 13 | return; |
|
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 780 | * |
||
| 781 | * @psalm-suppress PossiblyInvalidArgument |
||
| 782 | * @psalm-suppress InvalidScalarArgument |
||
| 783 | */ |
||
| 784 | View Code Duplication | if ( |
|
| 785 | 10 | $this->pathSeparator |
|
| 786 | && |
||
| 787 | 10 | (string) $offset === $offset |
|
| 788 | && |
||
| 789 | 10 | \strpos($offset, $this->pathSeparator) !== false |
|
| 790 | ) { |
||
| 791 | 7 | $path = \explode($this->pathSeparator, (string) $offset); |
|
| 792 | |||
| 793 | 7 | if ($path !== false) { |
|
| 794 | 7 | $pathToUnset = \array_pop($path); |
|
| 795 | |||
| 796 | /** |
||
| 797 | * @psalm-suppress MissingClosureReturnType |
||
| 798 | * @psalm-suppress MissingClosureParamType |
||
| 799 | */ |
||
| 800 | 7 | $this->callAtPath( |
|
| 801 | 7 | \implode($this->pathSeparator, $path), |
|
| 802 | 7 | static function (&$offset) use ($pathToUnset) { |
|
| 803 | 6 | if (\is_array($offset)) { |
|
| 804 | 5 | unset($offset[$pathToUnset]); |
|
| 805 | } else { |
||
| 806 | 1 | $offset = null; |
|
| 807 | } |
||
| 808 | 7 | } |
|
| 809 | ); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | 10 | unset($this->array[$offset]); |
|
| 814 | 10 | } |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Serialize the current "Arrayy"-object. |
||
| 818 | * |
||
| 819 | * EXAMPLE: <code> |
||
| 820 | * a([1, 4, 7])->serialize(); |
||
| 821 | * </code> |
||
| 822 | * |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | 2 | public function serialize(): string |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Sets the iterator classname for the current "Arrayy"-object. |
||
| 838 | * |
||
| 839 | * @param string $iteratorClass |
||
| 840 | * |
||
| 841 | * @throws \InvalidArgumentException |
||
| 842 | * |
||
| 843 | * @return void |
||
| 844 | * |
||
| 845 | * @psalm-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 846 | */ |
||
| 847 | 1196 | public function setIteratorClass($iteratorClass) |
|
| 869 | |||
| 870 | /** |
||
| 871 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 872 | * |
||
| 873 | * @param callable $function |
||
| 874 | * |
||
| 875 | * @throws \InvalidArgumentException |
||
| 876 | * |
||
| 877 | * @return $this |
||
| 878 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 879 | * |
||
| 880 | * @psalm-return static<TKey,T> |
||
| 881 | */ |
||
| 882 | 8 | View Code Duplication | public function uasort($function): self |
| 894 | |||
| 895 | /** |
||
| 896 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 897 | * |
||
| 898 | * @param callable $function |
||
| 899 | * |
||
| 900 | * @throws \InvalidArgumentException |
||
| 901 | * |
||
| 902 | * @return $this |
||
| 903 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 904 | * |
||
| 905 | * @psalm-return static<TKey,T> |
||
| 906 | * @psalm-mutation-free |
||
| 907 | */ |
||
| 908 | 4 | public function uasortImmutable($function): self |
|
| 919 | |||
| 920 | /** |
||
| 921 | * Sort the entries by keys using a user-defined comparison function. |
||
| 922 | * |
||
| 923 | * @param callable $function |
||
| 924 | * |
||
| 925 | * @throws \InvalidArgumentException |
||
| 926 | * |
||
| 927 | * @return static |
||
| 928 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 929 | * |
||
| 930 | * @psalm-return static<TKey,T> |
||
| 931 | */ |
||
| 932 | 5 | public function uksort($function): self |
|
| 936 | |||
| 937 | /** |
||
| 938 | * Sort the entries by keys using a user-defined comparison function. |
||
| 939 | * |
||
| 940 | * @param callable $function |
||
| 941 | * |
||
| 942 | * @throws \InvalidArgumentException |
||
| 943 | * |
||
| 944 | * @return static |
||
| 945 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 946 | * |
||
| 947 | * @psalm-return static<TKey,T> |
||
| 948 | * @psalm-mutation-free |
||
| 949 | */ |
||
| 950 | 1 | public function uksortImmutable($function): self |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Unserialize an string and return the instance of the "Arrayy"-class. |
||
| 957 | * |
||
| 958 | * EXAMPLE: <code> |
||
| 959 | * $serialized = a([1, 4, 7])->serialize(); |
||
| 960 | * a()->unserialize($serialized); |
||
| 961 | * </code> |
||
| 962 | * |
||
| 963 | * @param string $string |
||
| 964 | * |
||
| 965 | * @return $this |
||
| 966 | * |
||
| 967 | * @psalm-return static<TKey,T> |
||
| 968 | */ |
||
| 969 | 2 | public function unserialize($string): self |
|
| 979 | |||
| 980 | /** |
||
| 981 | * Append a (key) + values to the current array. |
||
| 982 | * |
||
| 983 | * EXAMPLE: <code> |
||
| 984 | * a(['fòô' => ['bàř']])->appendArrayValues(['foo1', 'foo2'], 'fòô'); // Arrayy['fòô' => ['bàř', 'foo1', 'foo2']] |
||
| 985 | * </code> |
||
| 986 | * |
||
| 987 | * @param array $values |
||
| 988 | * @param mixed $key |
||
| 989 | * |
||
| 990 | * @return $this |
||
| 991 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 992 | * |
||
| 993 | * @psalm-param array<mixed,T> $values |
||
| 994 | * @psalm-param TKey|null $key |
||
| 995 | * @psalm-return static<TKey,T> |
||
| 996 | */ |
||
| 997 | 1 | public function appendArrayValues(array $values, $key = null) |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Add a suffix to each key. |
||
| 1026 | * |
||
| 1027 | * @param mixed $prefix |
||
| 1028 | * |
||
| 1029 | * @return static |
||
| 1030 | * <p>(Immutable) Return an Arrayy object, with the prefixed keys.</p> |
||
| 1031 | * |
||
| 1032 | * @psalm-return static<TKey,T> |
||
| 1033 | * @psalm-mutation-free |
||
| 1034 | */ |
||
| 1035 | 10 | View Code Duplication | public function appendToEachKey($prefix): self |
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Add a prefix to each value. |
||
| 1057 | * |
||
| 1058 | * @param mixed $prefix |
||
| 1059 | * |
||
| 1060 | * @return static |
||
| 1061 | * <p>(Immutable) Return an Arrayy object, with the prefixed values.</p> |
||
| 1062 | * |
||
| 1063 | * @psalm-return static<TKey,T> |
||
| 1064 | * @psalm-mutation-free |
||
| 1065 | */ |
||
| 1066 | 10 | View Code Duplication | public function appendToEachValue($prefix): self |
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Sort an array in reverse order and maintain index association. |
||
| 1088 | * |
||
| 1089 | * @return $this |
||
| 1090 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1091 | * |
||
| 1092 | * @psalm-return static<TKey,T> |
||
| 1093 | */ |
||
| 1094 | 4 | public function arsort(): self |
|
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Sort an array in reverse order and maintain index association. |
||
| 1105 | * |
||
| 1106 | * @return $this |
||
| 1107 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 1108 | * |
||
| 1109 | * @psalm-return static<TKey,T> |
||
| 1110 | * @psalm-mutation-free |
||
| 1111 | */ |
||
| 1112 | 10 | public function arsortImmutable(): self |
|
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Iterate over the current array and execute a callback for each loop. |
||
| 1125 | * |
||
| 1126 | * EXAMPLE: <code> |
||
| 1127 | * $result = A::create(); |
||
| 1128 | * $closure = function ($value, $key) use ($result) { |
||
| 1129 | * $result[$key] = ':' . $value . ':'; |
||
| 1130 | * }; |
||
| 1131 | * a(['foo', 'bar' => 'bis'])->at($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 1132 | * </code> |
||
| 1133 | * |
||
| 1134 | * @param \Closure $closure |
||
| 1135 | * |
||
| 1136 | * @return static |
||
| 1137 | * <p>(Immutable)</p> |
||
| 1138 | * |
||
| 1139 | * @psalm-return static<TKey,T> |
||
| 1140 | * @psalm-mutation-free |
||
| 1141 | */ |
||
| 1142 | 3 | public function at(\Closure $closure): self |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Returns the average value of the current array. |
||
| 1159 | * |
||
| 1160 | * EXAMPLE: <code> |
||
| 1161 | * a([-9, -8, -7, 1.32])->average(2); // -5.67 |
||
| 1162 | * </code> |
||
| 1163 | * |
||
| 1164 | * @param int $decimals <p>The number of decimal-numbers to return.</p> |
||
| 1165 | * |
||
| 1166 | * @return float|int |
||
| 1167 | * <p>The average value.</p> |
||
| 1168 | * @psalm-mutation-free |
||
| 1169 | */ |
||
| 1170 | 10 | public function average($decimals = 0) |
|
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Changes all keys in an array. |
||
| 1187 | * |
||
| 1188 | * @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br /> |
||
| 1189 | * or <strong>CASE_LOWER</strong> (default)</p> |
||
| 1190 | * |
||
| 1191 | * @return static |
||
| 1192 | * <p>(Immutable)</p> |
||
| 1193 | * |
||
| 1194 | * @psalm-return static<TKey,T> |
||
| 1195 | * @psalm-mutation-free |
||
| 1196 | */ |
||
| 1197 | 1 | public function changeKeyCase(int $case = \CASE_LOWER): self |
|
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Change the path separator of the array wrapper. |
||
| 1229 | * |
||
| 1230 | * By default, the separator is: "." |
||
| 1231 | * |
||
| 1232 | * @param string $separator <p>Separator to set.</p> |
||
| 1233 | * |
||
| 1234 | * @return $this |
||
| 1235 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1236 | * |
||
| 1237 | * @psalm-return static<TKey,T> |
||
| 1238 | */ |
||
| 1239 | 11 | public function changeSeparator($separator): self |
|
| 1245 | |||
| 1246 | /** |
||
| 1247 | * Create a chunked version of the current array. |
||
| 1248 | * |
||
| 1249 | * EXAMPLE: <code> |
||
| 1250 | * a([-9, -8, -7, 1.32])->chunk(2); // Arrayy[[-9, -8], [-7, 1.32]] |
||
| 1251 | * </code> |
||
| 1252 | * |
||
| 1253 | * @param int $size <p>Size of each chunk.</p> |
||
| 1254 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 1255 | * |
||
| 1256 | * @return static |
||
| 1257 | * <p>(Immutable) A new array of chunks from the original array.</p> |
||
| 1258 | * |
||
| 1259 | * @psalm-return static<TKey,T> |
||
| 1260 | * @psalm-mutation-free |
||
| 1261 | */ |
||
| 1262 | 5 | public function chunk($size, $preserveKeys = false): self |
|
| 1263 | { |
||
| 1264 | 5 | if ($preserveKeys) { |
|
| 1265 | $generator = function () use ($size) { |
||
| 1266 | $values = []; |
||
| 1267 | $tmpCounter = 0; |
||
| 1268 | foreach ($this->getGenerator() as $key => $value) { |
||
| 1269 | ++$tmpCounter; |
||
| 1270 | |||
| 1271 | $values[$key] = $value; |
||
| 1272 | if ($tmpCounter === $size) { |
||
| 1273 | yield $values; |
||
| 1274 | |||
| 1275 | $values = []; |
||
| 1276 | $tmpCounter = 0; |
||
| 1277 | } |
||
| 1278 | } |
||
| 1279 | |||
| 1280 | if ($values !== []) { |
||
| 1281 | yield $values; |
||
| 1282 | } |
||
| 1283 | }; |
||
| 1284 | View Code Duplication | } else { |
|
| 1285 | 5 | $generator = function () use ($size) { |
|
| 1286 | 5 | $values = []; |
|
| 1287 | 5 | $tmpCounter = 0; |
|
| 1288 | 5 | foreach ($this->getGenerator() as $key => $value) { |
|
| 1289 | 5 | ++$tmpCounter; |
|
| 1290 | |||
| 1291 | 5 | $values[] = $value; |
|
| 1292 | 5 | if ($tmpCounter === $size) { |
|
| 1293 | 5 | yield $values; |
|
| 1294 | |||
| 1295 | 5 | $values = []; |
|
| 1296 | 5 | $tmpCounter = 0; |
|
| 1297 | } |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | 5 | if ($values !== []) { |
|
| 1301 | 4 | yield $values; |
|
| 1302 | } |
||
| 1303 | 5 | }; |
|
| 1304 | } |
||
| 1305 | |||
| 1306 | 5 | return static::create( |
|
| 1307 | 5 | $generator, |
|
| 1308 | 5 | $this->iteratorClass, |
|
| 1309 | 5 | false |
|
| 1310 | ); |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Clean all falsy values from the current array. |
||
| 1315 | * |
||
| 1316 | * EXAMPLE: <code> |
||
| 1317 | * a([-8 => -9, 1, 2 => false])->clean(); // Arrayy[-8 => -9, 1] |
||
| 1318 | * </code> |
||
| 1319 | * |
||
| 1320 | * @return static |
||
| 1321 | * <p>(Immutable)</p> |
||
| 1322 | * |
||
| 1323 | * @psalm-return static<TKey,T> |
||
| 1324 | * @psalm-mutation-free |
||
| 1325 | */ |
||
| 1326 | 8 | public function clean(): self |
|
| 1327 | { |
||
| 1328 | 8 | return $this->filter( |
|
| 1329 | 8 | static function ($value) { |
|
| 1330 | 7 | return (bool) $value; |
|
| 1331 | 8 | } |
|
| 1332 | ); |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * WARNING!!! -> Clear the current full array or a $key of it. |
||
| 1337 | * |
||
| 1338 | * EXAMPLE: <code> |
||
| 1339 | * a([-8 => -9, 1, 2 => false])->clear(); // Arrayy[] |
||
| 1340 | * </code> |
||
| 1341 | * |
||
| 1342 | * @param int|int[]|string|string[]|null $key |
||
| 1343 | * |
||
| 1344 | * @return $this |
||
| 1345 | * <p>(Mutable) Return this Arrayy object, with an empty array.</p> |
||
| 1346 | * |
||
| 1347 | * @psalm-return static<TKey,T> |
||
| 1348 | */ |
||
| 1349 | 10 | public function clear($key = null): self |
|
| 1368 | |||
| 1369 | /** |
||
| 1370 | * Check if an item is in the current array. |
||
| 1371 | * |
||
| 1372 | * EXAMPLE: <code> |
||
| 1373 | * a([1, true])->contains(true); // true |
||
| 1374 | * </code> |
||
| 1375 | * |
||
| 1376 | * @param float|int|string $value |
||
| 1377 | * @param bool $recursive |
||
| 1378 | * @param bool $strict |
||
| 1379 | * |
||
| 1380 | * @return bool |
||
| 1381 | * @psalm-mutation-free |
||
| 1382 | */ |
||
| 1383 | 23 | public function contains($value, bool $recursive = false, bool $strict = true): bool |
|
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Check if an (case-insensitive) string is in the current array. |
||
| 1408 | * |
||
| 1409 | * EXAMPLE: <code> |
||
| 1410 | * a(['E', 'é'])->containsCaseInsensitive('É'); // true |
||
| 1411 | * </code> |
||
| 1412 | * |
||
| 1413 | * @param mixed $value |
||
| 1414 | * @param bool $recursive |
||
| 1415 | * |
||
| 1416 | * @return bool |
||
| 1417 | * @psalm-mutation-free |
||
| 1418 | * |
||
| 1419 | * @psalm-suppress InvalidCast - hack for int|float|bool support |
||
| 1420 | */ |
||
| 1421 | 26 | public function containsCaseInsensitive($value, $recursive = false): bool |
|
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Check if the given key/index exists in the array. |
||
| 1455 | * |
||
| 1456 | * EXAMPLE: <code> |
||
| 1457 | * a([1 => true])->containsKey(1); // true |
||
| 1458 | * </code> |
||
| 1459 | * |
||
| 1460 | * @param int|string $key <p>key/index to search for</p> |
||
| 1461 | * |
||
| 1462 | * @return bool |
||
| 1463 | * <p>Returns true if the given key/index exists in the array, false otherwise.</p> |
||
| 1464 | * |
||
| 1465 | * @psalm-mutation-free |
||
| 1466 | */ |
||
| 1467 | 4 | public function containsKey($key): bool |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Check if all given needles are present in the array as key/index. |
||
| 1474 | * |
||
| 1475 | * EXAMPLE: <code> |
||
| 1476 | * a([1 => true])->containsKeys(array(1 => 0)); // true |
||
| 1477 | * </code> |
||
| 1478 | * |
||
| 1479 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1480 | * @param bool $recursive |
||
| 1481 | * |
||
| 1482 | * @return bool |
||
| 1483 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1484 | * |
||
| 1485 | * @psalm-param array<mixed,mixed>|array<TKey> $needles |
||
| 1486 | * @psalm-mutation-free |
||
| 1487 | */ |
||
| 1488 | 2 | public function containsKeys(array $needles, $recursive = false): bool |
|
| 1516 | |||
| 1517 | /** |
||
| 1518 | * Check if all given needles are present in the array as key/index. |
||
| 1519 | * |
||
| 1520 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1521 | * |
||
| 1522 | * @return bool |
||
| 1523 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1524 | * |
||
| 1525 | * @psalm-param array<mixed,mixed>|array<TKey> $needles |
||
| 1526 | * @psalm-mutation-free |
||
| 1527 | */ |
||
| 1528 | 1 | public function containsKeysRecursive(array $needles): bool |
|
| 1532 | |||
| 1533 | /** |
||
| 1534 | * alias: for "Arrayy->contains()" |
||
| 1535 | * |
||
| 1536 | * @param float|int|string $value |
||
| 1537 | * |
||
| 1538 | * @return bool |
||
| 1539 | * |
||
| 1540 | * @see Arrayy::contains() |
||
| 1541 | * @psalm-mutation-free |
||
| 1542 | */ |
||
| 1543 | 9 | public function containsValue($value): bool |
|
| 1547 | |||
| 1548 | /** |
||
| 1549 | * alias: for "Arrayy->contains($value, true)" |
||
| 1550 | * |
||
| 1551 | * @param float|int|string $value |
||
| 1552 | * |
||
| 1553 | * @return bool |
||
| 1554 | * |
||
| 1555 | * @see Arrayy::contains() |
||
| 1556 | * @psalm-mutation-free |
||
| 1557 | */ |
||
| 1558 | 18 | public function containsValueRecursive($value): bool |
|
| 1562 | |||
| 1563 | /** |
||
| 1564 | * Check if all given needles are present in the array. |
||
| 1565 | * |
||
| 1566 | * EXAMPLE: <code> |
||
| 1567 | * a([1, true])->containsValues(array(1, true)); // true |
||
| 1568 | * </code> |
||
| 1569 | * |
||
| 1570 | * @param array $needles |
||
| 1571 | * |
||
| 1572 | * @return bool |
||
| 1573 | * <p>Returns true if all the given values exists in the array, false otherwise.</p> |
||
| 1574 | * |
||
| 1575 | * @psalm-param array<mixed>|array<T> $needles |
||
| 1576 | * @psalm-mutation-free |
||
| 1577 | */ |
||
| 1578 | 1 | public function containsValues(array $needles): bool |
|
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Counts all the values of an array |
||
| 1596 | * |
||
| 1597 | * @see http://php.net/manual/en/function.array-count-values.php |
||
| 1598 | * |
||
| 1599 | * @return static |
||
| 1600 | * <p> |
||
| 1601 | * (Immutable) |
||
| 1602 | * An associative Arrayy-object of values from input as |
||
| 1603 | * keys and their count as value. |
||
| 1604 | * </p> |
||
| 1605 | * |
||
| 1606 | * @psalm-return static<TKey,T> |
||
| 1607 | * @psalm-mutation-free |
||
| 1608 | */ |
||
| 1609 | 7 | public function countValues(): self |
|
| 1613 | |||
| 1614 | /** |
||
| 1615 | * Creates an Arrayy object. |
||
| 1616 | * |
||
| 1617 | * @param mixed $data |
||
| 1618 | * @param string $iteratorClass |
||
| 1619 | * @param bool $checkPropertiesInConstructor |
||
| 1620 | * |
||
| 1621 | * @return static |
||
| 1622 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1623 | * |
||
| 1624 | * @psalm-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 1625 | * |
||
| 1626 | * @psalm-mutation-free |
||
| 1627 | */ |
||
| 1628 | 724 | public static function create( |
|
| 1639 | |||
| 1640 | /** |
||
| 1641 | * Flatten an array with the given character as a key delimiter. |
||
| 1642 | * |
||
| 1643 | * EXAMPLE: <code> |
||
| 1644 | * $callable = function ($a, $b) { |
||
| 1645 | * if ($a == $b) { |
||
| 1646 | * return 0; |
||
| 1647 | * } |
||
| 1648 | * return ($a > $b) ? 1 : -1; |
||
| 1649 | * }; |
||
| 1650 | * $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]); |
||
| 1651 | * $resultArrayy = $arrayy->customSortKeys($callable); // Arrayy['one' => 1, 'three' => 3, 'two' => 2] |
||
| 1652 | * </code> |
||
| 1653 | * |
||
| 1654 | * @param string $delimiter |
||
| 1655 | * @param string $prepend |
||
| 1656 | * @param array|null $items |
||
| 1657 | * |
||
| 1658 | * @return array |
||
| 1659 | */ |
||
| 1660 | 2 | public function flatten($delimiter = '.', $prepend = '', $items = null) |
|
| 1683 | |||
| 1684 | /** |
||
| 1685 | * WARNING: Creates an Arrayy object by reference. |
||
| 1686 | * |
||
| 1687 | * @param array $array |
||
| 1688 | * |
||
| 1689 | * @return $this |
||
| 1690 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1691 | * |
||
| 1692 | * @psalm-param array<mixed,mixed>|array<array-key,mixed> $array |
||
| 1693 | */ |
||
| 1694 | 26 | public function createByReference(array &$array = []): self |
|
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Create an new instance from a callable function which will return an Generator. |
||
| 1705 | * |
||
| 1706 | * @param callable $generatorFunction |
||
| 1707 | * |
||
| 1708 | * @return static |
||
| 1709 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1710 | * |
||
| 1711 | * @psalm-param callable():\Generator<array-key,mixed> $generatorFunction |
||
| 1712 | * |
||
| 1713 | * @psalm-mutation-free |
||
| 1714 | */ |
||
| 1715 | 7 | public static function createFromGeneratorFunction(callable $generatorFunction): self |
|
| 1719 | |||
| 1720 | /** |
||
| 1721 | * Create an new instance filled with a copy of values from a "Generator"-object. |
||
| 1722 | * |
||
| 1723 | * @param \Generator $generator |
||
| 1724 | * |
||
| 1725 | * @return static |
||
| 1726 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1727 | * |
||
| 1728 | * @psalm-param \Generator<array-key,mixed> $generator |
||
| 1729 | * |
||
| 1730 | * @psalm-mutation-free |
||
| 1731 | */ |
||
| 1732 | 4 | public static function createFromGeneratorImmutable(\Generator $generator): self |
|
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Create an new Arrayy object via JSON. |
||
| 1739 | * |
||
| 1740 | * @param string $json |
||
| 1741 | * |
||
| 1742 | * @return static |
||
| 1743 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1744 | * |
||
| 1745 | * @psalm-mutation-free |
||
| 1746 | */ |
||
| 1747 | 5 | public static function createFromJson(string $json): self |
|
| 1751 | |||
| 1752 | /** |
||
| 1753 | * Create an new Arrayy object via JSON. |
||
| 1754 | * |
||
| 1755 | * @param array $array |
||
| 1756 | * |
||
| 1757 | * @return static |
||
| 1758 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1759 | * |
||
| 1760 | * @psalm-mutation-free |
||
| 1761 | */ |
||
| 1762 | 1 | public static function createFromArray(array $array): self |
|
| 1766 | |||
| 1767 | /** |
||
| 1768 | * Create an new instance filled with values from an object that is iterable. |
||
| 1769 | * |
||
| 1770 | * @param \Traversable $object <p>iterable object</p> |
||
| 1771 | * |
||
| 1772 | * @return static |
||
| 1773 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1774 | * |
||
| 1775 | * @psalm-param \Traversable<array-key,mixed> $object |
||
| 1776 | * |
||
| 1777 | * @psalm-mutation-free |
||
| 1778 | */ |
||
| 1779 | 4 | public static function createFromObject(\Traversable $object): self |
|
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Create an new instance filled with values from an object. |
||
| 1802 | * |
||
| 1803 | * @param object $object |
||
| 1804 | * |
||
| 1805 | * @return static |
||
| 1806 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1807 | * |
||
| 1808 | * @psalm-mutation-free |
||
| 1809 | */ |
||
| 1810 | 5 | public static function createFromObjectVars($object): self |
|
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Create an new Arrayy object via string. |
||
| 1817 | * |
||
| 1818 | * @param string $str <p>The input string.</p> |
||
| 1819 | * @param string|null $delimiter <p>The boundary string.</p> |
||
| 1820 | * @param string|null $regEx <p>Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be |
||
| 1821 | * used.</p> |
||
| 1822 | * |
||
| 1823 | * @return static |
||
| 1824 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1825 | * |
||
| 1826 | * @psalm-mutation-free |
||
| 1827 | */ |
||
| 1828 | 10 | public static function createFromString(string $str, string $delimiter = null, string $regEx = null): self |
|
| 1829 | { |
||
| 1830 | 10 | if ($regEx) { |
|
| 1831 | 1 | \preg_match_all($regEx, $str, $array); |
|
| 1832 | |||
| 1833 | 1 | if (!empty($array)) { |
|
| 1834 | 1 | $array = $array[0]; |
|
| 1835 | } |
||
| 1836 | } else { |
||
| 1837 | /** @noinspection NestedPositiveIfStatementsInspection */ |
||
| 1838 | 9 | if ($delimiter !== null) { |
|
| 1839 | 7 | $array = \explode($delimiter, $str); |
|
| 1840 | } else { |
||
| 1841 | 2 | $array = [$str]; |
|
| 1842 | } |
||
| 1843 | } |
||
| 1844 | |||
| 1845 | // trim all string in the array |
||
| 1846 | /** |
||
| 1847 | * @psalm-suppress MissingClosureParamType |
||
| 1848 | */ |
||
| 1849 | 10 | \array_walk( |
|
| 1850 | 10 | $array, |
|
| 1851 | 10 | static function (&$val) { |
|
| 1852 | 10 | if ((string) $val === $val) { |
|
| 1853 | 10 | $val = \trim($val); |
|
| 1854 | } |
||
| 1855 | 10 | } |
|
| 1856 | ); |
||
| 1857 | |||
| 1858 | 10 | return static::create($array); |
|
| 1859 | } |
||
| 1860 | |||
| 1861 | /** |
||
| 1862 | * Create an new instance filled with a copy of values from a "Traversable"-object. |
||
| 1863 | * |
||
| 1864 | * @param \Traversable $traversable |
||
| 1865 | * |
||
| 1866 | * @return static |
||
| 1867 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1868 | * |
||
| 1869 | * @psalm-param \Traversable<array-key,mixed> $traversable |
||
| 1870 | * |
||
| 1871 | * @psalm-mutation-free |
||
| 1872 | */ |
||
| 1873 | 1 | public static function createFromTraversableImmutable(\Traversable $traversable): self |
|
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Create an new instance containing a range of elements. |
||
| 1880 | * |
||
| 1881 | * @param float|int|string $low <p>First value of the sequence.</p> |
||
| 1882 | * @param float|int|string $high <p>The sequence is ended upon reaching the end value.</p> |
||
| 1883 | * @param float|int $step <p>Used as the increment between elements in the sequence.</p> |
||
| 1884 | * |
||
| 1885 | * @return static |
||
| 1886 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1887 | * |
||
| 1888 | * @psalm-mutation-free |
||
| 1889 | */ |
||
| 1890 | 2 | public static function createWithRange($low, $high, $step = 1): self |
|
| 1894 | |||
| 1895 | /** |
||
| 1896 | * Gets the element of the array at the current internal iterator position. |
||
| 1897 | * |
||
| 1898 | * @return false|mixed |
||
| 1899 | */ |
||
| 1900 | public function current() |
||
| 1904 | |||
| 1905 | /** |
||
| 1906 | * Custom sort by index via "uksort". |
||
| 1907 | * |
||
| 1908 | * EXAMPLE: <code> |
||
| 1909 | * $callable = function ($a, $b) { |
||
| 1910 | * if ($a == $b) { |
||
| 1911 | * return 0; |
||
| 1912 | * } |
||
| 1913 | * return ($a > $b) ? 1 : -1; |
||
| 1914 | * }; |
||
| 1915 | * $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]); |
||
| 1916 | * $resultArrayy = $arrayy->customSortKeys($callable); // Arrayy['one' => 1, 'three' => 3, 'two' => 2] |
||
| 1917 | * </code> |
||
| 1918 | * |
||
| 1919 | * @see http://php.net/manual/en/function.uksort.php |
||
| 1920 | * |
||
| 1921 | * @param callable $function |
||
| 1922 | * |
||
| 1923 | * @throws \InvalidArgumentException |
||
| 1924 | * |
||
| 1925 | * @return $this |
||
| 1926 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1927 | * |
||
| 1928 | * @psalm-return static<TKey,T> |
||
| 1929 | */ |
||
| 1930 | 5 | public function customSortKeys(callable $function): self |
|
| 1938 | |||
| 1939 | /** |
||
| 1940 | * Custom sort by index via "uksort". |
||
| 1941 | * |
||
| 1942 | * @see http://php.net/manual/en/function.uksort.php |
||
| 1943 | * |
||
| 1944 | * @param callable $function |
||
| 1945 | * |
||
| 1946 | * @throws \InvalidArgumentException |
||
| 1947 | * |
||
| 1948 | * @return $this |
||
| 1949 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 1950 | * |
||
| 1951 | * @psalm-return static<TKey,T> |
||
| 1952 | * @psalm-mutation-free |
||
| 1953 | */ |
||
| 1954 | 1 | public function customSortKeysImmutable(callable $function): self |
|
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Custom sort by value via "usort". |
||
| 1970 | * |
||
| 1971 | * EXAMPLE: <code> |
||
| 1972 | * $callable = function ($a, $b) { |
||
| 1973 | * if ($a == $b) { |
||
| 1974 | * return 0; |
||
| 1975 | * } |
||
| 1976 | * return ($a > $b) ? 1 : -1; |
||
| 1977 | * }; |
||
| 1978 | * $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]); |
||
| 1979 | * $resultArrayy = $arrayy->customSortValues($callable); // Arrayy['one' => 1, 'two' => 2, 'three' => 3] |
||
| 1980 | * </code> |
||
| 1981 | * |
||
| 1982 | * @see http://php.net/manual/en/function.usort.php |
||
| 1983 | * |
||
| 1984 | * @param callable $function |
||
| 1985 | * |
||
| 1986 | * @throws \InvalidArgumentException |
||
| 1987 | * |
||
| 1988 | * @return $this |
||
| 1989 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1990 | * |
||
| 1991 | * @psalm-return static<TKey,T> |
||
| 1992 | */ |
||
| 1993 | 10 | View Code Duplication | public function customSortValues($function): self |
| 2005 | |||
| 2006 | /** |
||
| 2007 | * Custom sort by value via "usort". |
||
| 2008 | * |
||
| 2009 | * @see http://php.net/manual/en/function.usort.php |
||
| 2010 | * |
||
| 2011 | * @param callable $function |
||
| 2012 | * |
||
| 2013 | * @throws \InvalidArgumentException |
||
| 2014 | * |
||
| 2015 | * @return $this |
||
| 2016 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 2017 | * |
||
| 2018 | * @psalm-return static<TKey,T> |
||
| 2019 | * @psalm-mutation-free |
||
| 2020 | */ |
||
| 2021 | 4 | public function customSortValuesImmutable($function): self |
|
| 2032 | |||
| 2033 | /** |
||
| 2034 | * Delete the given key or keys. |
||
| 2035 | * |
||
| 2036 | * @param int|int[]|string|string[] $keyOrKeys |
||
| 2037 | * |
||
| 2038 | * @return void |
||
| 2039 | */ |
||
| 2040 | 9 | public function delete($keyOrKeys) |
|
| 2048 | |||
| 2049 | /** |
||
| 2050 | * Return values that are only in the current array. |
||
| 2051 | * |
||
| 2052 | * EXAMPLE: <code> |
||
| 2053 | * a([1 => 1, 2 => 2])->diff([1 => 1]); // Arrayy[2 => 2] |
||
| 2054 | * </code> |
||
| 2055 | * |
||
| 2056 | * @param array ...$array |
||
| 2057 | * |
||
| 2058 | * @return static |
||
| 2059 | * <p>(Immutable)</p> |
||
| 2060 | * |
||
| 2061 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$array |
||
| 2062 | * @psalm-return static<TKey,T> |
||
| 2063 | * @psalm-mutation-free |
||
| 2064 | */ |
||
| 2065 | 13 | public function diff(...$array): self |
|
| 2073 | |||
| 2074 | /** |
||
| 2075 | * Return values that are only in the current array. |
||
| 2076 | * |
||
| 2077 | * @param array ...$array |
||
| 2078 | * |
||
| 2079 | * @return static |
||
| 2080 | * <p>(Immutable)</p> |
||
| 2081 | * |
||
| 2082 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$array |
||
| 2083 | * @psalm-return static<TKey,T> |
||
| 2084 | * @psalm-mutation-free |
||
| 2085 | */ |
||
| 2086 | 8 | public function diffKey(...$array): self |
|
| 2094 | |||
| 2095 | /** |
||
| 2096 | * Return values and Keys that are only in the current array. |
||
| 2097 | * |
||
| 2098 | * @param array $array |
||
| 2099 | * |
||
| 2100 | * @return static |
||
| 2101 | * <p>(Immutable)</p> |
||
| 2102 | * |
||
| 2103 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2104 | * @psalm-return static<TKey,T> |
||
| 2105 | * @psalm-mutation-free |
||
| 2106 | */ |
||
| 2107 | 8 | public function diffKeyAndValue(array $array = []): self |
|
| 2115 | |||
| 2116 | /** |
||
| 2117 | * Return values that are only in the current multi-dimensional array. |
||
| 2118 | * |
||
| 2119 | * EXAMPLE: <code> |
||
| 2120 | * a([1 => [1 => 1], 2 => [2 => 2]])->diffRecursive([1 => [1 => 1]]); // Arrayy[2 => [2 => 2]] |
||
| 2121 | * </code> |
||
| 2122 | * |
||
| 2123 | * @param array $array |
||
| 2124 | * @param array|\Generator|null $helperVariableForRecursion <p>(only for internal usage)</p> |
||
| 2125 | * |
||
| 2126 | * @return static |
||
| 2127 | * <p>(Immutable)</p> |
||
| 2128 | * |
||
| 2129 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2130 | * @psalm-param null|array<TKey,T>|\Generator<TKey,T> $helperVariableForRecursion |
||
| 2131 | * @psalm-return static<TKey,T> |
||
| 2132 | * @psalm-mutation-free |
||
| 2133 | */ |
||
| 2134 | 1 | public function diffRecursive(array $array = [], $helperVariableForRecursion = null): self |
|
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Return values that are only in the new $array. |
||
| 2172 | * |
||
| 2173 | * EXAMPLE: <code> |
||
| 2174 | * a([1 => 1])->diffReverse([1 => 1, 2 => 2]); // Arrayy[2 => 2] |
||
| 2175 | * </code> |
||
| 2176 | * |
||
| 2177 | * @param array $array |
||
| 2178 | * |
||
| 2179 | * @return static |
||
| 2180 | * <p>(Immutable)</p> |
||
| 2181 | * |
||
| 2182 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2183 | * @psalm-return static<TKey,T> |
||
| 2184 | * @psalm-mutation-free |
||
| 2185 | */ |
||
| 2186 | 8 | public function diffReverse(array $array = []): self |
|
| 2194 | |||
| 2195 | /** |
||
| 2196 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 2197 | * |
||
| 2198 | * EXAMPLE: <code> |
||
| 2199 | * a(['a' => 1, 'b' => ''])->divide(); // Arrayy[Arrayy['a', 'b'], Arrayy[1, '']] |
||
| 2200 | * </code> |
||
| 2201 | * |
||
| 2202 | * @return static |
||
| 2203 | * <p>(Immutable)</p> |
||
| 2204 | * |
||
| 2205 | * @psalm-return static<TKey,T> |
||
| 2206 | * @psalm-mutation-free |
||
| 2207 | */ |
||
| 2208 | 1 | public function divide(): self |
|
| 2219 | |||
| 2220 | /** |
||
| 2221 | * Iterate over the current array and modify the array's value. |
||
| 2222 | * |
||
| 2223 | * EXAMPLE: <code> |
||
| 2224 | * $result = A::create(); |
||
| 2225 | * $closure = function ($value) { |
||
| 2226 | * return ':' . $value . ':'; |
||
| 2227 | * }; |
||
| 2228 | * a(['foo', 'bar' => 'bis'])->each($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 2229 | * </code> |
||
| 2230 | * |
||
| 2231 | * @param \Closure $closure |
||
| 2232 | * |
||
| 2233 | * @return static |
||
| 2234 | * <p>(Immutable)</p> |
||
| 2235 | * |
||
| 2236 | * @psalm-return static<TKey,T> |
||
| 2237 | * @psalm-mutation-free |
||
| 2238 | */ |
||
| 2239 | 5 | View Code Duplication | public function each(\Closure $closure): self |
| 2254 | |||
| 2255 | /** |
||
| 2256 | * Sets the internal iterator to the last element in the array and returns this element. |
||
| 2257 | * |
||
| 2258 | * @return mixed |
||
| 2259 | */ |
||
| 2260 | public function end() |
||
| 2264 | |||
| 2265 | /** |
||
| 2266 | * Check if a value is in the current array using a closure. |
||
| 2267 | * |
||
| 2268 | * EXAMPLE: <code> |
||
| 2269 | * $callable = function ($value, $key) { |
||
| 2270 | * return 2 === $key and 'two' === $value; |
||
| 2271 | * }; |
||
| 2272 | * a(['foo', 2 => 'two'])->exists($callable); // true |
||
| 2273 | * </code> |
||
| 2274 | * |
||
| 2275 | * @param \Closure $closure |
||
| 2276 | * |
||
| 2277 | * @return bool |
||
| 2278 | * <p>Returns true if the given value is found, false otherwise.</p> |
||
| 2279 | */ |
||
| 2280 | 4 | public function exists(\Closure $closure): bool |
|
| 2295 | |||
| 2296 | /** |
||
| 2297 | * Fill the array until "$num" with "$default" values. |
||
| 2298 | * |
||
| 2299 | * EXAMPLE: <code> |
||
| 2300 | * a(['bar'])->fillWithDefaults(3, 'foo'); // Arrayy['bar', 'foo', 'foo'] |
||
| 2301 | * </code> |
||
| 2302 | * |
||
| 2303 | * @param int $num |
||
| 2304 | * @param mixed $default |
||
| 2305 | * |
||
| 2306 | * @return static |
||
| 2307 | * <p>(Immutable)</p> |
||
| 2308 | * |
||
| 2309 | * @psalm-return static<TKey,T> |
||
| 2310 | * @psalm-mutation-free |
||
| 2311 | */ |
||
| 2312 | 8 | public function fillWithDefaults(int $num, $default = null): self |
|
| 2335 | |||
| 2336 | /** |
||
| 2337 | * Find all items in an array that pass the truth test. |
||
| 2338 | * |
||
| 2339 | * EXAMPLE: <code> |
||
| 2340 | * $closure = function ($value) { |
||
| 2341 | * return $value % 2 !== 0; |
||
| 2342 | * } |
||
| 2343 | * a([1, 2, 3, 4])->filter($closure); // Arrayy[0 => 1, 2 => 3] |
||
| 2344 | * </code> |
||
| 2345 | * |
||
| 2346 | * @param \Closure|null $closure [optional] <p> |
||
| 2347 | * The callback function to use |
||
| 2348 | * </p> |
||
| 2349 | * <p> |
||
| 2350 | * If no callback is supplied, all entries of |
||
| 2351 | * input equal to false (see |
||
| 2352 | * converting to |
||
| 2353 | * boolean) will be removed. |
||
| 2354 | * </p> |
||
| 2355 | * @param int $flag [optional] <p> |
||
| 2356 | * Flag determining what arguments are sent to <i>callback</i>: |
||
| 2357 | * </p> |
||
| 2358 | * <ul> |
||
| 2359 | * <li> |
||
| 2360 | * <b>ARRAY_FILTER_USE_KEY</b> (1) - pass key as the only argument |
||
| 2361 | * to <i>callback</i> instead of the value |
||
| 2362 | * </li> |
||
| 2363 | * <li> |
||
| 2364 | * <b>ARRAY_FILTER_USE_BOTH</b> (2) - pass both value and key as |
||
| 2365 | * arguments to <i>callback</i> instead of the value |
||
| 2366 | * </li> |
||
| 2367 | * </ul> |
||
| 2368 | * |
||
| 2369 | * @return static |
||
| 2370 | * <p>(Immutable)</p> |
||
| 2371 | * |
||
| 2372 | * @psalm-param null|\Closure(T=,TKey=):bool|\Closure(T=):bool||\Closure(TKey=):bool $closure |
||
| 2373 | * @psalm-return static<TKey,T> |
||
| 2374 | * @psalm-mutation-free |
||
| 2375 | */ |
||
| 2376 | 12 | public function filter($closure = null, int $flag = \ARRAY_FILTER_USE_BOTH) |
|
| 2377 | { |
||
| 2378 | 12 | if (!$closure) { |
|
| 2379 | 1 | return $this->clean(); |
|
| 2380 | } |
||
| 2381 | |||
| 2382 | 12 | if ($flag === \ARRAY_FILTER_USE_KEY) { |
|
| 2383 | 1 | $generator = function () use ($closure) { |
|
| 2384 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 2385 | 1 | if ($closure($key) === true) { |
|
| 2386 | 1 | yield $key => $value; |
|
| 2387 | } |
||
| 2388 | } |
||
| 2389 | 1 | }; |
|
| 2390 | 12 | } elseif ($flag === \ARRAY_FILTER_USE_BOTH) { |
|
| 2391 | 12 | $generator = function () use ($closure) { |
|
| 2392 | 11 | foreach ($this->getGenerator() as $key => $value) { |
|
| 2393 | 10 | if ($closure($value, $key) === true) { |
|
| 2394 | 10 | yield $key => $value; |
|
| 2395 | } |
||
| 2396 | } |
||
| 2397 | 12 | }; |
|
| 2398 | } else { |
||
| 2399 | 1 | View Code Duplication | $generator = function () use ($closure) { |
| 2400 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 2401 | 1 | if ($closure($value) === true) { |
|
| 2402 | 1 | yield $key => $value; |
|
| 2403 | } |
||
| 2404 | } |
||
| 2405 | 1 | }; |
|
| 2406 | } |
||
| 2407 | |||
| 2408 | 12 | return static::create( |
|
| 2409 | 12 | $generator, |
|
| 2410 | 12 | $this->iteratorClass, |
|
| 2411 | 12 | false |
|
| 2412 | ); |
||
| 2413 | } |
||
| 2414 | |||
| 2415 | /** |
||
| 2416 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular |
||
| 2417 | * property within that. |
||
| 2418 | * |
||
| 2419 | * @param string $property |
||
| 2420 | * @param string|string[] $value |
||
| 2421 | * @param string $comparisonOp |
||
| 2422 | * <p> |
||
| 2423 | * 'eq' (equals),<br /> |
||
| 2424 | * 'gt' (greater),<br /> |
||
| 2425 | * 'gte' || 'ge' (greater or equals),<br /> |
||
| 2426 | * 'lt' (less),<br /> |
||
| 2427 | * 'lte' || 'le' (less or equals),<br /> |
||
| 2428 | * 'ne' (not equals),<br /> |
||
| 2429 | * 'contains',<br /> |
||
| 2430 | * 'notContains',<br /> |
||
| 2431 | * 'newer' (via strtotime),<br /> |
||
| 2432 | * 'older' (via strtotime),<br /> |
||
| 2433 | * </p> |
||
| 2434 | * |
||
| 2435 | * @return static |
||
| 2436 | * <p>(Immutable)</p> |
||
| 2437 | * |
||
| 2438 | * @psalm-return static<TKey,T> |
||
| 2439 | * @psalm-mutation-free |
||
| 2440 | * |
||
| 2441 | * @psalm-suppress MissingClosureReturnType |
||
| 2442 | * @psalm-suppress MissingClosureParamType |
||
| 2443 | */ |
||
| 2444 | 1 | public function filterBy( |
|
| 2445 | string $property, |
||
| 2446 | $value, |
||
| 2447 | string $comparisonOp = null |
||
| 2448 | ): self { |
||
| 2449 | 1 | if (!$comparisonOp) { |
|
| 2450 | 1 | $comparisonOp = \is_array($value) ? 'contains' : 'eq'; |
|
| 2451 | } |
||
| 2452 | |||
| 2453 | $ops = [ |
||
| 2454 | 1 | 'eq' => static function ($item, $prop, $value): bool { |
|
| 2455 | 1 | return $item[$prop] === $value; |
|
| 2456 | 1 | }, |
|
| 2457 | 1 | 'gt' => static function ($item, $prop, $value): bool { |
|
| 2458 | return $item[$prop] > $value; |
||
| 2459 | 1 | }, |
|
| 2460 | 1 | 'ge' => static function ($item, $prop, $value): bool { |
|
| 2461 | return $item[$prop] >= $value; |
||
| 2462 | 1 | }, |
|
| 2463 | 1 | 'gte' => static function ($item, $prop, $value): bool { |
|
| 2464 | return $item[$prop] >= $value; |
||
| 2465 | 1 | }, |
|
| 2466 | 1 | 'lt' => static function ($item, $prop, $value): bool { |
|
| 2467 | 1 | return $item[$prop] < $value; |
|
| 2468 | 1 | }, |
|
| 2469 | 1 | 'le' => static function ($item, $prop, $value): bool { |
|
| 2470 | return $item[$prop] <= $value; |
||
| 2471 | 1 | }, |
|
| 2472 | 1 | 'lte' => static function ($item, $prop, $value): bool { |
|
| 2473 | return $item[$prop] <= $value; |
||
| 2474 | 1 | }, |
|
| 2475 | 1 | 'ne' => static function ($item, $prop, $value): bool { |
|
| 2476 | return $item[$prop] !== $value; |
||
| 2477 | 1 | }, |
|
| 2478 | 1 | 'contains' => static function ($item, $prop, $value): bool { |
|
| 2479 | 1 | return \in_array($item[$prop], (array) $value, true); |
|
| 2480 | 1 | }, |
|
| 2481 | 1 | 'notContains' => static function ($item, $prop, $value): bool { |
|
| 2482 | return !\in_array($item[$prop], (array) $value, true); |
||
| 2483 | 1 | }, |
|
| 2484 | 1 | 'newer' => static function ($item, $prop, $value): bool { |
|
| 2485 | return \strtotime($item[$prop]) > \strtotime($value); |
||
| 2486 | 1 | }, |
|
| 2487 | 1 | 'older' => static function ($item, $prop, $value): bool { |
|
| 2488 | return \strtotime($item[$prop]) < \strtotime($value); |
||
| 2489 | 1 | }, |
|
| 2490 | ]; |
||
| 2491 | |||
| 2492 | 1 | $result = \array_values( |
|
| 2493 | 1 | \array_filter( |
|
| 2494 | 1 | $this->toArray(false, true), |
|
| 2495 | 1 | static function ($item) use ( |
|
| 2496 | 1 | $property, |
|
| 2497 | 1 | $value, |
|
| 2498 | 1 | $ops, |
|
| 2499 | 1 | $comparisonOp |
|
| 2500 | ) { |
||
| 2501 | 1 | $item = (array) $item; |
|
| 2502 | 1 | $itemArrayy = static::create($item); |
|
| 2503 | 1 | $item[$property] = $itemArrayy->get($property, []); |
|
| 2504 | |||
| 2505 | 1 | return $ops[$comparisonOp]($item, $property, $value); |
|
| 2506 | 1 | } |
|
| 2507 | ) |
||
| 2508 | ); |
||
| 2509 | |||
| 2510 | 1 | return static::create( |
|
| 2511 | 1 | $result, |
|
| 2512 | 1 | $this->iteratorClass, |
|
| 2513 | 1 | false |
|
| 2514 | ); |
||
| 2515 | } |
||
| 2516 | |||
| 2517 | /** |
||
| 2518 | * Find the first item in an array that passes the truth test, otherwise return false. |
||
| 2519 | * |
||
| 2520 | * EXAMPLE: <code> |
||
| 2521 | * $search = 'foo'; |
||
| 2522 | * $closure = function ($value, $key) use ($search) { |
||
| 2523 | * return $value === $search; |
||
| 2524 | * }; |
||
| 2525 | * a(['foo', 'bar', 'lall'])->find($closure); // 'foo' |
||
| 2526 | * </code> |
||
| 2527 | * |
||
| 2528 | * @param \Closure $closure |
||
| 2529 | * |
||
| 2530 | * @return false|mixed |
||
| 2531 | * <p>Return false if we did not find the value.</p> |
||
| 2532 | */ |
||
| 2533 | 8 | View Code Duplication | public function find(\Closure $closure) |
| 2543 | |||
| 2544 | /** |
||
| 2545 | * find by ... |
||
| 2546 | * |
||
| 2547 | * EXAMPLE: <code> |
||
| 2548 | * $array = [ |
||
| 2549 | * 0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01'], |
||
| 2550 | * 1 => ['id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468, 'when' => '2014-07-15'], |
||
| 2551 | * ]; |
||
| 2552 | * a($array)->filterBy('name', 'foo'); // Arrayy[0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01']] |
||
| 2553 | * </code> |
||
| 2554 | * |
||
| 2555 | * @param string $property |
||
| 2556 | * @param string|string[] $value |
||
| 2557 | * @param string $comparisonOp |
||
| 2558 | * |
||
| 2559 | * @return static |
||
| 2560 | * <p>(Immutable)</p> |
||
| 2561 | * |
||
| 2562 | * @psalm-return static<TKey,T> |
||
| 2563 | * @psalm-mutation-free |
||
| 2564 | */ |
||
| 2565 | 1 | public function findBy(string $property, $value, string $comparisonOp = 'eq'): self |
|
| 2569 | |||
| 2570 | /** |
||
| 2571 | * Get the first value from the current array. |
||
| 2572 | * |
||
| 2573 | * EXAMPLE: <code> |
||
| 2574 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->first(); // 'foo' |
||
| 2575 | * </code> |
||
| 2576 | * |
||
| 2577 | * @return mixed |
||
| 2578 | * <p>Return null if there wasn't a element.</p> |
||
| 2579 | */ |
||
| 2580 | 22 | public function first() |
|
| 2589 | |||
| 2590 | /** |
||
| 2591 | * Get the first key from the current array. |
||
| 2592 | * |
||
| 2593 | * @return mixed |
||
| 2594 | * <p>Return null if there wasn't a element.</p> |
||
| 2595 | * @psalm-mutation-free |
||
| 2596 | */ |
||
| 2597 | 29 | public function firstKey() |
|
| 2603 | |||
| 2604 | /** |
||
| 2605 | * Get the first value(s) from the current array. |
||
| 2606 | * And will return an empty array if there was no first entry. |
||
| 2607 | * |
||
| 2608 | * EXAMPLE: <code> |
||
| 2609 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsImmutable(2); // Arrayy[0 => 'foo', 1 => 'bar'] |
||
| 2610 | * </code> |
||
| 2611 | * |
||
| 2612 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2613 | * |
||
| 2614 | * @return static |
||
| 2615 | * <p>(Immutable)</p> |
||
| 2616 | * |
||
| 2617 | * @psalm-return static<TKey,T> |
||
| 2618 | * @psalm-mutation-free |
||
| 2619 | */ |
||
| 2620 | 37 | View Code Duplication | public function firstsImmutable(int $number = null): self |
| 2636 | |||
| 2637 | /** |
||
| 2638 | * Get the first value(s) from the current array. |
||
| 2639 | * And will return an empty array if there was no first entry. |
||
| 2640 | * |
||
| 2641 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2642 | * |
||
| 2643 | * @return static |
||
| 2644 | * <p>(Immutable)</p> |
||
| 2645 | * |
||
| 2646 | * @psalm-return static<TKey,T> |
||
| 2647 | * @psalm-mutation-free |
||
| 2648 | */ |
||
| 2649 | 3 | View Code Duplication | public function firstsKeys(int $number = null): self |
| 2665 | |||
| 2666 | /** |
||
| 2667 | * Get and remove the first value(s) from the current array. |
||
| 2668 | * And will return an empty array if there was no first entry. |
||
| 2669 | * |
||
| 2670 | * EXAMPLE: <code> |
||
| 2671 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsMutable(); // 'foo' |
||
| 2672 | * </code> |
||
| 2673 | * |
||
| 2674 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2675 | * |
||
| 2676 | * @return $this |
||
| 2677 | * <p>(Mutable)</p> |
||
| 2678 | * |
||
| 2679 | * @psalm-return static<TKey,T> |
||
| 2680 | */ |
||
| 2681 | 34 | public function firstsMutable(int $number = null): self |
|
| 2693 | |||
| 2694 | /** |
||
| 2695 | * Exchanges all keys with their associated values in an array. |
||
| 2696 | * |
||
| 2697 | * EXAMPLE: <code> |
||
| 2698 | * a([0 => 'foo', 1 => 'bar'])->flip(); // Arrayy['foo' => 0, 'bar' => 1] |
||
| 2699 | * </code> |
||
| 2700 | * |
||
| 2701 | * @return static |
||
| 2702 | * <p>(Immutable)</p> |
||
| 2703 | * |
||
| 2704 | * @psalm-return static<array-key,TKey> |
||
| 2705 | * @psalm-mutation-free |
||
| 2706 | */ |
||
| 2707 | public function flip(): self |
||
| 2721 | |||
| 2722 | /** |
||
| 2723 | * Get a value from an array (optional using dot-notation). |
||
| 2724 | * |
||
| 2725 | * EXAMPLE: <code> |
||
| 2726 | * $arrayy = a(['user' => ['lastname' => 'Moelleken']]); |
||
| 2727 | * $arrayy->get('user.lastname'); // 'Moelleken' |
||
| 2728 | * // --- |
||
| 2729 | * $arrayy = new A(); |
||
| 2730 | * $arrayy['user'] = ['lastname' => 'Moelleken']; |
||
| 2731 | * $arrayy['user.firstname'] = 'Lars'; |
||
| 2732 | * $arrayy['user']['lastname']; // Moelleken |
||
| 2733 | * $arrayy['user.lastname']; // Moelleken |
||
| 2734 | * $arrayy['user.firstname']; // Lars |
||
| 2735 | * </code> |
||
| 2736 | * |
||
| 2737 | * @param mixed $key <p>The key to look for.</p> |
||
| 2738 | * @param mixed $fallback <p>Value to fallback to.</p> |
||
| 2739 | * @param array $array <p>The array to get from, if it's set to "null" we use the current array from the |
||
| 2740 | * class.</p> |
||
| 2741 | * @param bool $useByReference |
||
| 2742 | * |
||
| 2743 | * @return mixed|static |
||
| 2744 | * |
||
| 2745 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2746 | * @psalm-mutation-free |
||
| 2747 | */ |
||
| 2748 | 243 | public function get( |
|
| 2912 | |||
| 2913 | /** |
||
| 2914 | * alias: for "Arrayy->toArray()" |
||
| 2915 | * |
||
| 2916 | * @return array |
||
| 2917 | * |
||
| 2918 | * @see Arrayy::getArray() |
||
| 2919 | * |
||
| 2920 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 2921 | */ |
||
| 2922 | 15 | public function getAll(): array |
|
| 2926 | |||
| 2927 | /** |
||
| 2928 | * Get the current array from the "Arrayy"-object. |
||
| 2929 | * |
||
| 2930 | * alias for "toArray()" |
||
| 2931 | * |
||
| 2932 | * @param bool $convertAllArrayyElements <p> |
||
| 2933 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 2934 | * </p> |
||
| 2935 | * @param bool $preserveKeys <p> |
||
| 2936 | * e.g.: A generator maybe return the same key more then once, |
||
| 2937 | * so maybe you will ignore the keys. |
||
| 2938 | * </p> |
||
| 2939 | * |
||
| 2940 | * @return array |
||
| 2941 | * |
||
| 2942 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 2943 | * @psalm-mutation-free |
||
| 2944 | * |
||
| 2945 | * @see Arrayy::toArray() |
||
| 2946 | */ |
||
| 2947 | 501 | public function getArray( |
|
| 2956 | |||
| 2957 | /** |
||
| 2958 | * @param string $json |
||
| 2959 | * |
||
| 2960 | * @return $this |
||
| 2961 | */ |
||
| 2962 | 3 | public static function createFromJsonMapper(string $json) |
|
| 2963 | { |
||
| 2964 | // init |
||
| 2965 | 3 | $class = static::create(); |
|
| 2966 | |||
| 2967 | 3 | $jsonObject = \json_decode($json, false); |
|
| 2968 | |||
| 2969 | 3 | $mapper = new \Arrayy\Mapper\Json(); |
|
| 2970 | View Code Duplication | $mapper->undefinedPropertyHandler = static function ($object, $key, $jsonValue) use ($class) { |
|
| 2971 | if ($class->checkPropertiesMismatchInConstructor) { |
||
| 2972 | throw new \TypeError('Property mismatch - input: ' . \print_r(['key' => $key, 'jsonValue' => $jsonValue], true) . ' for object: ' . \get_class($object)); |
||
| 2973 | } |
||
| 2974 | }; |
||
| 2975 | |||
| 2976 | 3 | return $mapper->map($jsonObject, $class); |
|
| 2977 | } |
||
| 2978 | |||
| 2979 | /** |
||
| 2980 | * @return array<int|string,TypeCheckInterface>|mixed|TypeCheckArray<int|string,TypeCheckInterface>|TypeInterface |
||
| 2981 | * |
||
| 2982 | * @internal |
||
| 2983 | */ |
||
| 2984 | 6 | public function getPhpDocPropertiesFromClass() |
|
| 2992 | |||
| 2993 | /** |
||
| 2994 | * Get the current array from the "Arrayy"-object as list. |
||
| 2995 | * |
||
| 2996 | * alias for "toList()" |
||
| 2997 | * |
||
| 2998 | * @param bool $convertAllArrayyElements <p> |
||
| 2999 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 3000 | * </p> |
||
| 3001 | * |
||
| 3002 | * @return array |
||
| 3003 | * |
||
| 3004 | * @psalm-return array<int,mixed>|array<int,T> |
||
| 3005 | * @psalm-mutation-free |
||
| 3006 | * |
||
| 3007 | * @see Arrayy::toList() |
||
| 3008 | */ |
||
| 3009 | 1 | public function getList(bool $convertAllArrayyElements = false): array |
|
| 3013 | |||
| 3014 | /** |
||
| 3015 | * Returns the values from a single column of the input array, identified by |
||
| 3016 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 3017 | * |
||
| 3018 | * EXAMPLE: <code> |
||
| 3019 | * a([['foo' => 'bar', 'id' => 1], ['foo => 'lall', 'id' => 2]])->getColumn('foo', 'id'); // Arrayy[1 => 'bar', 2 => 'lall'] |
||
| 3020 | * </code> |
||
| 3021 | * |
||
| 3022 | * INFO: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 3023 | * array by the values from the $indexKey column in the input array. |
||
| 3024 | * |
||
| 3025 | * @param int|string|null $columnKey |
||
| 3026 | * @param int|string|null $indexKey |
||
| 3027 | * |
||
| 3028 | * @return static |
||
| 3029 | * <p>(Immutable)</p> |
||
| 3030 | * |
||
| 3031 | * @psalm-return static<TKey,T> |
||
| 3032 | * @psalm-mutation-free |
||
| 3033 | */ |
||
| 3034 | 1 | public function getColumn($columnKey = null, $indexKey = null): self |
|
| 3035 | { |
||
| 3036 | 1 | if ($columnKey === null && $indexKey === null) { |
|
| 3037 | 1 | $generator = function () { |
|
| 3038 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 3039 | 1 | yield $value; |
|
| 3040 | } |
||
| 3041 | 1 | }; |
|
| 3042 | } else { |
||
| 3043 | 1 | $generator = function () use ($columnKey, $indexKey) { |
|
| 3044 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 3045 | // reset |
||
| 3046 | 1 | $newKey = null; |
|
| 3047 | 1 | $newValue = null; |
|
| 3048 | 1 | $newValueFound = false; |
|
| 3049 | |||
| 3050 | 1 | if ($indexKey !== null) { |
|
| 3051 | 1 | foreach ($value as $keyInner => $valueInner) { |
|
| 3052 | 1 | if ($indexKey === $keyInner) { |
|
| 3053 | 1 | $newKey = $valueInner; |
|
| 3054 | } |
||
| 3055 | |||
| 3056 | 1 | if ($columnKey === $keyInner) { |
|
| 3057 | 1 | $newValue = $valueInner; |
|
| 3058 | 1 | $newValueFound = true; |
|
| 3059 | } |
||
| 3060 | } |
||
| 3061 | } else { |
||
| 3062 | 1 | foreach ($value as $keyInner => $valueInner) { |
|
| 3063 | 1 | if ($columnKey === $keyInner) { |
|
| 3064 | 1 | $newValue = $valueInner; |
|
| 3065 | 1 | $newValueFound = true; |
|
| 3066 | } |
||
| 3067 | } |
||
| 3068 | } |
||
| 3069 | |||
| 3070 | 1 | if ($newValueFound === false) { |
|
| 3071 | 1 | if ($newKey !== null) { |
|
| 3072 | 1 | yield $newKey => $value; |
|
| 3073 | } else { |
||
| 3074 | 1 | yield $value; |
|
| 3075 | } |
||
| 3076 | } else { |
||
| 3077 | /** @noinspection NestedPositiveIfStatementsInspection */ |
||
| 3078 | 1 | if ($newKey !== null) { |
|
| 3079 | 1 | yield $newKey => $newValue; |
|
| 3080 | } else { |
||
| 3081 | 1 | yield $newValue; |
|
| 3082 | } |
||
| 3083 | } |
||
| 3084 | } |
||
| 3085 | 1 | }; |
|
| 3086 | } |
||
| 3087 | |||
| 3088 | 1 | return static::create( |
|
| 3089 | 1 | $generator, |
|
| 3090 | 1 | $this->iteratorClass, |
|
| 3091 | 1 | false |
|
| 3092 | ); |
||
| 3093 | } |
||
| 3094 | |||
| 3095 | /** |
||
| 3096 | * Get the current array from the "Arrayy"-object as generator by reference. |
||
| 3097 | * |
||
| 3098 | * @return \Generator |
||
| 3099 | * |
||
| 3100 | * @psalm-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3101 | */ |
||
| 3102 | 75 | public function &getGeneratorByReference(): \Generator |
|
| 3121 | |||
| 3122 | /** |
||
| 3123 | * Get the current array from the "Arrayy"-object as generator. |
||
| 3124 | * |
||
| 3125 | * @return \Generator |
||
| 3126 | * |
||
| 3127 | * @psalm-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3128 | * @psalm-mutation-free |
||
| 3129 | */ |
||
| 3130 | 1000 | public function getGenerator(): \Generator |
|
| 3140 | |||
| 3141 | /** |
||
| 3142 | * alias: for "Arrayy->keys()" |
||
| 3143 | * |
||
| 3144 | * @return static |
||
| 3145 | * <p>(Immutable)</p> |
||
| 3146 | * |
||
| 3147 | * @see Arrayy::keys() |
||
| 3148 | * |
||
| 3149 | * @psalm-return static<array-key,TKey> |
||
| 3150 | * @psalm-mutation-free |
||
| 3151 | */ |
||
| 3152 | 2 | public function getKeys() |
|
| 3156 | |||
| 3157 | /** |
||
| 3158 | * Get the current array from the "Arrayy"-object as object. |
||
| 3159 | * |
||
| 3160 | * @return \stdClass |
||
| 3161 | */ |
||
| 3162 | 4 | public function getObject(): \stdClass |
|
| 3166 | |||
| 3167 | /** |
||
| 3168 | * alias: for "Arrayy->randomImmutable()" |
||
| 3169 | * |
||
| 3170 | * @return static |
||
| 3171 | * <p>(Immutable)</p> |
||
| 3172 | * |
||
| 3173 | * @see Arrayy::randomImmutable() |
||
| 3174 | * |
||
| 3175 | * @psalm-return static<int|array-key,T> |
||
| 3176 | */ |
||
| 3177 | 4 | public function getRandom(): self |
|
| 3181 | |||
| 3182 | /** |
||
| 3183 | * alias: for "Arrayy->randomKey()" |
||
| 3184 | * |
||
| 3185 | * @return mixed |
||
| 3186 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 3187 | * |
||
| 3188 | * @see Arrayy::randomKey() |
||
| 3189 | */ |
||
| 3190 | 3 | public function getRandomKey() |
|
| 3194 | |||
| 3195 | /** |
||
| 3196 | * alias: for "Arrayy->randomKeys()" |
||
| 3197 | * |
||
| 3198 | * @param int $number |
||
| 3199 | * |
||
| 3200 | * @return static |
||
| 3201 | * <p>(Immutable)</p> |
||
| 3202 | * |
||
| 3203 | * @see Arrayy::randomKeys() |
||
| 3204 | * |
||
| 3205 | * @psalm-return static<TKey,T> |
||
| 3206 | */ |
||
| 3207 | 8 | public function getRandomKeys(int $number): self |
|
| 3211 | |||
| 3212 | /** |
||
| 3213 | * alias: for "Arrayy->randomValue()" |
||
| 3214 | * |
||
| 3215 | * @return mixed |
||
| 3216 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 3217 | * |
||
| 3218 | * @see Arrayy::randomValue() |
||
| 3219 | */ |
||
| 3220 | 3 | public function getRandomValue() |
|
| 3224 | |||
| 3225 | /** |
||
| 3226 | * alias: for "Arrayy->randomValues()" |
||
| 3227 | * |
||
| 3228 | * @param int $number |
||
| 3229 | * |
||
| 3230 | * @return static |
||
| 3231 | * <p>(Immutable)</p> |
||
| 3232 | * |
||
| 3233 | * @see Arrayy::randomValues() |
||
| 3234 | * |
||
| 3235 | * @psalm-return static<TKey,T> |
||
| 3236 | */ |
||
| 3237 | 6 | public function getRandomValues(int $number): self |
|
| 3241 | |||
| 3242 | /** |
||
| 3243 | * Gets all values. |
||
| 3244 | * |
||
| 3245 | * @return static |
||
| 3246 | * <p>The values of all elements in this array, in the order they |
||
| 3247 | * appear in the array.</p> |
||
| 3248 | * |
||
| 3249 | * @psalm-return static<TKey,T> |
||
| 3250 | */ |
||
| 3251 | 4 | public function getValues() |
|
| 3261 | |||
| 3262 | /** |
||
| 3263 | * Gets all values via Generator. |
||
| 3264 | * |
||
| 3265 | * @return \Generator |
||
| 3266 | * <p>The values of all elements in this array, in the order they |
||
| 3267 | * appear in the array as Generator.</p> |
||
| 3268 | * |
||
| 3269 | * @psalm-return \Generator<TKey,T> |
||
| 3270 | */ |
||
| 3271 | 4 | public function getValuesYield(): \Generator |
|
| 3275 | |||
| 3276 | /** |
||
| 3277 | * Group values from a array according to the results of a closure. |
||
| 3278 | * |
||
| 3279 | * @param callable|string $grouper <p>A callable function name.</p> |
||
| 3280 | * @param bool $saveKeys |
||
| 3281 | * |
||
| 3282 | * @return static |
||
| 3283 | * <p>(Immutable)</p> |
||
| 3284 | * |
||
| 3285 | * @psalm-return static<TKey,T> |
||
| 3286 | * @psalm-mutation-free |
||
| 3287 | */ |
||
| 3288 | 4 | public function group($grouper, bool $saveKeys = false): self |
|
| 3329 | |||
| 3330 | /** |
||
| 3331 | * Check if an array has a given key. |
||
| 3332 | * |
||
| 3333 | * @param mixed $key |
||
| 3334 | * |
||
| 3335 | * @return bool |
||
| 3336 | */ |
||
| 3337 | 30 | public function has($key): bool |
|
| 3363 | |||
| 3364 | /** |
||
| 3365 | * Check if an array has a given value. |
||
| 3366 | * |
||
| 3367 | * INFO: If you need to search recursive please use ```contains($value, true)```. |
||
| 3368 | * |
||
| 3369 | * @param mixed $value |
||
| 3370 | * |
||
| 3371 | * @return bool |
||
| 3372 | */ |
||
| 3373 | 1 | public function hasValue($value): bool |
|
| 3377 | |||
| 3378 | /** |
||
| 3379 | * Implodes the values of this array. |
||
| 3380 | * |
||
| 3381 | * EXAMPLE: <code> |
||
| 3382 | * a([0 => -9, 1, 2])->implode('|'); // '-9|1|2' |
||
| 3383 | * </code> |
||
| 3384 | * |
||
| 3385 | * @param string $glue |
||
| 3386 | * @param string $prefix |
||
| 3387 | * |
||
| 3388 | * @return string |
||
| 3389 | * @psalm-mutation-free |
||
| 3390 | */ |
||
| 3391 | 28 | public function implode(string $glue = '', string $prefix = ''): string |
|
| 3395 | |||
| 3396 | /** |
||
| 3397 | * Implodes the keys of this array. |
||
| 3398 | * |
||
| 3399 | * @param string $glue |
||
| 3400 | * |
||
| 3401 | * @return string |
||
| 3402 | * @psalm-mutation-free |
||
| 3403 | */ |
||
| 3404 | 8 | public function implodeKeys(string $glue = ''): string |
|
| 3408 | |||
| 3409 | /** |
||
| 3410 | * Given a list and an iterate-function that returns |
||
| 3411 | * a key for each element in the list (or a property name), |
||
| 3412 | * returns an object with an index of each item. |
||
| 3413 | * |
||
| 3414 | * @param mixed $key |
||
| 3415 | * |
||
| 3416 | * @return static |
||
| 3417 | * <p>(Immutable)</p> |
||
| 3418 | * |
||
| 3419 | * @psalm-return static<TKey,T> |
||
| 3420 | * @psalm-mutation-free |
||
| 3421 | */ |
||
| 3422 | 4 | View Code Duplication | public function indexBy($key): self |
| 3439 | |||
| 3440 | /** |
||
| 3441 | * alias: for "Arrayy->searchIndex()" |
||
| 3442 | * |
||
| 3443 | * @param mixed $value <p>The value to search for.</p> |
||
| 3444 | * |
||
| 3445 | * @return false|mixed |
||
| 3446 | * |
||
| 3447 | * @see Arrayy::searchIndex() |
||
| 3448 | */ |
||
| 3449 | 4 | public function indexOf($value) |
|
| 3453 | |||
| 3454 | /** |
||
| 3455 | * Get everything but the last..$to items. |
||
| 3456 | * |
||
| 3457 | * EXAMPLE: <code> |
||
| 3458 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->initial(2); // Arrayy[0 => 'foo'] |
||
| 3459 | * </code> |
||
| 3460 | * |
||
| 3461 | * @param int $to |
||
| 3462 | * |
||
| 3463 | * @return static |
||
| 3464 | * <p>(Immutable)</p> |
||
| 3465 | * |
||
| 3466 | * @psalm-return static<TKey,T> |
||
| 3467 | * @psalm-mutation-free |
||
| 3468 | */ |
||
| 3469 | 12 | public function initial(int $to = 1): self |
|
| 3473 | |||
| 3474 | /** |
||
| 3475 | * Return an array with all elements found in input array. |
||
| 3476 | * |
||
| 3477 | * EXAMPLE: <code> |
||
| 3478 | * a(['foo', 'bar'])->intersection(['bar', 'baz']); // Arrayy['bar'] |
||
| 3479 | * </code> |
||
| 3480 | * |
||
| 3481 | * @param array $search |
||
| 3482 | * @param bool $keepKeys |
||
| 3483 | * |
||
| 3484 | * @return static |
||
| 3485 | * <p>(Immutable)</p> |
||
| 3486 | * |
||
| 3487 | * @psalm-param array<mixed,mixed>|array<TKey,T> $search |
||
| 3488 | * @psalm-return static<TKey,T> |
||
| 3489 | * @psalm-mutation-free |
||
| 3490 | */ |
||
| 3491 | 4 | public function intersection(array $search, bool $keepKeys = false): self |
|
| 3492 | { |
||
| 3493 | 4 | if ($keepKeys) { |
|
| 3494 | /** |
||
| 3495 | * @psalm-suppress MissingClosureReturnType |
||
| 3496 | * @psalm-suppress MissingClosureParamType |
||
| 3497 | */ |
||
| 3498 | 1 | return static::create( |
|
| 3499 | 1 | \array_uintersect( |
|
| 3500 | 1 | $this->toArray(), |
|
| 3501 | 1 | $search, |
|
| 3502 | 1 | static function ($a, $b) { |
|
| 3503 | 1 | return $a === $b ? 0 : -1; |
|
| 3504 | 1 | } |
|
| 3505 | ), |
||
| 3506 | 1 | $this->iteratorClass, |
|
| 3507 | 1 | false |
|
| 3508 | ); |
||
| 3509 | } |
||
| 3510 | |||
| 3511 | 3 | return static::create( |
|
| 3512 | 3 | \array_values(\array_intersect($this->toArray(), $search)), |
|
| 3513 | 3 | $this->iteratorClass, |
|
| 3514 | 3 | false |
|
| 3515 | ); |
||
| 3516 | } |
||
| 3517 | |||
| 3518 | /** |
||
| 3519 | * Return an array with all elements found in input array. |
||
| 3520 | * |
||
| 3521 | * @param array ...$array |
||
| 3522 | * |
||
| 3523 | * @return static |
||
| 3524 | * <p>(Immutable)</p> |
||
| 3525 | * |
||
| 3526 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$array |
||
| 3527 | * @psalm-return static<TKey,T> |
||
| 3528 | * @psalm-mutation-free |
||
| 3529 | */ |
||
| 3530 | 1 | public function intersectionMulti(...$array): self |
|
| 3538 | |||
| 3539 | /** |
||
| 3540 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 3541 | * |
||
| 3542 | * EXAMPLE: <code> |
||
| 3543 | * a(['foo', 'bar'])->intersects(['föö', 'bär']); // false |
||
| 3544 | * </code> |
||
| 3545 | * |
||
| 3546 | * @param array $search |
||
| 3547 | * |
||
| 3548 | * @return bool |
||
| 3549 | * |
||
| 3550 | * @psalm-param array<mixed,mixed>|array<TKey,T> $search |
||
| 3551 | */ |
||
| 3552 | 1 | public function intersects(array $search): bool |
|
| 3556 | |||
| 3557 | /** |
||
| 3558 | * Invoke a function on all of an array's values. |
||
| 3559 | * |
||
| 3560 | * @param callable $callable |
||
| 3561 | * @param mixed $arguments |
||
| 3562 | * |
||
| 3563 | * @return static |
||
| 3564 | * <p>(Immutable)</p> |
||
| 3565 | * |
||
| 3566 | * @psalm-param callable(T=,mixed):mixed $callable |
||
| 3567 | * @psalm-return static<TKey,T> |
||
| 3568 | * @psalm-mutation-free |
||
| 3569 | */ |
||
| 3570 | 1 | public function invoke($callable, $arguments = []): self |
|
| 3594 | |||
| 3595 | /** |
||
| 3596 | * Check whether array is associative or not. |
||
| 3597 | * |
||
| 3598 | * EXAMPLE: <code> |
||
| 3599 | * a(['foo' => 'bar', 2, 3])->isAssoc(); // true |
||
| 3600 | * </code> |
||
| 3601 | * |
||
| 3602 | * @param bool $recursive |
||
| 3603 | * |
||
| 3604 | * @return bool |
||
| 3605 | * <p>Returns true if associative, false otherwise.</p> |
||
| 3606 | */ |
||
| 3607 | 15 | View Code Duplication | public function isAssoc(bool $recursive = false): bool |
| 3622 | |||
| 3623 | /** |
||
| 3624 | * Check if a given key or keys are empty. |
||
| 3625 | * |
||
| 3626 | * @param int|int[]|string|string[]|null $keys |
||
| 3627 | * |
||
| 3628 | * @return bool |
||
| 3629 | * <p>Returns true if empty, false otherwise.</p> |
||
| 3630 | * @psalm-mutation-free |
||
| 3631 | */ |
||
| 3632 | 45 | public function isEmpty($keys = null): bool |
|
| 3650 | |||
| 3651 | /** |
||
| 3652 | * Check if the current array is equal to the given "$array" or not. |
||
| 3653 | * |
||
| 3654 | * EXAMPLE: <code> |
||
| 3655 | * a(['💩'])->isEqual(['💩']); // true |
||
| 3656 | * </code> |
||
| 3657 | * |
||
| 3658 | * @param array $array |
||
| 3659 | * |
||
| 3660 | * @return bool |
||
| 3661 | * |
||
| 3662 | * @psalm-param array<mixed,mixed> $array |
||
| 3663 | */ |
||
| 3664 | 1 | public function isEqual(array $array): bool |
|
| 3668 | |||
| 3669 | /** |
||
| 3670 | * Check if the current array is a multi-array. |
||
| 3671 | * |
||
| 3672 | * EXAMPLE: <code> |
||
| 3673 | * a(['foo' => [1, 2 , 3]])->isMultiArray(); // true |
||
| 3674 | * </code> |
||
| 3675 | * |
||
| 3676 | * @return bool |
||
| 3677 | */ |
||
| 3678 | 22 | public function isMultiArray(): bool |
|
| 3688 | |||
| 3689 | /** |
||
| 3690 | * Check whether array is numeric or not. |
||
| 3691 | * |
||
| 3692 | * @return bool |
||
| 3693 | * <p>Returns true if numeric, false otherwise.</p> |
||
| 3694 | */ |
||
| 3695 | 5 | View Code Duplication | public function isNumeric(): bool |
| 3710 | |||
| 3711 | /** |
||
| 3712 | * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not. |
||
| 3713 | * |
||
| 3714 | * EXAMPLE: <code> |
||
| 3715 | * a([0 => 'foo', 1 => 'lall', 2 => 'foobar'])->isSequential(); // true |
||
| 3716 | * </code> |
||
| 3717 | * |
||
| 3718 | * INFO: If the array is empty we count it as non-sequential. |
||
| 3719 | * |
||
| 3720 | * @param bool $recursive |
||
| 3721 | * |
||
| 3722 | * @return bool |
||
| 3723 | * @psalm-mutation-free |
||
| 3724 | */ |
||
| 3725 | 10 | public function isSequential(bool $recursive = false): bool |
|
| 3753 | |||
| 3754 | /** |
||
| 3755 | * @return array |
||
| 3756 | * |
||
| 3757 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 3758 | */ |
||
| 3759 | 2 | public function jsonSerialize(): array |
|
| 3763 | |||
| 3764 | /** |
||
| 3765 | * Gets the key/index of the element at the current internal iterator position. |
||
| 3766 | * |
||
| 3767 | * @return int|string|null |
||
| 3768 | */ |
||
| 3769 | public function key() |
||
| 3773 | |||
| 3774 | /** |
||
| 3775 | * Checks if the given key exists in the provided array. |
||
| 3776 | * |
||
| 3777 | * INFO: This method only use "array_key_exists()" if you want to use "dot"-notation, |
||
| 3778 | * then you need to use "Arrayy->offsetExists()". |
||
| 3779 | * |
||
| 3780 | * @param int|string $key the key to look for |
||
| 3781 | * |
||
| 3782 | * @return bool |
||
| 3783 | * @psalm-mutation-free |
||
| 3784 | */ |
||
| 3785 | 164 | public function keyExists($key): bool |
|
| 3789 | |||
| 3790 | /** |
||
| 3791 | * Get all keys from the current array. |
||
| 3792 | * |
||
| 3793 | * EXAMPLE: <code> |
||
| 3794 | * a([1 => 'foo', 2 => 'foo2', 3 => 'bar'])->keys(); // Arrayy[1, 2, 3] |
||
| 3795 | * </code> |
||
| 3796 | * |
||
| 3797 | * @param bool $recursive [optional] <p> |
||
| 3798 | * Get all keys, also from all sub-arrays from an multi-dimensional array. |
||
| 3799 | * </p> |
||
| 3800 | * @param mixed|null $search_values [optional] <p> |
||
| 3801 | * If specified, then only keys containing these values are returned. |
||
| 3802 | * </p> |
||
| 3803 | * @param bool $strict [optional] <p> |
||
| 3804 | * Determines if strict comparison (===) should be used during the search. |
||
| 3805 | * </p> |
||
| 3806 | * |
||
| 3807 | * @return static |
||
| 3808 | * <p>(Immutable) An array of all the keys in input.</p> |
||
| 3809 | * |
||
| 3810 | * @psalm-return static<array-key,TKey> |
||
| 3811 | * @psalm-mutation-free |
||
| 3812 | */ |
||
| 3813 | 29 | public function keys( |
|
| 3814 | bool $recursive = false, |
||
| 3815 | $search_values = null, |
||
| 3816 | bool $strict = true |
||
| 3817 | ): self { |
||
| 3818 | |||
| 3819 | // recursive |
||
| 3820 | |||
| 3821 | 29 | if ($recursive === true) { |
|
| 3822 | 4 | $array = $this->array_keys_recursive( |
|
| 3823 | 4 | null, |
|
| 3824 | 4 | $search_values, |
|
| 3825 | 4 | $strict |
|
| 3826 | ); |
||
| 3827 | |||
| 3828 | 4 | return static::create( |
|
| 3829 | 4 | $array, |
|
| 3830 | 4 | $this->iteratorClass, |
|
| 3831 | 4 | false |
|
| 3832 | ); |
||
| 3833 | } |
||
| 3834 | |||
| 3835 | // non recursive |
||
| 3836 | |||
| 3837 | 28 | if ($search_values === null) { |
|
| 3838 | 28 | $arrayFunction = function (): \Generator { |
|
| 3839 | 28 | foreach ($this->getGenerator() as $key => $value) { |
|
| 3840 | 26 | yield $key; |
|
| 3841 | } |
||
| 3842 | 28 | }; |
|
| 3843 | } else { |
||
| 3844 | 1 | $arrayFunction = function () use ($search_values, $strict): \Generator { |
|
| 3845 | 1 | $is_array_tmp = \is_array($search_values); |
|
| 3846 | |||
| 3847 | /** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ |
||
| 3848 | 1 | foreach ($this->getGeneratorByReference() as $key => &$value) { |
|
| 3849 | View Code Duplication | if ( |
|
| 3850 | ( |
||
| 3851 | 1 | $is_array_tmp === false |
|
| 3852 | && |
||
| 3853 | 1 | $strict === true |
|
| 3854 | && |
||
| 3855 | 1 | $search_values === $value |
|
| 3856 | ) |
||
| 3857 | || |
||
| 3858 | ( |
||
| 3859 | 1 | $is_array_tmp === false |
|
| 3860 | && |
||
| 3861 | 1 | $strict === false |
|
| 3862 | && |
||
| 3863 | 1 | $search_values == $value |
|
| 3864 | ) |
||
| 3865 | || |
||
| 3866 | ( |
||
| 3867 | 1 | $is_array_tmp === true |
|
| 3868 | && |
||
| 3869 | 1 | \in_array($value, $search_values, $strict) |
|
| 3870 | ) |
||
| 3871 | ) { |
||
| 3872 | 1 | yield $key; |
|
| 3873 | } |
||
| 3874 | } |
||
| 3875 | 1 | }; |
|
| 3876 | } |
||
| 3877 | |||
| 3878 | 28 | return static::create( |
|
| 3879 | 28 | $arrayFunction, |
|
| 3880 | 28 | $this->iteratorClass, |
|
| 3881 | 28 | false |
|
| 3882 | ); |
||
| 3883 | } |
||
| 3884 | |||
| 3885 | /** |
||
| 3886 | * Sort an array by key in reverse order. |
||
| 3887 | * |
||
| 3888 | * @param int $sort_flags [optional] <p> |
||
| 3889 | * You may modify the behavior of the sort using the optional |
||
| 3890 | * parameter sort_flags, for details |
||
| 3891 | * see sort. |
||
| 3892 | * </p> |
||
| 3893 | * |
||
| 3894 | * @return $this |
||
| 3895 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 3896 | * |
||
| 3897 | * @psalm-return static<TKey,T> |
||
| 3898 | */ |
||
| 3899 | 4 | public function krsort(int $sort_flags = 0): self |
|
| 3907 | |||
| 3908 | /** |
||
| 3909 | * Sort an array by key in reverse order. |
||
| 3910 | * |
||
| 3911 | * @param int $sort_flags [optional] <p> |
||
| 3912 | * You may modify the behavior of the sort using the optional |
||
| 3913 | * parameter sort_flags, for details |
||
| 3914 | * see sort. |
||
| 3915 | * </p> |
||
| 3916 | * |
||
| 3917 | * @return $this |
||
| 3918 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 3919 | * |
||
| 3920 | * @psalm-return static<TKey,T> |
||
| 3921 | * @psalm-mutation-free |
||
| 3922 | */ |
||
| 3923 | 4 | public function krsortImmutable(int $sort_flags = 0): self |
|
| 3934 | |||
| 3935 | /** |
||
| 3936 | * Get the last value from the current array. |
||
| 3937 | * |
||
| 3938 | * EXAMPLE: <code> |
||
| 3939 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->last(); // 'lall' |
||
| 3940 | * </code> |
||
| 3941 | * |
||
| 3942 | * @return mixed|null |
||
| 3943 | * <p>Return null if there wasn't a element.</p> |
||
| 3944 | * @psalm-mutation-free |
||
| 3945 | */ |
||
| 3946 | 17 | public function last() |
|
| 3955 | |||
| 3956 | /** |
||
| 3957 | * Get the last key from the current array. |
||
| 3958 | * |
||
| 3959 | * @return mixed|null |
||
| 3960 | * <p>Return null if there wasn't a element.</p> |
||
| 3961 | * @psalm-mutation-free |
||
| 3962 | */ |
||
| 3963 | 21 | public function lastKey() |
|
| 3969 | |||
| 3970 | /** |
||
| 3971 | * Get the last value(s) from the current array. |
||
| 3972 | * |
||
| 3973 | * EXAMPLE: <code> |
||
| 3974 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall'] |
||
| 3975 | * </code> |
||
| 3976 | * |
||
| 3977 | * @param int|null $number |
||
| 3978 | * |
||
| 3979 | * @return static |
||
| 3980 | * <p>(Immutable)</p> |
||
| 3981 | * |
||
| 3982 | * @psalm-return static<TKey,T> |
||
| 3983 | * @psalm-mutation-free |
||
| 3984 | */ |
||
| 3985 | 13 | public function lastsImmutable(int $number = null): self |
|
| 4015 | |||
| 4016 | /** |
||
| 4017 | * Get the last value(s) from the current array. |
||
| 4018 | * |
||
| 4019 | * EXAMPLE: <code> |
||
| 4020 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall'] |
||
| 4021 | * </code> |
||
| 4022 | * |
||
| 4023 | * @param int|null $number |
||
| 4024 | * |
||
| 4025 | * @return $this |
||
| 4026 | * <p>(Mutable)</p> |
||
| 4027 | * |
||
| 4028 | * @psalm-return static<TKey,T> |
||
| 4029 | */ |
||
| 4030 | 13 | public function lastsMutable(int $number = null): self |
|
| 4041 | |||
| 4042 | /** |
||
| 4043 | * Count the values from the current array. |
||
| 4044 | * |
||
| 4045 | * alias: for "Arrayy->count()" |
||
| 4046 | * |
||
| 4047 | * @param int $mode |
||
| 4048 | * |
||
| 4049 | * @return int |
||
| 4050 | * |
||
| 4051 | * @see Arrayy::count() |
||
| 4052 | */ |
||
| 4053 | 20 | public function length(int $mode = \COUNT_NORMAL): int |
|
| 4057 | |||
| 4058 | /** |
||
| 4059 | * Apply the given function to the every element of the array, |
||
| 4060 | * collecting the results. |
||
| 4061 | * |
||
| 4062 | * @param callable $callable |
||
| 4063 | * @param bool $useKeyAsSecondParameter |
||
| 4064 | * @param mixed ...$arguments |
||
| 4065 | * |
||
| 4066 | * @return static |
||
| 4067 | * <p>(Immutable) Arrayy object with modified elements.</p> |
||
| 4068 | * |
||
| 4069 | * @psalm-param callable(T,TKey=,mixed=):mixed $callable |
||
| 4070 | * @psalm-return static<TKey,T> |
||
| 4071 | * @psalm-mutation-free |
||
| 4072 | */ |
||
| 4073 | 5 | public function map( |
|
| 4074 | callable $callable, |
||
| 4075 | bool $useKeyAsSecondParameter = false, |
||
| 4076 | ...$arguments |
||
| 4077 | ) { |
||
| 4078 | /** |
||
| 4079 | * @psalm-suppress ImpureFunctionCall - func_num_args is only used to detect the number of args |
||
| 4080 | */ |
||
| 4081 | 5 | $useArguments = \func_num_args() > 2; |
|
| 4082 | |||
| 4083 | 5 | return static::create( |
|
| 4084 | 5 | function () use ($useArguments, $callable, $useKeyAsSecondParameter, $arguments) { |
|
| 4085 | 5 | foreach ($this->getGenerator() as $key => $value) { |
|
| 4086 | 4 | if ($useArguments) { |
|
| 4087 | 3 | if ($useKeyAsSecondParameter) { |
|
| 4088 | yield $key => $callable($value, $key, ...$arguments); |
||
| 4089 | } else { |
||
| 4090 | 3 | yield $key => $callable($value, ...$arguments); |
|
| 4091 | } |
||
| 4092 | } else { |
||
| 4093 | /** @noinspection NestedPositiveIfStatementsInspection */ |
||
| 4094 | 4 | if ($useKeyAsSecondParameter) { |
|
| 4095 | yield $key => $callable($value, $key); |
||
| 4096 | } else { |
||
| 4097 | 4 | yield $key => $callable($value); |
|
| 4098 | } |
||
| 4099 | } |
||
| 4100 | } |
||
| 4101 | 5 | }, |
|
| 4102 | 5 | $this->iteratorClass, |
|
| 4103 | 5 | false |
|
| 4104 | ); |
||
| 4105 | } |
||
| 4106 | |||
| 4107 | /** |
||
| 4108 | * Check if all items in current array match a truth test. |
||
| 4109 | * |
||
| 4110 | * EXAMPLE: <code> |
||
| 4111 | * $closure = function ($value, $key) { |
||
| 4112 | * return ($value % 2 === 0); |
||
| 4113 | * }; |
||
| 4114 | * a([2, 4, 8])->matches($closure); // true |
||
| 4115 | * </code> |
||
| 4116 | * |
||
| 4117 | * @param \Closure $closure |
||
| 4118 | * |
||
| 4119 | * @return bool |
||
| 4120 | */ |
||
| 4121 | 15 | View Code Duplication | public function matches(\Closure $closure): bool |
| 4137 | |||
| 4138 | /** |
||
| 4139 | * Check if any item in the current array matches a truth test. |
||
| 4140 | * |
||
| 4141 | * EXAMPLE: <code> |
||
| 4142 | * $closure = function ($value, $key) { |
||
| 4143 | * return ($value % 2 === 0); |
||
| 4144 | * }; |
||
| 4145 | * a([1, 4, 7])->matches($closure); // true |
||
| 4146 | * </code> |
||
| 4147 | * |
||
| 4148 | * @param \Closure $closure |
||
| 4149 | * |
||
| 4150 | * @return bool |
||
| 4151 | */ |
||
| 4152 | 14 | View Code Duplication | public function matchesAny(\Closure $closure): bool |
| 4168 | |||
| 4169 | /** |
||
| 4170 | * Get the max value from an array. |
||
| 4171 | * |
||
| 4172 | * EXAMPLE: <code> |
||
| 4173 | * a([-9, -8, -7, 1.32])->max(); // 1.32 |
||
| 4174 | * </code> |
||
| 4175 | * |
||
| 4176 | * @return false|mixed |
||
| 4177 | * <p>Will return false if there are no values.</p> |
||
| 4178 | */ |
||
| 4179 | 10 | View Code Duplication | public function max() |
| 4199 | |||
| 4200 | /** |
||
| 4201 | * Merge the new $array into the current array. |
||
| 4202 | * |
||
| 4203 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 4204 | * |
||
| 4205 | * EXAMPLE: <code> |
||
| 4206 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4207 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4208 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[1 => 'one', 'foo' => 'bar2', 3 => 'three'] |
||
| 4209 | * // --- |
||
| 4210 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4211 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4212 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2'] |
||
| 4213 | * </code> |
||
| 4214 | * |
||
| 4215 | * @param array $array |
||
| 4216 | * @param bool $recursive |
||
| 4217 | * |
||
| 4218 | * @return static |
||
| 4219 | * <p>(Immutable)</p> |
||
| 4220 | * |
||
| 4221 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4222 | * @psalm-return static<int|TKey,T> |
||
| 4223 | * @psalm-mutation-free |
||
| 4224 | */ |
||
| 4225 | 33 | View Code Duplication | public function mergeAppendKeepIndex(array $array = [], bool $recursive = false): self |
| 4240 | |||
| 4241 | /** |
||
| 4242 | * Merge the new $array into the current array. |
||
| 4243 | * |
||
| 4244 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
| 4245 | * - create new indexes |
||
| 4246 | * |
||
| 4247 | * EXAMPLE: <code> |
||
| 4248 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4249 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4250 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 'foo' => 'bar2', 1 => 'three'] |
||
| 4251 | * // --- |
||
| 4252 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4253 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4254 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 1 => 'foo', 2 => 'foo', 3 => 'bar2'] |
||
| 4255 | * </code> |
||
| 4256 | * |
||
| 4257 | * @param array $array |
||
| 4258 | * @param bool $recursive |
||
| 4259 | * |
||
| 4260 | * @return static |
||
| 4261 | * <p>(Immutable)</p> |
||
| 4262 | * |
||
| 4263 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4264 | * @psalm-return static<TKey,T> |
||
| 4265 | * @psalm-mutation-free |
||
| 4266 | */ |
||
| 4267 | 20 | View Code Duplication | public function mergeAppendNewIndex(array $array = [], bool $recursive = false): self |
| 4282 | |||
| 4283 | /** |
||
| 4284 | * Merge the the current array into the $array. |
||
| 4285 | * |
||
| 4286 | * - use key,value from the new $array, also if the index is in the current array |
||
| 4287 | * |
||
| 4288 | * EXAMPLE: <code> |
||
| 4289 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4290 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4291 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy['foo' => 'bar1', 3 => 'three', 1 => 'one'] |
||
| 4292 | * // --- |
||
| 4293 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4294 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4295 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy[0 => 'one', 1 => 'foo'] |
||
| 4296 | * </code> |
||
| 4297 | * |
||
| 4298 | * @param array $array |
||
| 4299 | * @param bool $recursive |
||
| 4300 | * |
||
| 4301 | * @return static |
||
| 4302 | * <p>(Immutable)</p> |
||
| 4303 | * |
||
| 4304 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4305 | * @psalm-return static<TKey,T> |
||
| 4306 | * @psalm-mutation-free |
||
| 4307 | */ |
||
| 4308 | 17 | View Code Duplication | public function mergePrependKeepIndex(array $array = [], bool $recursive = false): self |
| 4323 | |||
| 4324 | /** |
||
| 4325 | * Merge the current array into the new $array. |
||
| 4326 | * |
||
| 4327 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
| 4328 | * - create new indexes |
||
| 4329 | * |
||
| 4330 | * EXAMPLE: <code> |
||
| 4331 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4332 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4333 | * a($array1)->mergePrependNewIndex($array2); // Arrayy['foo' => 'bar1', 0 => 'three', 1 => 'one'] |
||
| 4334 | * // --- |
||
| 4335 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4336 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4337 | * a($array1)->mergePrependNewIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2', 2 => 'one', 3 => 'foo'] |
||
| 4338 | * </code> |
||
| 4339 | * |
||
| 4340 | * @param array $array |
||
| 4341 | * @param bool $recursive |
||
| 4342 | * |
||
| 4343 | * @return static |
||
| 4344 | * <p>(Immutable)</p> |
||
| 4345 | * |
||
| 4346 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4347 | * @psalm-return static<TKey,T> |
||
| 4348 | * @psalm-mutation-free |
||
| 4349 | */ |
||
| 4350 | 21 | View Code Duplication | public function mergePrependNewIndex(array $array = [], bool $recursive = false): self |
| 4365 | |||
| 4366 | /** |
||
| 4367 | * @return ArrayyMeta|static |
||
| 4368 | */ |
||
| 4369 | 18 | public static function meta() |
|
| 4373 | |||
| 4374 | /** |
||
| 4375 | * Get the min value from an array. |
||
| 4376 | * |
||
| 4377 | * EXAMPLE: <code> |
||
| 4378 | * a([-9, -8, -7, 1.32])->min(); // -9 |
||
| 4379 | * </code> |
||
| 4380 | * |
||
| 4381 | * @return false|mixed |
||
| 4382 | * <p>Will return false if there are no values.</p> |
||
| 4383 | */ |
||
| 4384 | 10 | View Code Duplication | public function min() |
| 4404 | |||
| 4405 | /** |
||
| 4406 | * Get the most used value from the array. |
||
| 4407 | * |
||
| 4408 | * @return mixed|null |
||
| 4409 | * <p>(Immutable) Return null if there wasn't a element.</p> |
||
| 4410 | * @psalm-mutation-free |
||
| 4411 | */ |
||
| 4412 | 3 | public function mostUsedValue() |
|
| 4416 | |||
| 4417 | /** |
||
| 4418 | * Get the most used value from the array. |
||
| 4419 | * |
||
| 4420 | * @param int|null $number <p>How many values you will take?</p> |
||
| 4421 | * |
||
| 4422 | * @return static |
||
| 4423 | * <p>(Immutable)</p> |
||
| 4424 | * |
||
| 4425 | * @psalm-return static<TKey,T> |
||
| 4426 | * @psalm-mutation-free |
||
| 4427 | */ |
||
| 4428 | 3 | public function mostUsedValues(int $number = null): self |
|
| 4432 | |||
| 4433 | /** |
||
| 4434 | * Move an array element to a new index. |
||
| 4435 | * |
||
| 4436 | * EXAMPLE: <code> |
||
| 4437 | * $arr2 = new A(['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e']); |
||
| 4438 | * $newArr2 = $arr2->moveElement('D', 1); // Arrayy['A' => 'a', 'D' => 'd', 'B' => 'b', 'C' => 'c', 'E' => 'e'] |
||
| 4439 | * </code> |
||
| 4440 | * |
||
| 4441 | * @param int|string $from |
||
| 4442 | * @param int $to |
||
| 4443 | * |
||
| 4444 | * @return static |
||
| 4445 | * <p>(Immutable)</p> |
||
| 4446 | * |
||
| 4447 | * @psalm-return static<TKey,T> |
||
| 4448 | * @psalm-mutation-free |
||
| 4449 | */ |
||
| 4450 | 1 | public function moveElement($from, $to): self |
|
| 4483 | |||
| 4484 | /** |
||
| 4485 | * Move an array element to the first place. |
||
| 4486 | * |
||
| 4487 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4488 | * loss the keys of an indexed array. |
||
| 4489 | * |
||
| 4490 | * @param int|string $key |
||
| 4491 | * |
||
| 4492 | * @return static |
||
| 4493 | * <p>(Immutable)</p> |
||
| 4494 | * |
||
| 4495 | * @psalm-return static<TKey,T> |
||
| 4496 | * @psalm-mutation-free |
||
| 4497 | */ |
||
| 4498 | 1 | View Code Duplication | public function moveElementToFirstPlace($key): self |
| 4514 | |||
| 4515 | /** |
||
| 4516 | * Move an array element to the last place. |
||
| 4517 | * |
||
| 4518 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4519 | * loss the keys of an indexed array. |
||
| 4520 | * |
||
| 4521 | * @param int|string $key |
||
| 4522 | * |
||
| 4523 | * @return static |
||
| 4524 | * <p>(Immutable)</p> |
||
| 4525 | * |
||
| 4526 | * @psalm-return static<TKey,T> |
||
| 4527 | * @psalm-mutation-free |
||
| 4528 | */ |
||
| 4529 | 1 | View Code Duplication | public function moveElementToLastPlace($key): self |
| 4545 | |||
| 4546 | /** |
||
| 4547 | * Moves the internal iterator position to the next element and returns this element. |
||
| 4548 | * |
||
| 4549 | * @return false|mixed |
||
| 4550 | * <p>(Mutable) Will return false if there are no values.</p> |
||
| 4551 | */ |
||
| 4552 | public function next() |
||
| 4556 | |||
| 4557 | /** |
||
| 4558 | * Get the next nth keys and values from the array. |
||
| 4559 | * |
||
| 4560 | * @param int $step |
||
| 4561 | * @param int $offset |
||
| 4562 | * |
||
| 4563 | * @return static |
||
| 4564 | * <p>(Immutable)</p> |
||
| 4565 | * |
||
| 4566 | * @psalm-return static<TKey,T> |
||
| 4567 | * @psalm-mutation-free |
||
| 4568 | */ |
||
| 4569 | public function nth(int $step, int $offset = 0): self |
||
| 4588 | |||
| 4589 | /** |
||
| 4590 | * Get a subset of the items from the given array. |
||
| 4591 | * |
||
| 4592 | * @param int[]|string[] $keys |
||
| 4593 | * |
||
| 4594 | * @return static |
||
| 4595 | * <p>(Immutable)</p> |
||
| 4596 | * |
||
| 4597 | * @psalm-param array-key[] $keys |
||
| 4598 | * @psalm-return static<TKey,T> |
||
| 4599 | * @psalm-mutation-free |
||
| 4600 | */ |
||
| 4601 | 1 | View Code Duplication | public function only(array $keys): self |
| 4602 | { |
||
| 4603 | 1 | $keys = \array_flip($keys); |
|
| 4604 | |||
| 4605 | 1 | $generator = function () use ($keys): \Generator { |
|
| 4606 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 4607 | 1 | if (isset($keys[$key])) { |
|
| 4608 | 1 | yield $key => $value; |
|
| 4609 | } |
||
| 4610 | } |
||
| 4611 | 1 | }; |
|
| 4612 | |||
| 4613 | 1 | return static::create( |
|
| 4614 | 1 | $generator, |
|
| 4615 | 1 | $this->iteratorClass, |
|
| 4616 | 1 | false |
|
| 4617 | ); |
||
| 4618 | } |
||
| 4619 | |||
| 4620 | /** |
||
| 4621 | * Pad array to the specified size with a given value. |
||
| 4622 | * |
||
| 4623 | * @param int $size <p>Size of the result array.</p> |
||
| 4624 | * @param mixed $value <p>Empty value by default.</p> |
||
| 4625 | * |
||
| 4626 | * @return static |
||
| 4627 | * <p>(Immutable) Arrayy object padded to $size with $value.</p> |
||
| 4628 | * |
||
| 4629 | * @psalm-return static<TKey,T> |
||
| 4630 | * @psalm-mutation-free |
||
| 4631 | */ |
||
| 4632 | 5 | public function pad(int $size, $value): self |
|
| 4640 | |||
| 4641 | /** |
||
| 4642 | * Partitions this array in two array according to a predicate. |
||
| 4643 | * Keys are preserved in the resulting array. |
||
| 4644 | * |
||
| 4645 | * @param \Closure $closure |
||
| 4646 | * <p>The predicate on which to partition.</p> |
||
| 4647 | * |
||
| 4648 | * @return array<int, static> |
||
| 4649 | * <p>An array with two elements. The first element contains the array |
||
| 4650 | * of elements where the predicate returned TRUE, the second element |
||
| 4651 | * contains the array of elements where the predicate returned FALSE.</p> |
||
| 4652 | * |
||
| 4653 | * @psalm-return array<int, static<TKey,T>> |
||
| 4654 | */ |
||
| 4655 | 1 | public function partition(\Closure $closure): array |
|
| 4671 | |||
| 4672 | /** |
||
| 4673 | * Pop a specified value off the end of the current array. |
||
| 4674 | * |
||
| 4675 | * @return mixed|null |
||
| 4676 | * <p>(Mutable) The popped element from the current array or null if the array is e.g. empty.</p> |
||
| 4677 | */ |
||
| 4678 | 5 | public function pop() |
|
| 4684 | |||
| 4685 | /** |
||
| 4686 | * Prepend a (key) + value to the current array. |
||
| 4687 | * |
||
| 4688 | * EXAMPLE: <code> |
||
| 4689 | * a(['fòô' => 'bàř'])->prepend('foo'); // Arrayy[0 => 'foo', 'fòô' => 'bàř'] |
||
| 4690 | * </code> |
||
| 4691 | * |
||
| 4692 | * @param mixed $value |
||
| 4693 | * @param mixed $key |
||
| 4694 | * |
||
| 4695 | * @return $this |
||
| 4696 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4697 | * |
||
| 4698 | * @psalm-return static<TKey,T> |
||
| 4699 | */ |
||
| 4700 | 11 | public function prepend($value, $key = null) |
|
| 4716 | |||
| 4717 | /** |
||
| 4718 | * Prepend a (key) + value to the current array. |
||
| 4719 | * |
||
| 4720 | * EXAMPLE: <code> |
||
| 4721 | * a(['fòô' => 'bàř'])->prependImmutable('foo')->getArray(); // [0 => 'foo', 'fòô' => 'bàř'] |
||
| 4722 | * </code> |
||
| 4723 | * |
||
| 4724 | * @param mixed $value |
||
| 4725 | * @param mixed $key |
||
| 4726 | * |
||
| 4727 | * @return $this |
||
| 4728 | * <p>(Immutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4729 | * |
||
| 4730 | * @psalm-return static<TKey,T> |
||
| 4731 | * @psalm-mutation-free |
||
| 4732 | */ |
||
| 4733 | View Code Duplication | public function prependImmutable($value, $key = null) |
|
| 4758 | |||
| 4759 | /** |
||
| 4760 | * Add a suffix to each key. |
||
| 4761 | * |
||
| 4762 | * @param mixed $suffix |
||
| 4763 | * |
||
| 4764 | * @return static |
||
| 4765 | * <p>(Immutable) Return an Arrayy object, with the prepended keys.</p> |
||
| 4766 | * |
||
| 4767 | * @psalm-return static<TKey,T> |
||
| 4768 | * @psalm-mutation-free |
||
| 4769 | */ |
||
| 4770 | 10 | View Code Duplication | public function prependToEachKey($suffix): self |
| 4796 | |||
| 4797 | /** |
||
| 4798 | * Add a suffix to each value. |
||
| 4799 | * |
||
| 4800 | * @param mixed $suffix |
||
| 4801 | * |
||
| 4802 | * @return static |
||
| 4803 | * <p>(Immutable) Return an Arrayy object, with the prepended values.</p> |
||
| 4804 | * |
||
| 4805 | * @psalm-return static<TKey,T> |
||
| 4806 | * @psalm-mutation-free |
||
| 4807 | */ |
||
| 4808 | 10 | View Code Duplication | public function prependToEachValue($suffix): self |
| 4836 | |||
| 4837 | /** |
||
| 4838 | * Return the value of a given key and |
||
| 4839 | * delete the key. |
||
| 4840 | * |
||
| 4841 | * @param int|int[]|string|string[]|null $keyOrKeys |
||
| 4842 | * @param mixed $fallback |
||
| 4843 | * |
||
| 4844 | * @return mixed |
||
| 4845 | */ |
||
| 4846 | 5 | public function pull($keyOrKeys = null, $fallback = null) |
|
| 4868 | |||
| 4869 | /** |
||
| 4870 | * Push one or more values onto the end of array at once. |
||
| 4871 | * |
||
| 4872 | * @param array ...$args |
||
| 4873 | * |
||
| 4874 | * @return $this |
||
| 4875 | * <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p> |
||
| 4876 | * |
||
| 4877 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 4878 | * |
||
| 4879 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$args |
||
| 4880 | * @psalm-return static<TKey,T> |
||
| 4881 | */ |
||
| 4882 | 7 | public function push(...$args) |
|
| 4900 | |||
| 4901 | /** |
||
| 4902 | * Get a random value from the current array. |
||
| 4903 | * |
||
| 4904 | * EXAMPLE: <code> |
||
| 4905 | * a([1, 2, 3, 4])->randomImmutable(2); // e.g.: Arrayy[1, 4] |
||
| 4906 | * </code> |
||
| 4907 | * |
||
| 4908 | * @param int|null $number <p>How many values you will take?</p> |
||
| 4909 | * |
||
| 4910 | * @return static |
||
| 4911 | * <p>(Immutable)</p> |
||
| 4912 | * |
||
| 4913 | * @psalm-return static<int|array-key,T> |
||
| 4914 | */ |
||
| 4915 | 19 | public function randomImmutable(int $number = null): self |
|
| 4948 | |||
| 4949 | /** |
||
| 4950 | * Pick a random key/index from the keys of this array. |
||
| 4951 | * |
||
| 4952 | * EXAMPLE: <code> |
||
| 4953 | * $arrayy = A::create([1 => 'one', 2 => 'two']); |
||
| 4954 | * $arrayy->randomKey(); // e.g. 2 |
||
| 4955 | * </code> |
||
| 4956 | * |
||
| 4957 | * @throws \RangeException If array is empty |
||
| 4958 | * |
||
| 4959 | * @return mixed |
||
| 4960 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 4961 | */ |
||
| 4962 | 4 | public function randomKey() |
|
| 4972 | |||
| 4973 | /** |
||
| 4974 | * Pick a given number of random keys/indexes out of this array. |
||
| 4975 | * |
||
| 4976 | * EXAMPLE: <code> |
||
| 4977 | * a([1 => 'one', 2 => 'two'])->randomKeys(); // e.g. Arrayy[1, 2] |
||
| 4978 | * </code> |
||
| 4979 | * |
||
| 4980 | * @param int $number <p>The number of keys/indexes (should be <= \count($this->array))</p> |
||
| 4981 | * |
||
| 4982 | * @throws \RangeException If array is empty |
||
| 4983 | * |
||
| 4984 | * @return static |
||
| 4985 | * <p>(Immutable)</p> |
||
| 4986 | * |
||
| 4987 | * @psalm-return static<TKey,T> |
||
| 4988 | */ |
||
| 4989 | 13 | public function randomKeys(int $number): self |
|
| 5017 | |||
| 5018 | /** |
||
| 5019 | * Get a random value from the current array. |
||
| 5020 | * |
||
| 5021 | * EXAMPLE: <code> |
||
| 5022 | * a([1, 2, 3, 4])->randomMutable(2); // e.g.: Arrayy[1, 4] |
||
| 5023 | * </code> |
||
| 5024 | * |
||
| 5025 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5026 | * |
||
| 5027 | * @return $this |
||
| 5028 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5029 | * |
||
| 5030 | * @psalm-return static<TKey,T> |
||
| 5031 | */ |
||
| 5032 | 17 | public function randomMutable(int $number = null): self |
|
| 5057 | |||
| 5058 | /** |
||
| 5059 | * Pick a random value from the values of this array. |
||
| 5060 | * |
||
| 5061 | * EXAMPLE: <code> |
||
| 5062 | * a([1 => 'one', 2 => 'two'])->randomValue(); // e.g. 'one' |
||
| 5063 | * </code> |
||
| 5064 | * |
||
| 5065 | * @return mixed |
||
| 5066 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 5067 | */ |
||
| 5068 | 4 | public function randomValue() |
|
| 5078 | |||
| 5079 | /** |
||
| 5080 | * Pick a given number of random values out of this array. |
||
| 5081 | * |
||
| 5082 | * EXAMPLE: <code> |
||
| 5083 | * a([1 => 'one', 2 => 'two'])->randomValues(); // e.g. Arrayy['one', 'two'] |
||
| 5084 | * </code> |
||
| 5085 | * |
||
| 5086 | * @param int $number |
||
| 5087 | * |
||
| 5088 | * @return static |
||
| 5089 | * <p>(Mutable)</p> |
||
| 5090 | * |
||
| 5091 | * @psalm-return static<TKey,T> |
||
| 5092 | */ |
||
| 5093 | 7 | public function randomValues(int $number): self |
|
| 5097 | |||
| 5098 | /** |
||
| 5099 | * Get a random value from an array, with the ability to skew the results. |
||
| 5100 | * |
||
| 5101 | * EXAMPLE: <code> |
||
| 5102 | * a([0 => 3, 1 => 4])->randomWeighted([1 => 4]); // e.g.: Arrayy[4] (has a 66% chance of returning 4) |
||
| 5103 | * </code> |
||
| 5104 | * |
||
| 5105 | * @param array $array |
||
| 5106 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5107 | * |
||
| 5108 | * @return static<int,mixed> |
||
| 5109 | * <p>(Immutable)</p> |
||
| 5110 | * |
||
| 5111 | * @psalm-param array<mixed,mixed> $array |
||
| 5112 | * @psalm-return static<int|array-key,T> |
||
| 5113 | */ |
||
| 5114 | 9 | public function randomWeighted(array $array, int $number = null): self |
|
| 5129 | |||
| 5130 | /** |
||
| 5131 | * Reduce the current array via callable e.g. anonymous-function and return the end result. |
||
| 5132 | * |
||
| 5133 | * EXAMPLE: <code> |
||
| 5134 | * function myReducer($resultArray, $value) { |
||
| 5135 | * if ($value == 'foo') { |
||
| 5136 | * $resultArray[] = $value; |
||
| 5137 | * } |
||
| 5138 | * return $resultArray; |
||
| 5139 | * }; |
||
| 5140 | * a(['foo', 'bar'])->reduce('myReducer'); // Arrayy['foo'] |
||
| 5141 | * </code> |
||
| 5142 | * |
||
| 5143 | * @param callable $callable |
||
| 5144 | * @param mixed $initial |
||
| 5145 | * |
||
| 5146 | * @return static |
||
| 5147 | * <p>(Immutable)</p> |
||
| 5148 | * |
||
| 5149 | * @psalm-return static<TKey,T> |
||
| 5150 | * @psalm-mutation-free |
||
| 5151 | */ |
||
| 5152 | 18 | View Code Duplication | public function reduce($callable, $initial = []): self |
| 5164 | |||
| 5165 | /** |
||
| 5166 | * @param bool $unique |
||
| 5167 | * |
||
| 5168 | * @return static |
||
| 5169 | * <p>(Immutable)</p> |
||
| 5170 | * |
||
| 5171 | * @psalm-return static<TKey,T> |
||
| 5172 | * @psalm-mutation-free |
||
| 5173 | */ |
||
| 5174 | 14 | public function reduce_dimension(bool $unique = true): self |
|
| 5197 | |||
| 5198 | /** |
||
| 5199 | * Create a numerically re-indexed Arrayy object. |
||
| 5200 | * |
||
| 5201 | * EXAMPLE: <code> |
||
| 5202 | * a([2 => 1, 3 => 2])->reindex(); // Arrayy[0 => 1, 1 => 2] |
||
| 5203 | * </code> |
||
| 5204 | * |
||
| 5205 | * @return $this |
||
| 5206 | * <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p> |
||
| 5207 | * |
||
| 5208 | * @psalm-return static<TKey,T> |
||
| 5209 | */ |
||
| 5210 | 9 | public function reindex(): self |
|
| 5218 | |||
| 5219 | /** |
||
| 5220 | * Return all items that fail the truth test. |
||
| 5221 | * |
||
| 5222 | * EXAMPLE: <code> |
||
| 5223 | * $closure = function ($value) { |
||
| 5224 | * return $value % 2 !== 0; |
||
| 5225 | * } |
||
| 5226 | * a([1, 2, 3, 4])->reject($closure); // Arrayy[1 => 2, 3 => 4] |
||
| 5227 | * </code> |
||
| 5228 | * |
||
| 5229 | * @param \Closure $closure |
||
| 5230 | * |
||
| 5231 | * @return static |
||
| 5232 | * <p>(Immutable)</p> |
||
| 5233 | * |
||
| 5234 | * @psalm-return static<TKey,T> |
||
| 5235 | * @psalm-mutation-free |
||
| 5236 | */ |
||
| 5237 | 1 | View Code Duplication | public function reject(\Closure $closure): self |
| 5254 | |||
| 5255 | /** |
||
| 5256 | * Remove a value from the current array (optional using dot-notation). |
||
| 5257 | * |
||
| 5258 | * EXAMPLE: <code> |
||
| 5259 | * a([1 => 'bar', 'foo' => 'foo'])->remove(1); // Arrayy['foo' => 'foo'] |
||
| 5260 | * </code> |
||
| 5261 | * |
||
| 5262 | * @param mixed $key |
||
| 5263 | * |
||
| 5264 | * @return static |
||
| 5265 | * <p>(Mutable)</p> |
||
| 5266 | * |
||
| 5267 | * @psalm-param TKey $key |
||
| 5268 | * @psalm-return static<TKey,T> |
||
| 5269 | */ |
||
| 5270 | 22 | public function remove($key) |
|
| 5293 | |||
| 5294 | /** |
||
| 5295 | * alias: for "Arrayy->removeValue()" |
||
| 5296 | * |
||
| 5297 | * @param mixed $element |
||
| 5298 | * |
||
| 5299 | * @return static |
||
| 5300 | * <p>(Immutable)</p> |
||
| 5301 | * |
||
| 5302 | * @psalm-param T $element |
||
| 5303 | * @psalm-return static<TKey,T> |
||
| 5304 | * @psalm-mutation-free |
||
| 5305 | */ |
||
| 5306 | 8 | public function removeElement($element) |
|
| 5310 | |||
| 5311 | /** |
||
| 5312 | * Remove the first value from the current array. |
||
| 5313 | * |
||
| 5314 | * EXAMPLE: <code> |
||
| 5315 | * a([1 => 'bar', 'foo' => 'foo'])->removeFirst(); // Arrayy['foo' => 'foo'] |
||
| 5316 | * </code> |
||
| 5317 | * |
||
| 5318 | * @return static |
||
| 5319 | * <p>(Immutable)</p> |
||
| 5320 | * |
||
| 5321 | * @psalm-return static<TKey,T> |
||
| 5322 | * @psalm-mutation-free |
||
| 5323 | */ |
||
| 5324 | 7 | View Code Duplication | public function removeFirst(): self |
| 5336 | |||
| 5337 | /** |
||
| 5338 | * Remove the last value from the current array. |
||
| 5339 | * |
||
| 5340 | * EXAMPLE: <code> |
||
| 5341 | * a([1 => 'bar', 'foo' => 'foo'])->removeLast(); // Arrayy[1 => 'bar'] |
||
| 5342 | * </code> |
||
| 5343 | * |
||
| 5344 | * @return static |
||
| 5345 | * <p>(Immutable)</p> |
||
| 5346 | * |
||
| 5347 | * @psalm-return static<TKey,T> |
||
| 5348 | * @psalm-mutation-free |
||
| 5349 | */ |
||
| 5350 | 7 | View Code Duplication | public function removeLast(): self |
| 5362 | |||
| 5363 | /** |
||
| 5364 | * Removes a particular value from an array (numeric or associative). |
||
| 5365 | * |
||
| 5366 | * EXAMPLE: <code> |
||
| 5367 | * a([1 => 'bar', 'foo' => 'foo'])->removeValue('foo'); // Arrayy[1 => 'bar'] |
||
| 5368 | * </code> |
||
| 5369 | * |
||
| 5370 | * @param mixed $value |
||
| 5371 | * |
||
| 5372 | * @return static |
||
| 5373 | * <p>(Immutable)</p> |
||
| 5374 | * |
||
| 5375 | * @psalm-param T $value |
||
| 5376 | * @psalm-return static<TKey,T> |
||
| 5377 | * @psalm-mutation-free |
||
| 5378 | */ |
||
| 5379 | 8 | public function removeValue($value): self |
|
| 5402 | |||
| 5403 | /** |
||
| 5404 | * Generate array of repeated arrays. |
||
| 5405 | * |
||
| 5406 | * @param int $times <p>How many times has to be repeated.</p> |
||
| 5407 | * |
||
| 5408 | * @return static |
||
| 5409 | * <p>(Immutable)</p> |
||
| 5410 | * |
||
| 5411 | * @psalm-return static<TKey,T> |
||
| 5412 | * @psalm-mutation-free |
||
| 5413 | */ |
||
| 5414 | 1 | public function repeat($times): self |
|
| 5426 | |||
| 5427 | /** |
||
| 5428 | * Replace a key with a new key/value pair. |
||
| 5429 | * |
||
| 5430 | * EXAMPLE: <code> |
||
| 5431 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 5432 | * $arrayy->replace(2, 'notfoo', 'notbar'); // Arrayy[1 => 'foo', 'notfoo' => 'notbar', 3 => 'bar'] |
||
| 5433 | * </code> |
||
| 5434 | * |
||
| 5435 | * @param mixed $oldKey |
||
| 5436 | * @param mixed $newKey |
||
| 5437 | * @param mixed $newValue |
||
| 5438 | * |
||
| 5439 | * @return static |
||
| 5440 | * <p>(Immutable)</p> |
||
| 5441 | * |
||
| 5442 | * @psalm-return static<TKey,T> |
||
| 5443 | * @psalm-mutation-free |
||
| 5444 | */ |
||
| 5445 | 5 | public function replace($oldKey, $newKey, $newValue): self |
|
| 5455 | |||
| 5456 | /** |
||
| 5457 | * Create an array using the current array as values and the other array as keys. |
||
| 5458 | * |
||
| 5459 | * EXAMPLE: <code> |
||
| 5460 | * $firstArray = [ |
||
| 5461 | * 1 => 'one', |
||
| 5462 | * 2 => 'two', |
||
| 5463 | * 3 => 'three', |
||
| 5464 | * ]; |
||
| 5465 | * $secondArray = [ |
||
| 5466 | * 'one' => 1, |
||
| 5467 | * 1 => 'one', |
||
| 5468 | * 2 => 2, |
||
| 5469 | * ]; |
||
| 5470 | * $arrayy = a($firstArray); |
||
| 5471 | * $arrayy->replaceAllKeys($secondArray); // Arrayy[1 => "one", 'one' => "two", 2 => "three"] |
||
| 5472 | * </code> |
||
| 5473 | * |
||
| 5474 | * @param array $keys <p>An array of keys.</p> |
||
| 5475 | * |
||
| 5476 | * @return static |
||
| 5477 | * <p>(Immutable) Arrayy object with keys from the other array.</p> |
||
| 5478 | * |
||
| 5479 | * @psalm-param array<mixed,mixed>|array<mixed,TKey> $keys |
||
| 5480 | * @psalm-return static<TKey,T> |
||
| 5481 | * @psalm-mutation-free |
||
| 5482 | */ |
||
| 5483 | 2 | public function replaceAllKeys(array $keys): self |
|
| 5491 | |||
| 5492 | /** |
||
| 5493 | * Create an array using the current array as keys and the other array as values. |
||
| 5494 | * |
||
| 5495 | * EXAMPLE: <code> |
||
| 5496 | * $firstArray = [ |
||
| 5497 | * 1 => 'one', |
||
| 5498 | * 2 => 'two', |
||
| 5499 | * 3 => 'three', |
||
| 5500 | * ]; |
||
| 5501 | * $secondArray = [ |
||
| 5502 | * 'one' => 1, |
||
| 5503 | * 1 => 'one', |
||
| 5504 | * 2 => 2, |
||
| 5505 | * ]; |
||
| 5506 | * $arrayy = a($firstArray); |
||
| 5507 | * $arrayy->replaceAllValues($secondArray); // Arrayy['one' => 1, 'two' => 'one', 'three' => 2] |
||
| 5508 | * </code> |
||
| 5509 | * |
||
| 5510 | * @param array $array <p>An array of values.</p> |
||
| 5511 | * |
||
| 5512 | * @return static |
||
| 5513 | * <p>(Immutable) Arrayy object with values from the other array.</p> |
||
| 5514 | * |
||
| 5515 | * @psalm-param array<mixed,T> $array |
||
| 5516 | * @psalm-return static<TKey,T> |
||
| 5517 | * @psalm-mutation-free |
||
| 5518 | */ |
||
| 5519 | 2 | public function replaceAllValues(array $array): self |
|
| 5527 | |||
| 5528 | /** |
||
| 5529 | * Replace the keys in an array with another set. |
||
| 5530 | * |
||
| 5531 | * EXAMPLE: <code> |
||
| 5532 | * a([1 => 'bar', 'foo' => 'foo'])->replaceKeys([1 => 2, 'foo' => 'replaced']); // Arrayy[2 => 'bar', 'replaced' => 'foo'] |
||
| 5533 | * </code> |
||
| 5534 | * |
||
| 5535 | * @param array $keys <p>An array of keys matching the array's size</p> |
||
| 5536 | * |
||
| 5537 | * @return static |
||
| 5538 | * <p>(Immutable)</p> |
||
| 5539 | * |
||
| 5540 | * @psalm-param array<mixed,mixed>|array<mixed,TKey> $keys |
||
| 5541 | * @psalm-return static<TKey,T> |
||
| 5542 | * @psalm-mutation-free |
||
| 5543 | */ |
||
| 5544 | 1 | public function replaceKeys(array $keys): self |
|
| 5555 | |||
| 5556 | /** |
||
| 5557 | * Replace the first matched value in an array. |
||
| 5558 | * |
||
| 5559 | * EXAMPLE: <code> |
||
| 5560 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5561 | * a($testArray)->replaceOneValue('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'foobar'] |
||
| 5562 | * </code> |
||
| 5563 | * |
||
| 5564 | * @param mixed $search <p>The value to replace.</p> |
||
| 5565 | * @param mixed $replacement <p>The value to replace.</p> |
||
| 5566 | * |
||
| 5567 | * @return static |
||
| 5568 | * <p>(Immutable)</p> |
||
| 5569 | * |
||
| 5570 | * @psalm-return static<TKey,T> |
||
| 5571 | * @psalm-mutation-free |
||
| 5572 | */ |
||
| 5573 | 3 | View Code Duplication | public function replaceOneValue($search, $replacement = ''): self |
| 5588 | |||
| 5589 | /** |
||
| 5590 | * Replace values in the current array. |
||
| 5591 | * |
||
| 5592 | * EXAMPLE: <code> |
||
| 5593 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5594 | * a($testArray)->replaceValues('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'replacedbar'] |
||
| 5595 | * </code> |
||
| 5596 | * |
||
| 5597 | * @param mixed $search <p>The value to replace.</p> |
||
| 5598 | * @param mixed $replacement <p>What to replace it with.</p> |
||
| 5599 | * |
||
| 5600 | * @return static |
||
| 5601 | * <p>(Immutable)</p> |
||
| 5602 | * |
||
| 5603 | * @psalm-return static<TKey,T> |
||
| 5604 | * @psalm-mutation-free |
||
| 5605 | */ |
||
| 5606 | 1 | public function replaceValues($search, $replacement = ''): self |
|
| 5607 | { |
||
| 5608 | /** |
||
| 5609 | * @psalm-suppress MissingClosureReturnType |
||
| 5610 | * @psalm-suppress MissingClosureParamType |
||
| 5611 | */ |
||
| 5612 | 1 | return $this->each( |
|
| 5613 | 1 | static function ($value) use ($search, $replacement) { |
|
| 5614 | 1 | return \str_replace($search, $replacement, $value); |
|
| 5615 | 1 | } |
|
| 5616 | ); |
||
| 5617 | } |
||
| 5618 | |||
| 5619 | /** |
||
| 5620 | * Get the last elements from index $from until the end of this array. |
||
| 5621 | * |
||
| 5622 | * EXAMPLE: <code> |
||
| 5623 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->rest(2); // Arrayy[0 => 'lall'] |
||
| 5624 | * </code> |
||
| 5625 | * |
||
| 5626 | * @param int $from |
||
| 5627 | * |
||
| 5628 | * @return static |
||
| 5629 | * <p>(Immutable)</p> |
||
| 5630 | * |
||
| 5631 | * @psalm-return static<TKey,T> |
||
| 5632 | * @psalm-mutation-free |
||
| 5633 | */ |
||
| 5634 | 15 | View Code Duplication | public function rest(int $from = 1): self |
| 5644 | |||
| 5645 | /** |
||
| 5646 | * Return the array in the reverse order. |
||
| 5647 | * |
||
| 5648 | * EXAMPLE: <code> |
||
| 5649 | * a([1, 2, 3])->reverse(); // self[3, 2, 1] |
||
| 5650 | * </code> |
||
| 5651 | * |
||
| 5652 | * @return $this |
||
| 5653 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5654 | * |
||
| 5655 | * @psalm-return static<TKey,T> |
||
| 5656 | */ |
||
| 5657 | 9 | public function reverse(): self |
|
| 5665 | |||
| 5666 | /** |
||
| 5667 | * Sort an array in reverse order. |
||
| 5668 | * |
||
| 5669 | * @param int $sort_flags [optional] <p> |
||
| 5670 | * You may modify the behavior of the sort using the optional |
||
| 5671 | * parameter sort_flags, for details |
||
| 5672 | * see sort. |
||
| 5673 | * </p> |
||
| 5674 | * |
||
| 5675 | * @return $this |
||
| 5676 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5677 | * |
||
| 5678 | * @psalm-return static<TKey,T> |
||
| 5679 | */ |
||
| 5680 | 4 | public function rsort(int $sort_flags = 0): self |
|
| 5688 | |||
| 5689 | /** |
||
| 5690 | * Sort an array in reverse order. |
||
| 5691 | * |
||
| 5692 | * @param int $sort_flags [optional] <p> |
||
| 5693 | * You may modify the behavior of the sort using the optional |
||
| 5694 | * parameter sort_flags, for details |
||
| 5695 | * see sort. |
||
| 5696 | * </p> |
||
| 5697 | * |
||
| 5698 | * @return $this |
||
| 5699 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 5700 | * |
||
| 5701 | * @psalm-return static<TKey,T> |
||
| 5702 | * @psalm-mutation-free |
||
| 5703 | */ |
||
| 5704 | 4 | public function rsortImmutable(int $sort_flags = 0): self |
|
| 5715 | |||
| 5716 | /** |
||
| 5717 | * Search for the first index of the current array via $value. |
||
| 5718 | * |
||
| 5719 | * EXAMPLE: <code> |
||
| 5720 | * a(['fòô' => 'bàř', 'lall' => 'bàř'])->searchIndex('bàř'); // Arrayy[0 => 'fòô'] |
||
| 5721 | * </code> |
||
| 5722 | * |
||
| 5723 | * @param mixed $value |
||
| 5724 | * |
||
| 5725 | * @return false|float|int|string |
||
| 5726 | * <p>Will return <b>FALSE</b> if the value can't be found.</p> |
||
| 5727 | * @psalm-mutation-free |
||
| 5728 | */ |
||
| 5729 | 21 | public function searchIndex($value) |
|
| 5739 | |||
| 5740 | /** |
||
| 5741 | * Search for the value of the current array via $index. |
||
| 5742 | * |
||
| 5743 | * EXAMPLE: <code> |
||
| 5744 | * a(['fòô' => 'bàř'])->searchValue('fòô'); // Arrayy[0 => 'bàř'] |
||
| 5745 | * </code> |
||
| 5746 | * |
||
| 5747 | * @param mixed $index |
||
| 5748 | * |
||
| 5749 | * @return static |
||
| 5750 | * <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p> |
||
| 5751 | * |
||
| 5752 | * @psalm-return static<TKey,T> |
||
| 5753 | * @psalm-mutation-free |
||
| 5754 | */ |
||
| 5755 | 9 | public function searchValue($index): self |
|
| 5785 | |||
| 5786 | /** |
||
| 5787 | * Set a value for the current array (optional using dot-notation). |
||
| 5788 | * |
||
| 5789 | * EXAMPLE: <code> |
||
| 5790 | * $arrayy = a(['Lars' => ['lastname' => 'Moelleken']]); |
||
| 5791 | * $arrayy->set('Lars.lastname', 'Müller'); // Arrayy['Lars', ['lastname' => 'Müller']]] |
||
| 5792 | * </code> |
||
| 5793 | * |
||
| 5794 | * @param string $key <p>The key to set.</p> |
||
| 5795 | * @param mixed $value <p>Its value.</p> |
||
| 5796 | * |
||
| 5797 | * @return $this |
||
| 5798 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5799 | * |
||
| 5800 | * @psalm-param TKey $key |
||
| 5801 | * @psalm-param T $value |
||
| 5802 | * @psalm-return static<TKey,T> |
||
| 5803 | */ |
||
| 5804 | 28 | public function set($key, $value): self |
|
| 5810 | |||
| 5811 | /** |
||
| 5812 | * Get a value from a array and set it if it was not. |
||
| 5813 | * |
||
| 5814 | * WARNING: this method only set the value, if the $key is not already set |
||
| 5815 | * |
||
| 5816 | * EXAMPLE: <code> |
||
| 5817 | * $arrayy = a([1 => 1, 2 => 2, 3 => 3]); |
||
| 5818 | * $arrayy->setAndGet(1, 4); // 1 |
||
| 5819 | * $arrayy->setAndGet(0, 4); // 4 |
||
| 5820 | * </code> |
||
| 5821 | * |
||
| 5822 | * @param mixed $key <p>The key</p> |
||
| 5823 | * @param mixed $fallback <p>The default value to set if it isn't.</p> |
||
| 5824 | * |
||
| 5825 | * @return mixed |
||
| 5826 | * <p>(Mutable)</p> |
||
| 5827 | */ |
||
| 5828 | 11 | public function setAndGet($key, $fallback = null) |
|
| 5839 | |||
| 5840 | /** |
||
| 5841 | * Shifts a specified value off the beginning of array. |
||
| 5842 | * |
||
| 5843 | * @return mixed |
||
| 5844 | * <p>(Mutable) A shifted element from the current array.</p> |
||
| 5845 | */ |
||
| 5846 | 5 | public function shift() |
|
| 5852 | |||
| 5853 | /** |
||
| 5854 | * Shuffle the current array. |
||
| 5855 | * |
||
| 5856 | * EXAMPLE: <code> |
||
| 5857 | * a([1 => 'bar', 'foo' => 'foo'])->shuffle(); // e.g.: Arrayy[['foo' => 'foo', 1 => 'bar']] |
||
| 5858 | * </code> |
||
| 5859 | * |
||
| 5860 | * @param bool $secure <p>using a CSPRNG | @link https://paragonie.com/b/JvICXzh_jhLyt4y3</p> |
||
| 5861 | * @param array $array [optional] |
||
| 5862 | * |
||
| 5863 | * @return static |
||
| 5864 | * <p>(Immutable)</p> |
||
| 5865 | * |
||
| 5866 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 5867 | * @psalm-return static<TKey,T> |
||
| 5868 | * |
||
| 5869 | * @noinspection BadExceptionsProcessingInspection |
||
| 5870 | * @noinspection NonSecureShuffleUsageInspection |
||
| 5871 | */ |
||
| 5872 | 2 | public function shuffle(bool $secure = false, array $array = null): self |
|
| 5873 | { |
||
| 5874 | 2 | if ($array === null) { |
|
| 5875 | 2 | $array = $this->toArray(false); |
|
| 5876 | } |
||
| 5877 | |||
| 5878 | 2 | if ($secure !== true) { |
|
| 5879 | 2 | \shuffle($array); |
|
| 5880 | } else { |
||
| 5881 | 1 | $size = \count($array, \COUNT_NORMAL); |
|
| 5882 | 1 | $keys = \array_keys($array); |
|
| 5883 | 1 | for ($i = $size - 1; $i > 0; --$i) { |
|
| 5884 | try { |
||
| 5885 | 1 | $r = \random_int(0, $i); |
|
| 5886 | } catch (\Exception $e) { |
||
| 5887 | $r = \mt_rand(0, $i); |
||
| 5888 | } |
||
| 5889 | 1 | if ($r !== $i) { |
|
| 5890 | 1 | $temp = $array[$keys[$r]]; |
|
| 5891 | 1 | $array[$keys[$r]] = $array[$keys[$i]]; |
|
| 5892 | 1 | $array[$keys[$i]] = $temp; |
|
| 5893 | } |
||
| 5894 | } |
||
| 5895 | } |
||
| 5896 | |||
| 5897 | 2 | foreach ($array as $key => $value) { |
|
| 5898 | // check if recursive is needed |
||
| 5899 | 2 | if (\is_array($value)) { |
|
| 5900 | 2 | $array[$key] = $this->shuffle($secure, $value); |
|
| 5901 | } |
||
| 5902 | } |
||
| 5903 | |||
| 5904 | 2 | return static::create( |
|
| 5905 | 2 | $array, |
|
| 5906 | 2 | $this->iteratorClass, |
|
| 5907 | 2 | false |
|
| 5908 | ); |
||
| 5909 | } |
||
| 5910 | |||
| 5911 | /** |
||
| 5912 | * Count the values from the current array. |
||
| 5913 | * |
||
| 5914 | * alias: for "Arrayy->count()" |
||
| 5915 | * |
||
| 5916 | * @param int $mode |
||
| 5917 | * |
||
| 5918 | * @return int |
||
| 5919 | */ |
||
| 5920 | 20 | public function size(int $mode = \COUNT_NORMAL): int |
|
| 5924 | |||
| 5925 | /** |
||
| 5926 | * Checks whether array has exactly $size items. |
||
| 5927 | * |
||
| 5928 | * @param int $size |
||
| 5929 | * |
||
| 5930 | * @return bool |
||
| 5931 | */ |
||
| 5932 | 1 | public function sizeIs(int $size): bool |
|
| 5948 | |||
| 5949 | /** |
||
| 5950 | * Checks whether array has between $fromSize to $toSize items. $toSize can be |
||
| 5951 | * smaller than $fromSize. |
||
| 5952 | * |
||
| 5953 | * @param int $fromSize |
||
| 5954 | * @param int $toSize |
||
| 5955 | * |
||
| 5956 | * @return bool |
||
| 5957 | */ |
||
| 5958 | 1 | public function sizeIsBetween(int $fromSize, int $toSize): bool |
|
| 5978 | |||
| 5979 | /** |
||
| 5980 | * Checks whether array has more than $size items. |
||
| 5981 | * |
||
| 5982 | * @param int $size |
||
| 5983 | * |
||
| 5984 | * @return bool |
||
| 5985 | */ |
||
| 5986 | 1 | View Code Duplication | public function sizeIsGreaterThan(int $size): bool |
| 6000 | |||
| 6001 | /** |
||
| 6002 | * Checks whether array has less than $size items. |
||
| 6003 | * |
||
| 6004 | * @param int $size |
||
| 6005 | * |
||
| 6006 | * @return bool |
||
| 6007 | */ |
||
| 6008 | 1 | View Code Duplication | public function sizeIsLessThan(int $size): bool |
| 6022 | |||
| 6023 | /** |
||
| 6024 | * Counts all elements in an array, or something in an object. |
||
| 6025 | * |
||
| 6026 | * <p> |
||
| 6027 | * For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}. |
||
| 6028 | * The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() |
||
| 6029 | * function. Please see the {@see Array} section of the manual for a detailed explanation of how arrays are |
||
| 6030 | * implemented and used in PHP. |
||
| 6031 | * </p> |
||
| 6032 | * |
||
| 6033 | * @return int |
||
| 6034 | * <p> |
||
| 6035 | * The number of elements in var, which is |
||
| 6036 | * typically an array, since anything else will have one |
||
| 6037 | * element. |
||
| 6038 | * </p> |
||
| 6039 | * <p> |
||
| 6040 | * If var is not an array or an object with |
||
| 6041 | * implemented Countable interface, |
||
| 6042 | * 1 will be returned. |
||
| 6043 | * There is one exception, if var is &null;, |
||
| 6044 | * 0 will be returned. |
||
| 6045 | * </p> |
||
| 6046 | * <p> |
||
| 6047 | * Caution: count may return 0 for a variable that isn't set, |
||
| 6048 | * but it may also return 0 for a variable that has been initialized with an |
||
| 6049 | * empty array. Use isset to test if a variable is set. |
||
| 6050 | * </p> |
||
| 6051 | */ |
||
| 6052 | 10 | public function sizeRecursive(): int |
|
| 6056 | |||
| 6057 | /** |
||
| 6058 | * Extract a slice of the array. |
||
| 6059 | * |
||
| 6060 | * @param int $offset <p>Slice begin index.</p> |
||
| 6061 | * @param int|null $length <p>Length of the slice.</p> |
||
| 6062 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 6063 | * |
||
| 6064 | * @return static |
||
| 6065 | * <p>(Immutable) A slice of the original array with length $length.</p> |
||
| 6066 | * |
||
| 6067 | * @psalm-return static<TKey,T> |
||
| 6068 | * @psalm-mutation-free |
||
| 6069 | */ |
||
| 6070 | 5 | public function slice(int $offset, int $length = null, bool $preserveKeys = false) |
|
| 6083 | |||
| 6084 | /** |
||
| 6085 | * Sort the current array and optional you can keep the keys. |
||
| 6086 | * |
||
| 6087 | * EXAMPLE: <code> |
||
| 6088 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sort(SORT_ASC, SORT_NATURAL, false); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6089 | * </code> |
||
| 6090 | * |
||
| 6091 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6092 | * @param int $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6093 | * <strong>SORT_NATURAL</strong></p> |
||
| 6094 | * @param bool $keepKeys |
||
| 6095 | * |
||
| 6096 | * @return static |
||
| 6097 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6098 | * |
||
| 6099 | * @psalm-return static<TKey,T> |
||
| 6100 | */ |
||
| 6101 | 20 | public function sort( |
|
| 6115 | |||
| 6116 | /** |
||
| 6117 | * Sort the current array and optional you can keep the keys. |
||
| 6118 | * |
||
| 6119 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6120 | * @param int $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6121 | * <strong>SORT_NATURAL</strong></p> |
||
| 6122 | * @param bool $keepKeys |
||
| 6123 | * |
||
| 6124 | * @return static |
||
| 6125 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6126 | * |
||
| 6127 | * @psalm-return static<TKey,T> |
||
| 6128 | */ |
||
| 6129 | 12 | public function sortImmutable( |
|
| 6145 | |||
| 6146 | /** |
||
| 6147 | * Sort the current array by key. |
||
| 6148 | * |
||
| 6149 | * EXAMPLE: <code> |
||
| 6150 | * a([1 => 2, 0 => 1])->sortKeys(\SORT_ASC); // Arrayy[0 => 1, 1 => 2] |
||
| 6151 | * </code> |
||
| 6152 | * |
||
| 6153 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6154 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6155 | * |
||
| 6156 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6157 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6158 | * <strong>SORT_NATURAL</strong></p> |
||
| 6159 | * |
||
| 6160 | * @return $this |
||
| 6161 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6162 | * |
||
| 6163 | * @psalm-return static<TKey,T> |
||
| 6164 | */ |
||
| 6165 | 18 | public function sortKeys( |
|
| 6175 | |||
| 6176 | /** |
||
| 6177 | * Sort the current array by key. |
||
| 6178 | * |
||
| 6179 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6180 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6181 | * |
||
| 6182 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6183 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6184 | * <strong>SORT_NATURAL</strong></p> |
||
| 6185 | * |
||
| 6186 | * @return $this |
||
| 6187 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6188 | * |
||
| 6189 | * @psalm-return static<TKey,T> |
||
| 6190 | * @psalm-mutation-free |
||
| 6191 | */ |
||
| 6192 | 8 | public function sortKeysImmutable( |
|
| 6205 | |||
| 6206 | /** |
||
| 6207 | * Sort the current array by value. |
||
| 6208 | * |
||
| 6209 | * EXAMPLE: <code> |
||
| 6210 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueKeepIndex(SORT_ASC, SORT_REGULAR); // Arrayy[0 => 'a', 3 => 'd', 2 => 'f'] |
||
| 6211 | * </code> |
||
| 6212 | * |
||
| 6213 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6214 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6215 | * <strong>SORT_NATURAL</strong></p> |
||
| 6216 | * |
||
| 6217 | * @return static |
||
| 6218 | * <p>(Mutable)</p> |
||
| 6219 | * |
||
| 6220 | * @psalm-return static<TKey,T> |
||
| 6221 | */ |
||
| 6222 | 1 | public function sortValueKeepIndex( |
|
| 6228 | |||
| 6229 | /** |
||
| 6230 | * Sort the current array by value. |
||
| 6231 | * |
||
| 6232 | * EXAMPLE: <code> |
||
| 6233 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueNewIndex(SORT_ASC, SORT_NATURAL); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6234 | * </code> |
||
| 6235 | * |
||
| 6236 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6237 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6238 | * <strong>SORT_NATURAL</strong></p> |
||
| 6239 | * |
||
| 6240 | * @return static |
||
| 6241 | * <p>(Mutable)</p> |
||
| 6242 | * |
||
| 6243 | * @psalm-return static<TKey,T> |
||
| 6244 | */ |
||
| 6245 | 1 | public function sortValueNewIndex($direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6249 | |||
| 6250 | /** |
||
| 6251 | * Sort a array by value or by a closure. |
||
| 6252 | * |
||
| 6253 | * - If the sorter is null, the array is sorted naturally. |
||
| 6254 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
| 6255 | * |
||
| 6256 | * EXAMPLE: <code> |
||
| 6257 | * $testArray = range(1, 5); |
||
| 6258 | * $under = a($testArray)->sorter( |
||
| 6259 | * function ($value) { |
||
| 6260 | * return $value % 2 === 0; |
||
| 6261 | * } |
||
| 6262 | * ); |
||
| 6263 | * var_dump($under); // Arrayy[1, 3, 5, 2, 4] |
||
| 6264 | * </code> |
||
| 6265 | * |
||
| 6266 | * @param callable|string|null $sorter |
||
| 6267 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or |
||
| 6268 | * <strong>SORT_DESC</strong></p> |
||
| 6269 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6270 | * <strong>SORT_NATURAL</strong></p> |
||
| 6271 | * |
||
| 6272 | * @return static |
||
| 6273 | * <p>(Immutable)</p> |
||
| 6274 | * |
||
| 6275 | * @psalm-return static<TKey,T> |
||
| 6276 | * @psalm-mutation-free |
||
| 6277 | */ |
||
| 6278 | 1 | public function sorter($sorter = null, $direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6279 | { |
||
| 6280 | 1 | $array = $this->toArray(); |
|
| 6281 | 1 | $direction = $this->getDirection($direction); |
|
| 6282 | |||
| 6283 | // Transform all values into their results. |
||
| 6284 | 1 | if ($sorter) { |
|
| 6285 | 1 | $arrayy = static::create( |
|
| 6286 | 1 | $array, |
|
| 6287 | 1 | $this->iteratorClass, |
|
| 6288 | 1 | false |
|
| 6289 | ); |
||
| 6290 | |||
| 6291 | /** |
||
| 6292 | * @psalm-suppress MissingClosureReturnType |
||
| 6293 | * @psalm-suppress MissingClosureParamType |
||
| 6294 | */ |
||
| 6295 | 1 | $results = $arrayy->each( |
|
| 6296 | 1 | static function ($value) use ($sorter) { |
|
| 6297 | 1 | if (\is_callable($sorter) === true) { |
|
| 6298 | 1 | return $sorter($value); |
|
| 6299 | } |
||
| 6300 | |||
| 6301 | 1 | return $sorter === $value; |
|
| 6302 | 1 | } |
|
| 6303 | ); |
||
| 6304 | |||
| 6305 | 1 | $results = $results->toArray(); |
|
| 6306 | } else { |
||
| 6307 | 1 | $results = $array; |
|
| 6308 | } |
||
| 6309 | |||
| 6310 | // Sort by the results and replace by original values |
||
| 6311 | 1 | \array_multisort($results, $direction, $strategy, $array); |
|
| 6312 | |||
| 6313 | 1 | return static::create( |
|
| 6314 | 1 | $array, |
|
| 6315 | 1 | $this->iteratorClass, |
|
| 6316 | 1 | false |
|
| 6317 | ); |
||
| 6318 | } |
||
| 6319 | |||
| 6320 | /** |
||
| 6321 | * @param int $offset |
||
| 6322 | * @param int|null $length |
||
| 6323 | * @param array $replacement |
||
| 6324 | * |
||
| 6325 | * @return static |
||
| 6326 | * <p>(Immutable)</p> |
||
| 6327 | * |
||
| 6328 | * @psalm-param array<mixed,mixed>|array<mixed,T> $replacement |
||
| 6329 | * @psalm-return static<TKey,T> |
||
| 6330 | * @psalm-mutation-free |
||
| 6331 | */ |
||
| 6332 | 1 | public function splice(int $offset, int $length = null, $replacement = []): self |
|
| 6349 | |||
| 6350 | /** |
||
| 6351 | * Split an array in the given amount of pieces. |
||
| 6352 | * |
||
| 6353 | * EXAMPLE: <code> |
||
| 6354 | * a(['a' => 1, 'b' => 2])->split(2, true); // Arrayy[['a' => 1], ['b' => 2]] |
||
| 6355 | * </code> |
||
| 6356 | * |
||
| 6357 | * @param int $numberOfPieces |
||
| 6358 | * @param bool $keepKeys |
||
| 6359 | * |
||
| 6360 | * @return static |
||
| 6361 | * <p>(Immutable)</p> |
||
| 6362 | * |
||
| 6363 | * @psalm-return static<TKey,T> |
||
| 6364 | * @psalm-mutation-free |
||
| 6365 | */ |
||
| 6366 | 1 | public function split(int $numberOfPieces = 2, bool $keepKeys = false): self |
|
| 6367 | { |
||
| 6368 | 1 | if ($keepKeys) { |
|
| 6369 | 1 | $generator = function () use ($numberOfPieces) { |
|
| 6370 | 1 | $carry = []; |
|
| 6371 | 1 | $i = 1; |
|
| 6372 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 6373 | 1 | $carry[$key] = $value; |
|
| 6374 | |||
| 6375 | 1 | if ($i % $numberOfPieces !== 0) { |
|
| 6376 | 1 | ++$i; |
|
| 6377 | |||
| 6378 | 1 | continue; |
|
| 6379 | } |
||
| 6380 | |||
| 6381 | 1 | yield $carry; |
|
| 6382 | |||
| 6383 | 1 | $carry = []; |
|
| 6384 | 1 | $i = 1; |
|
| 6385 | } |
||
| 6386 | |||
| 6387 | 1 | if ($carry !== []) { |
|
| 6388 | 1 | yield $carry; |
|
| 6389 | } |
||
| 6390 | 1 | }; |
|
| 6391 | View Code Duplication | } else { |
|
| 6392 | 1 | $generator = function () use ($numberOfPieces) { |
|
| 6393 | 1 | $carry = []; |
|
| 6394 | 1 | $i = 1; |
|
| 6395 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 6396 | 1 | $carry[] = $value; |
|
| 6397 | |||
| 6398 | 1 | if ($i % $numberOfPieces !== 0) { |
|
| 6399 | 1 | ++$i; |
|
| 6400 | |||
| 6401 | 1 | continue; |
|
| 6402 | } |
||
| 6403 | |||
| 6404 | 1 | yield $carry; |
|
| 6405 | |||
| 6406 | 1 | $carry = []; |
|
| 6407 | 1 | $i = 1; |
|
| 6408 | } |
||
| 6409 | |||
| 6410 | 1 | if ($carry !== []) { |
|
| 6411 | 1 | yield $carry; |
|
| 6412 | } |
||
| 6413 | 1 | }; |
|
| 6414 | } |
||
| 6415 | |||
| 6416 | 1 | return static::create( |
|
| 6417 | 1 | $generator, |
|
| 6418 | 1 | $this->iteratorClass, |
|
| 6419 | 1 | false |
|
| 6420 | ); |
||
| 6421 | } |
||
| 6422 | |||
| 6423 | /** |
||
| 6424 | * Strip all empty items from the current array. |
||
| 6425 | * |
||
| 6426 | * EXAMPLE: <code> |
||
| 6427 | * a(['a' => 1, 'b' => ''])->stripEmpty(); // Arrayy[['a' => 1]] |
||
| 6428 | * </code> |
||
| 6429 | * |
||
| 6430 | * @return static |
||
| 6431 | * <p>(Immutable)</p> |
||
| 6432 | * |
||
| 6433 | * @psalm-return static<TKey,T> |
||
| 6434 | * @psalm-mutation-free |
||
| 6435 | */ |
||
| 6436 | 1 | public function stripEmpty(): self |
|
| 6437 | { |
||
| 6438 | 1 | return $this->filter( |
|
| 6439 | 1 | static function ($item) { |
|
| 6440 | 1 | if ($item === null) { |
|
| 6441 | 1 | return false; |
|
| 6442 | } |
||
| 6443 | |||
| 6444 | 1 | return (bool) \trim((string) $item); |
|
| 6445 | 1 | } |
|
| 6446 | ); |
||
| 6447 | } |
||
| 6448 | |||
| 6449 | /** |
||
| 6450 | * Swap two values between positions by key. |
||
| 6451 | * |
||
| 6452 | * EXAMPLE: <code> |
||
| 6453 | * a(['a' => 1, 'b' => ''])->swap('a', 'b'); // Arrayy[['a' => '', 'b' => 1]] |
||
| 6454 | * </code> |
||
| 6455 | * |
||
| 6456 | * @param int|string $swapA <p>a key in the array</p> |
||
| 6457 | * @param int|string $swapB <p>a key in the array</p> |
||
| 6458 | * |
||
| 6459 | * @return static |
||
| 6460 | * <p>(Immutable)</p> |
||
| 6461 | * |
||
| 6462 | * @psalm-return static<TKey,T> |
||
| 6463 | * @psalm-mutation-free |
||
| 6464 | */ |
||
| 6465 | 1 | public function swap($swapA, $swapB): self |
|
| 6477 | |||
| 6478 | /** |
||
| 6479 | * Get the current array from the "Arrayy"-object. |
||
| 6480 | * alias for "getArray()" |
||
| 6481 | * |
||
| 6482 | * @param bool $convertAllArrayyElements <p> |
||
| 6483 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6484 | * </p> |
||
| 6485 | * @param bool $preserveKeys <p> |
||
| 6486 | * e.g.: A generator maybe return the same key more then once, |
||
| 6487 | * so maybe you will ignore the keys. |
||
| 6488 | * </p> |
||
| 6489 | * |
||
| 6490 | * @return array |
||
| 6491 | * |
||
| 6492 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 6493 | * @psalm-mutation-free |
||
| 6494 | */ |
||
| 6495 | 931 | public function toArray( |
|
| 6523 | |||
| 6524 | /** |
||
| 6525 | * Get the current array from the "Arrayy"-object as list. |
||
| 6526 | * |
||
| 6527 | * @param bool $convertAllArrayyElements <p> |
||
| 6528 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6529 | * </p> |
||
| 6530 | * |
||
| 6531 | * @return array |
||
| 6532 | * |
||
| 6533 | * @psalm-return list<array<TKey,T>> |
||
| 6534 | * @psalm-mutation-free |
||
| 6535 | */ |
||
| 6536 | 1 | public function toList(bool $convertAllArrayyElements = false): array |
|
| 6543 | |||
| 6544 | /** |
||
| 6545 | * Convert the current array to JSON. |
||
| 6546 | * |
||
| 6547 | * EXAMPLE: <code> |
||
| 6548 | * a(['bar', ['foo']])->toJson(); // '["bar",{"1":"foo"}]' |
||
| 6549 | * </code> |
||
| 6550 | * |
||
| 6551 | * @param int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p> |
||
| 6552 | * @param int $depth [optional] <p>Set the maximum depth. Must be greater than zero.</p> |
||
| 6553 | * |
||
| 6554 | * @return string |
||
| 6555 | */ |
||
| 6556 | 12 | public function toJson(int $options = 0, int $depth = 512): string |
|
| 6565 | |||
| 6566 | /** |
||
| 6567 | * @param string[]|null $items [optional] |
||
| 6568 | * @param string[] $helper [optional] |
||
| 6569 | * |
||
| 6570 | * @return static|static[] |
||
| 6571 | * |
||
| 6572 | * @psalm-return static<int, static<TKey,T>> |
||
| 6573 | */ |
||
| 6574 | 1 | public function toPermutation(array $items = null, array $helper = []): self |
|
| 6607 | |||
| 6608 | /** |
||
| 6609 | * Implodes array to a string with specified separator. |
||
| 6610 | * |
||
| 6611 | * @param string $separator [optional] <p>The element's separator.</p> |
||
| 6612 | * |
||
| 6613 | * @return string |
||
| 6614 | * <p>The string representation of array, separated by ",".</p> |
||
| 6615 | */ |
||
| 6616 | 19 | public function toString(string $separator = ','): string |
|
| 6620 | |||
| 6621 | /** |
||
| 6622 | * Return a duplicate free copy of the current array. |
||
| 6623 | * |
||
| 6624 | * EXAMPLE: <code> |
||
| 6625 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[1, 2] |
||
| 6626 | * </code> |
||
| 6627 | * |
||
| 6628 | * @return $this |
||
| 6629 | * <p>(Mutable)</p> |
||
| 6630 | * |
||
| 6631 | * @psalm-return static<TKey,T> |
||
| 6632 | */ |
||
| 6633 | 13 | public function uniqueNewIndex(): self |
|
| 6634 | { |
||
| 6635 | // INFO: \array_unique() can't handle e.g. "stdClass"-values in an array |
||
| 6636 | |||
| 6637 | /** |
||
| 6638 | * @psalm-suppress MissingClosureReturnType |
||
| 6639 | * @psalm-suppress MissingClosureParamType |
||
| 6640 | */ |
||
| 6641 | 13 | $this->array = $this->reduce( |
|
| 6642 | 13 | static function ($resultArray, $value) { |
|
| 6643 | 12 | if (!\in_array($value, $resultArray, true)) { |
|
| 6644 | 12 | $resultArray[] = $value; |
|
| 6645 | } |
||
| 6646 | |||
| 6647 | 12 | return $resultArray; |
|
| 6648 | 13 | }, |
|
| 6649 | 13 | [] |
|
| 6650 | 13 | )->toArray(); |
|
| 6651 | 13 | $this->generator = null; |
|
| 6652 | |||
| 6653 | 13 | return $this; |
|
| 6654 | } |
||
| 6655 | |||
| 6656 | /** |
||
| 6657 | * Return a duplicate free copy of the current array. (with the old keys) |
||
| 6658 | * |
||
| 6659 | * EXAMPLE: <code> |
||
| 6660 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[2 => 1, 3 => 2] |
||
| 6661 | * </code> |
||
| 6662 | * |
||
| 6663 | * @return $this |
||
| 6664 | * <p>(Mutable)</p> |
||
| 6665 | * |
||
| 6666 | * @psalm-return static<TKey,T> |
||
| 6667 | */ |
||
| 6668 | 11 | public function uniqueKeepIndex(): self |
|
| 6669 | { |
||
| 6670 | // INFO: \array_unique() can't handle e.g. "stdClass"-values in an array |
||
| 6671 | |||
| 6672 | // init |
||
| 6673 | 11 | $array = $this->toArray(); |
|
| 6674 | |||
| 6675 | /** |
||
| 6676 | * @psalm-suppress MissingClosureReturnType |
||
| 6677 | * @psalm-suppress MissingClosureParamType |
||
| 6678 | */ |
||
| 6679 | 11 | $this->array = \array_reduce( |
|
| 6680 | 11 | \array_keys($array), |
|
| 6681 | 11 | static function ($resultArray, $key) use ($array) { |
|
| 6682 | 10 | if (!\in_array($array[$key], $resultArray, true)) { |
|
| 6683 | 10 | $resultArray[$key] = $array[$key]; |
|
| 6684 | } |
||
| 6685 | |||
| 6686 | 10 | return $resultArray; |
|
| 6687 | 11 | }, |
|
| 6688 | 11 | [] |
|
| 6689 | ); |
||
| 6690 | 11 | $this->generator = null; |
|
| 6691 | |||
| 6692 | 11 | return $this; |
|
| 6693 | } |
||
| 6694 | |||
| 6695 | /** |
||
| 6696 | * alias: for "Arrayy->uniqueNewIndex()" |
||
| 6697 | * |
||
| 6698 | * @return static |
||
| 6699 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 6700 | * |
||
| 6701 | * @see Arrayy::unique() |
||
| 6702 | * |
||
| 6703 | * @psalm-return static<TKey,T> |
||
| 6704 | */ |
||
| 6705 | 13 | public function unique(): self |
|
| 6709 | |||
| 6710 | /** |
||
| 6711 | * Prepends one or more values to the beginning of array at once. |
||
| 6712 | * |
||
| 6713 | * @param array ...$args |
||
| 6714 | * |
||
| 6715 | * @return $this |
||
| 6716 | * <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p> |
||
| 6717 | * |
||
| 6718 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$args |
||
| 6719 | * @psalm-return static<TKey,T> |
||
| 6720 | */ |
||
| 6721 | 4 | public function unshift(...$args): self |
|
| 6729 | |||
| 6730 | /** |
||
| 6731 | * Tests whether the given closure return something valid for all elements of this array. |
||
| 6732 | * |
||
| 6733 | * @param \Closure $closure the predicate |
||
| 6734 | * |
||
| 6735 | * @return bool |
||
| 6736 | * <p>TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.</p> |
||
| 6737 | */ |
||
| 6738 | 1 | View Code Duplication | public function validate(\Closure $closure): bool |
| 6748 | |||
| 6749 | /** |
||
| 6750 | * Get all values from a array. |
||
| 6751 | * |
||
| 6752 | * EXAMPLE: <code> |
||
| 6753 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 6754 | * $arrayyTmp->values(); // Arrayy[0 => 'foo', 1 => 'foo2', 2 => 'bar'] |
||
| 6755 | * </code> |
||
| 6756 | * |
||
| 6757 | * @return static |
||
| 6758 | * <p>(Immutable)</p> |
||
| 6759 | * |
||
| 6760 | * @psalm-return static<TKey,T> |
||
| 6761 | * @psalm-mutation-free |
||
| 6762 | */ |
||
| 6763 | 2 | public function values(): self |
|
| 6764 | { |
||
| 6765 | 2 | return static::create( |
|
| 6766 | 2 | function () { |
|
| 6767 | /** @noinspection YieldFromCanBeUsedInspection */ |
||
| 6768 | 2 | foreach ($this->getGenerator() as $value) { |
|
| 6769 | 2 | yield $value; |
|
| 6770 | } |
||
| 6771 | 2 | }, |
|
| 6772 | 2 | $this->iteratorClass, |
|
| 6773 | 2 | false |
|
| 6774 | ); |
||
| 6775 | } |
||
| 6776 | |||
| 6777 | /** |
||
| 6778 | * Apply the given function to every element in the array, discarding the results. |
||
| 6779 | * |
||
| 6780 | * EXAMPLE: <code> |
||
| 6781 | * $callable = function (&$value, $key) { |
||
| 6782 | * $value = $key; |
||
| 6783 | * }; |
||
| 6784 | * $arrayy = a([1, 2, 3]); |
||
| 6785 | * $arrayy->walk($callable); // Arrayy[0, 1, 2] |
||
| 6786 | * </code> |
||
| 6787 | * |
||
| 6788 | * @param callable $callable |
||
| 6789 | * @param bool $recursive [optional] <p>Whether array will be walked recursively or no</p> |
||
| 6790 | * @param mixed $userData [optional] <p> |
||
| 6791 | * If the optional $userData parameter is supplied, |
||
| 6792 | * it will be passed as the third parameter to the $callable. |
||
| 6793 | * </p> |
||
| 6794 | * |
||
| 6795 | * @return $this |
||
| 6796 | * <p>(Mutable) Return this Arrayy object, with modified elements.</p> |
||
| 6797 | * |
||
| 6798 | * @psalm-return static<TKey,T> |
||
| 6799 | */ |
||
| 6800 | 12 | public function walk( |
|
| 6826 | |||
| 6827 | /** |
||
| 6828 | * Returns a collection of matching items. |
||
| 6829 | * |
||
| 6830 | * @param string $keyOrPropertyOrMethod the property or method to evaluate |
||
| 6831 | * @param mixed $value the value to match |
||
| 6832 | * |
||
| 6833 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 6834 | * |
||
| 6835 | * @return static |
||
| 6836 | * |
||
| 6837 | * @psalm-param T $value |
||
| 6838 | * @psalm-return static<TKey,T> |
||
| 6839 | */ |
||
| 6840 | 1 | public function where(string $keyOrPropertyOrMethod, $value): self |
|
| 6841 | { |
||
| 6842 | 1 | return $this->filter( |
|
| 6843 | 1 | function ($item) use ($keyOrPropertyOrMethod, $value) { |
|
| 6844 | $accessorValue = $this->extractValue( |
||
| 6845 | $item, |
||
| 6846 | $keyOrPropertyOrMethod |
||
| 6847 | ); |
||
| 6848 | |||
| 6849 | return $accessorValue === $value; |
||
| 6850 | 1 | } |
|
| 6851 | ); |
||
| 6852 | } |
||
| 6853 | |||
| 6854 | /** |
||
| 6855 | * Convert an array into a object. |
||
| 6856 | * |
||
| 6857 | * @param array $array |
||
| 6858 | * |
||
| 6859 | * @return \stdClass |
||
| 6860 | * |
||
| 6861 | * @psalm-param array<mixed,mixed> $array |
||
| 6862 | */ |
||
| 6863 | 4 | final protected static function arrayToObject(array $array = []): \stdClass |
|
| 6882 | |||
| 6883 | /** |
||
| 6884 | * @param array|\Generator|null $input <p> |
||
| 6885 | * An array containing keys to return. |
||
| 6886 | * </p> |
||
| 6887 | * @param mixed|null $search_values [optional] <p> |
||
| 6888 | * If specified, then only keys containing these values are returned. |
||
| 6889 | * </p> |
||
| 6890 | * @param bool $strict [optional] <p> |
||
| 6891 | * Determines if strict comparison (===) should be used during the |
||
| 6892 | * search. |
||
| 6893 | * </p> |
||
| 6894 | * |
||
| 6895 | * @return array |
||
| 6896 | * <p>an array of all the keys in input</p> |
||
| 6897 | * |
||
| 6898 | * @psalm-param array<mixed,mixed>|\Generator<TKey,T>|null $input |
||
| 6899 | * @psalm-return array<TKey|mixed> |
||
| 6900 | * @psalm-mutation-free |
||
| 6901 | */ |
||
| 6902 | 11 | protected function array_keys_recursive( |
|
| 6963 | |||
| 6964 | /** |
||
| 6965 | * @param mixed $path |
||
| 6966 | * @param callable $callable |
||
| 6967 | * @param array|null $currentOffset |
||
| 6968 | * |
||
| 6969 | * @return void |
||
| 6970 | * |
||
| 6971 | * @psalm-param array<mixed,mixed>|array<TKey,T>|null $currentOffset |
||
| 6972 | * @psalm-mutation-free |
||
| 6973 | */ |
||
| 6974 | 10 | protected function callAtPath($path, $callable, &$currentOffset = null) |
|
| 7003 | |||
| 7004 | /** |
||
| 7005 | * Extracts the value of the given property or method from the object. |
||
| 7006 | * |
||
| 7007 | * @param static $object <p>The object to extract the value from.</p> |
||
| 7008 | * @param string $keyOrPropertyOrMethod <p>The property or method for which the |
||
| 7009 | * value should be extracted.</p> |
||
| 7010 | * |
||
| 7011 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 7012 | * |
||
| 7013 | * @return mixed |
||
| 7014 | * <p>The value extracted from the specified property or method.</p> |
||
| 7015 | * |
||
| 7016 | * @psalm-param self<TKey,T> $object |
||
| 7017 | */ |
||
| 7018 | 1 | final protected function extractValue(self $object, string $keyOrPropertyOrMethod) |
|
| 7040 | |||
| 7041 | /** |
||
| 7042 | * create a fallback for array |
||
| 7043 | * |
||
| 7044 | * 1. use the current array, if it's a array |
||
| 7045 | * 2. fallback to empty array, if there is nothing |
||
| 7046 | * 3. call "getArray()" on object, if there is a "Arrayy"-object |
||
| 7047 | * 4. call "createFromObject()" on object, if there is a "\Traversable"-object |
||
| 7048 | * 5. call "__toArray()" on object, if the method exists |
||
| 7049 | * 6. cast a string or object with "__toString()" into an array |
||
| 7050 | * 7. throw a "InvalidArgumentException"-Exception |
||
| 7051 | * |
||
| 7052 | * @param mixed $data |
||
| 7053 | * |
||
| 7054 | * @throws \InvalidArgumentException |
||
| 7055 | * |
||
| 7056 | * @return array |
||
| 7057 | * |
||
| 7058 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 7059 | */ |
||
| 7060 | 1206 | protected function fallbackForArray(&$data): array |
|
| 7070 | |||
| 7071 | /** |
||
| 7072 | * @param bool $preserveKeys <p> |
||
| 7073 | * e.g.: A generator maybe return the same key more then once, |
||
| 7074 | * so maybe you will ignore the keys. |
||
| 7075 | * </p> |
||
| 7076 | * |
||
| 7077 | * @return bool |
||
| 7078 | * |
||
| 7079 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7080 | * @psalm-mutation-free :/ |
||
| 7081 | */ |
||
| 7082 | 1118 | protected function generatorToArray(bool $preserveKeys = true) |
|
| 7093 | |||
| 7094 | /** |
||
| 7095 | * Get correct PHP constant for direction. |
||
| 7096 | * |
||
| 7097 | * @param int|string $direction |
||
| 7098 | * |
||
| 7099 | * @return int |
||
| 7100 | * @psalm-mutation-free |
||
| 7101 | */ |
||
| 7102 | 43 | protected function getDirection($direction): int |
|
| 7124 | |||
| 7125 | /** |
||
| 7126 | * @return TypeCheckInterface[] |
||
| 7127 | * |
||
| 7128 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7129 | */ |
||
| 7130 | 24 | protected function getPropertiesFromPhpDoc() |
|
| 7185 | |||
| 7186 | /** |
||
| 7187 | * @param mixed $glue |
||
| 7188 | * @param mixed $pieces |
||
| 7189 | * @param bool $useKeys |
||
| 7190 | * |
||
| 7191 | * @return string |
||
| 7192 | * @psalm-mutation-free |
||
| 7193 | */ |
||
| 7194 | 36 | protected function implode_recursive( |
|
| 7227 | |||
| 7228 | /** |
||
| 7229 | * @param mixed $needle <p> |
||
| 7230 | * The searched value. |
||
| 7231 | * </p> |
||
| 7232 | * <p> |
||
| 7233 | * If needle is a string, the comparison is done |
||
| 7234 | * in a case-sensitive manner. |
||
| 7235 | * </p> |
||
| 7236 | * @param array|\Generator|null $haystack <p> |
||
| 7237 | * The array. |
||
| 7238 | * </p> |
||
| 7239 | * @param bool $strict [optional] <p> |
||
| 7240 | * If the third parameter strict is set to true |
||
| 7241 | * then the in_array function will also check the |
||
| 7242 | * types of the |
||
| 7243 | * needle in the haystack. |
||
| 7244 | * </p> |
||
| 7245 | * |
||
| 7246 | * @return bool |
||
| 7247 | * <p>true if needle is found in the array, false otherwise</p> |
||
| 7248 | * |
||
| 7249 | * @psalm-param array<mixed,mixed>|\Generator<TKey,T>|null $haystack |
||
| 7250 | * @psalm-mutation-free |
||
| 7251 | */ |
||
| 7252 | 18 | protected function in_array_recursive($needle, $haystack = null, $strict = true): bool |
|
| 7277 | |||
| 7278 | /** |
||
| 7279 | * @param mixed $data |
||
| 7280 | * |
||
| 7281 | * @return array|null |
||
| 7282 | * |
||
| 7283 | * @psalm-return array<mixed,mixed>|array<TKey,T>|null |
||
| 7284 | */ |
||
| 7285 | 1206 | protected function internalGetArray(&$data) |
|
| 7336 | |||
| 7337 | /** |
||
| 7338 | * Internal mechanics of remove method. |
||
| 7339 | * |
||
| 7340 | * @param mixed $key |
||
| 7341 | * |
||
| 7342 | * @return bool |
||
| 7343 | */ |
||
| 7344 | 22 | protected function internalRemove($key): bool |
|
| 7377 | |||
| 7378 | /** |
||
| 7379 | * Internal mechanic of set method. |
||
| 7380 | * |
||
| 7381 | * @param int|string|null $key |
||
| 7382 | * @param mixed $value |
||
| 7383 | * @param bool $checkProperties |
||
| 7384 | * |
||
| 7385 | * @return bool |
||
| 7386 | */ |
||
| 7387 | 1056 | protected function internalSet( |
|
| 7446 | |||
| 7447 | /** |
||
| 7448 | * Convert a object into an array. |
||
| 7449 | * |
||
| 7450 | * @param mixed|object $object |
||
| 7451 | * |
||
| 7452 | * @return array|mixed |
||
| 7453 | * |
||
| 7454 | * @psalm-mutation-free |
||
| 7455 | */ |
||
| 7456 | 5 | protected static function objectToArray($object) |
|
| 7469 | |||
| 7470 | /** |
||
| 7471 | * @param array $data |
||
| 7472 | * @param bool $checkPropertiesInConstructor |
||
| 7473 | * |
||
| 7474 | * @return void |
||
| 7475 | * |
||
| 7476 | * @psalm-param array<mixed,T> $data |
||
| 7477 | */ |
||
| 7478 | 1204 | protected function setInitialValuesAndProperties(array &$data, bool $checkPropertiesInConstructor) |
|
| 7523 | |||
| 7524 | /** |
||
| 7525 | * sorting keys |
||
| 7526 | * |
||
| 7527 | * @param array $elements |
||
| 7528 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7529 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7530 | * <strong>SORT_NATURAL</strong></p> |
||
| 7531 | * |
||
| 7532 | * @return $this |
||
| 7533 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7534 | * |
||
| 7535 | * @psalm-param array<mixed,mixed>|array<mixed|TKey,T> $elements |
||
| 7536 | * @psalm-return static<TKey,T> |
||
| 7537 | */ |
||
| 7538 | 18 | protected function sorterKeys( |
|
| 7559 | |||
| 7560 | /** |
||
| 7561 | * @param array $elements <p>Warning: used as reference</p> |
||
| 7562 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7563 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7564 | * <strong>SORT_NATURAL</strong></p> |
||
| 7565 | * @param bool $keepKeys |
||
| 7566 | * |
||
| 7567 | * @return $this |
||
| 7568 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7569 | * |
||
| 7570 | * @psalm-param array<mixed,mixed>|array<mixed|TKey,T> $elements |
||
| 7571 | * @psalm-return static<TKey,T> |
||
| 7572 | */ |
||
| 7573 | 24 | protected function sorting( |
|
| 7607 | |||
| 7608 | /** |
||
| 7609 | * @param array $array |
||
| 7610 | * |
||
| 7611 | * @return array |
||
| 7612 | * |
||
| 7613 | * @psalm-mutation-free |
||
| 7614 | */ |
||
| 7615 | 25 | private function getArrayRecursiveHelperArrayy(array $array) |
|
| 7616 | { |
||
| 7617 | 25 | if ($array === []) { |
|
| 7618 | return []; |
||
| 7619 | } |
||
| 7620 | |||
| 7621 | 25 | \array_walk_recursive( |
|
| 7622 | 25 | $array, |
|
| 7623 | /** |
||
| 7624 | * @param array|self $item |
||
| 7625 | * |
||
| 7626 | * @return void |
||
| 7627 | */ |
||
| 7628 | 25 | static function (&$item) { |
|
| 7629 | 25 | if ($item instanceof self) { |
|
| 7630 | 1 | $item = $item->getArray(); |
|
| 7631 | } |
||
| 7632 | 25 | } |
|
| 7633 | ); |
||
| 7634 | |||
| 7635 | 25 | return $array; |
|
| 7636 | } |
||
| 7637 | |||
| 7638 | /** |
||
| 7639 | * @param int|string|null $key |
||
| 7640 | * @param mixed $value |
||
| 7641 | * |
||
| 7642 | * @return void |
||
| 7643 | */ |
||
| 7644 | 112 | private function checkType($key, $value) |
|
| 7662 | } |
||
| 7663 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..