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 | 1212 | public function __construct( |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 52 | 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 | 133 | 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 | 1 | 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|mixed|static $data |
||
| 442 | * |
||
| 443 | * 1. use the current array, if it's a array |
||
| 444 | * 2. fallback to empty array, if there is nothing |
||
| 445 | * 3. call "getArray()" on object, if there is a "Arrayy"-object |
||
| 446 | * 4. call "createFromObject()" on object, if there is a "\Traversable"-object |
||
| 447 | * 5. call "__toArray()" on object, if the method exists |
||
| 448 | * 6. cast a string or object with "__toString()" into an array |
||
| 449 | * 7. throw a "InvalidArgumentException"-Exception |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | * |
||
| 453 | * @psalm-param T,array<TKey,T>|self<TKey,T> $data |
||
| 454 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 455 | */ |
||
| 456 | 1 | public function exchangeArray($data): array |
|
| 457 | { |
||
| 458 | 1 | $this->array = $this->fallbackForArray($data); |
|
| 459 | 1 | $this->generator = null; |
|
| 460 | |||
| 461 | 1 | return $this->array; |
|
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Creates a copy of the ArrayyObject. |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | * |
||
| 469 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 470 | */ |
||
| 471 | 6 | public function getArrayCopy(): array |
|
| 472 | { |
||
| 473 | 6 | $this->generatorToArray(); |
|
| 474 | |||
| 475 | 6 | return $this->array; |
|
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns a new iterator, thus implementing the \Iterator interface. |
||
| 480 | * |
||
| 481 | * EXAMPLE: <code> |
||
| 482 | * a(['foo', 'bar'])->getIterator(); // ArrayyIterator['foo', 'bar'] |
||
| 483 | * </code> |
||
| 484 | * |
||
| 485 | * @return \Iterator<mixed, mixed> |
||
| 486 | * <p>An iterator for the values in the array.</p> |
||
| 487 | * @psalm-return \Iterator<array-key|TKey, mixed|T> |
||
| 488 | */ |
||
| 489 | 28 | public function getIterator(): \Iterator |
|
| 490 | { |
||
| 491 | 28 | if ($this->generator instanceof ArrayyRewindableGenerator) { |
|
| 492 | 1 | $generator = clone $this->generator; |
|
| 493 | 1 | $this->generator = new ArrayyRewindableExtendedGenerator( |
|
| 494 | static function () use ($generator): \Generator { |
||
| 495 | 1 | yield from $generator; |
|
| 496 | 1 | }, |
|
| 497 | 1 | null, |
|
| 498 | 1 | static::class |
|
| 499 | ); |
||
| 500 | |||
| 501 | 1 | return $this->generator; |
|
| 502 | } |
||
| 503 | |||
| 504 | 27 | $iterator = $this->getIteratorClass(); |
|
| 505 | |||
| 506 | 27 | if ($iterator === ArrayyIterator::class) { |
|
| 507 | 27 | return new $iterator($this->toArray(), 0, static::class); |
|
| 508 | } |
||
| 509 | |||
| 510 | $return = new $iterator($this->toArray()); |
||
| 511 | \assert($return instanceof \Iterator); |
||
| 512 | |||
| 513 | return $return; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Gets the iterator classname for the ArrayObject. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | * |
||
| 521 | * @psalm-return class-string |
||
| 522 | */ |
||
| 523 | 27 | public function getIteratorClass(): string |
|
| 524 | { |
||
| 525 | 27 | return $this->iteratorClass; |
|
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Sort the entries by key. |
||
| 530 | * |
||
| 531 | * @param int $sort_flags [optional] <p> |
||
| 532 | * You may modify the behavior of the sort using the optional |
||
| 533 | * parameter sort_flags, for details |
||
| 534 | * see sort. |
||
| 535 | * </p> |
||
| 536 | * |
||
| 537 | * @return $this |
||
| 538 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 539 | * |
||
| 540 | * @psalm-return static<TKey,T> |
||
| 541 | */ |
||
| 542 | 4 | public function ksort(int $sort_flags = 0): self |
|
| 543 | { |
||
| 544 | 4 | $this->generatorToArray(); |
|
| 545 | |||
| 546 | 4 | \ksort($this->array, $sort_flags); |
|
| 547 | |||
| 548 | 4 | return $this; |
|
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Sort the entries by key. |
||
| 553 | * |
||
| 554 | * @param int $sort_flags [optional] <p> |
||
| 555 | * You may modify the behavior of the sort using the optional |
||
| 556 | * parameter sort_flags, for details |
||
| 557 | * see sort. |
||
| 558 | * </p> |
||
| 559 | * |
||
| 560 | * @return $this |
||
| 561 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 562 | * |
||
| 563 | * @psalm-return static<TKey,T> |
||
| 564 | */ |
||
| 565 | 4 | public function ksortImmutable(int $sort_flags = 0): self |
|
| 566 | { |
||
| 567 | 4 | $that = clone $this; |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @psalm-suppress ImpureMethodCall - object is already cloned |
||
| 571 | */ |
||
| 572 | 4 | $that->ksort($sort_flags); |
|
| 573 | |||
| 574 | 4 | return $that; |
|
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 579 | * |
||
| 580 | * @return $this |
||
| 581 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 582 | * |
||
| 583 | * @psalm-return static<TKey,T> |
||
| 584 | */ |
||
| 585 | 8 | public function natcasesort(): self |
|
| 586 | { |
||
| 587 | 8 | $this->generatorToArray(); |
|
| 588 | |||
| 589 | 8 | \natcasesort($this->array); |
|
| 590 | |||
| 591 | 8 | return $this; |
|
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 596 | * |
||
| 597 | * @return $this |
||
| 598 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 599 | * |
||
| 600 | * @psalm-return static<TKey,T> |
||
| 601 | * @psalm-mutation-free |
||
| 602 | */ |
||
| 603 | 4 | public function natcasesortImmutable(): self |
|
| 604 | { |
||
| 605 | 4 | $that = clone $this; |
|
| 606 | |||
| 607 | /** |
||
| 608 | * @psalm-suppress ImpureMethodCall - object is already cloned |
||
| 609 | */ |
||
| 610 | 4 | $that->natcasesort(); |
|
| 611 | |||
| 612 | 4 | return $that; |
|
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Sort entries using a "natural order" algorithm. |
||
| 617 | * |
||
| 618 | * @return $this |
||
| 619 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 620 | * |
||
| 621 | * @psalm-return static<TKey,T> |
||
| 622 | */ |
||
| 623 | 10 | public function natsort(): self |
|
| 624 | { |
||
| 625 | 10 | $this->generatorToArray(); |
|
| 626 | |||
| 627 | 10 | \natsort($this->array); |
|
| 628 | |||
| 629 | 10 | return $this; |
|
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Sort entries using a "natural order" algorithm. |
||
| 634 | * |
||
| 635 | * @return $this |
||
| 636 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 637 | * |
||
| 638 | * @psalm-return static<TKey,T> |
||
| 639 | * @psalm-mutation-free |
||
| 640 | */ |
||
| 641 | 4 | public function natsortImmutable(): self |
|
| 642 | { |
||
| 643 | 4 | $that = clone $this; |
|
| 644 | |||
| 645 | /** |
||
| 646 | * @psalm-suppress ImpureMethodCall - object is already cloned |
||
| 647 | */ |
||
| 648 | 4 | $that->natsort(); |
|
| 649 | |||
| 650 | 4 | return $that; |
|
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Whether or not an offset exists. |
||
| 655 | * |
||
| 656 | * @param bool|int|string $offset |
||
| 657 | * |
||
| 658 | * @return bool |
||
| 659 | * |
||
| 660 | * @noinspection PhpSillyAssignmentInspection |
||
| 661 | * |
||
| 662 | * @psalm-mutation-free |
||
| 663 | */ |
||
| 664 | 164 | public function offsetExists($offset): bool |
|
| 665 | { |
||
| 666 | // php cast "bool"-index into "int"-index |
||
| 667 | 164 | if ((bool) $offset === $offset) { |
|
| 668 | 1 | $offset = (int) $offset; |
|
| 669 | } |
||
| 670 | 164 | \assert(\is_int($offset) || \is_string($offset)); |
|
| 671 | |||
| 672 | 164 | $offsetExists = $this->keyExists($offset); |
|
| 673 | 164 | if ($offsetExists === true) { |
|
| 674 | 143 | return true; |
|
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 679 | * |
||
| 680 | * @psalm-suppress PossiblyInvalidArgument |
||
| 681 | * @psalm-suppress InvalidScalarArgument |
||
| 682 | */ |
||
| 683 | View Code Duplication | if ( |
|
| 684 | 124 | $this->pathSeparator |
|
| 685 | && |
||
| 686 | 124 | (string) $offset === $offset |
|
| 687 | && |
||
| 688 | 124 | \strpos($offset, $this->pathSeparator) !== false |
|
| 689 | ) { |
||
| 690 | 4 | $explodedPath = \explode($this->pathSeparator, (string) $offset); |
|
| 691 | 4 | if ($explodedPath !== false) { |
|
| 692 | /** @var string $lastOffset - helper for phpstan */ |
||
| 693 | 4 | $lastOffset = \array_pop($explodedPath); |
|
| 694 | 4 | $containerPath = \implode($this->pathSeparator, $explodedPath); |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @psalm-suppress MissingClosureReturnType |
||
| 698 | * @psalm-suppress MissingClosureParamType |
||
| 699 | */ |
||
| 700 | 4 | $this->callAtPath( |
|
| 701 | 4 | $containerPath, |
|
| 702 | static function ($container) use ($lastOffset, &$offsetExists) { |
||
| 703 | 4 | $offsetExists = \array_key_exists($lastOffset, $container); |
|
| 704 | 4 | } |
|
| 705 | ); |
||
| 706 | } |
||
| 707 | } |
||
| 708 | |||
| 709 | 124 | return $offsetExists; |
|
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Returns the value at specified offset. |
||
| 714 | * |
||
| 715 | * @param int|string $offset |
||
| 716 | * |
||
| 717 | * @return mixed |
||
| 718 | * <p>Will return null if the offset did not exists.</p> |
||
| 719 | */ |
||
| 720 | 133 | public function &offsetGet($offset) |
|
| 721 | { |
||
| 722 | // init |
||
| 723 | 133 | $value = null; |
|
| 724 | |||
| 725 | 133 | if ($this->offsetExists($offset)) { |
|
| 726 | 131 | $value = &$this->__get($offset); |
|
| 727 | } |
||
| 728 | |||
| 729 | 133 | return $value; |
|
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Assigns a value to the specified offset + check the type. |
||
| 734 | * |
||
| 735 | * @param int|string|null $offset |
||
| 736 | * @param mixed $value |
||
| 737 | * |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | 28 | public function offsetSet($offset, $value) |
|
| 741 | { |
||
| 742 | 28 | $this->generatorToArray(); |
|
| 743 | |||
| 744 | 28 | if ($offset === null) { |
|
| 745 | 7 | if ($this->properties !== []) { |
|
| 746 | 2 | $this->checkType(null, $value); |
|
| 747 | } |
||
| 748 | |||
| 749 | 6 | $this->array[] = $value; |
|
| 750 | } else { |
||
| 751 | 21 | $this->internalSet( |
|
| 752 | 21 | $offset, |
|
| 753 | 21 | $value, |
|
| 754 | 21 | true |
|
| 755 | ); |
||
| 756 | } |
||
| 757 | 27 | } |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Unset an offset. |
||
| 761 | * |
||
| 762 | * @param int|string $offset |
||
| 763 | * |
||
| 764 | * @return void |
||
| 765 | * <p>(Mutable) Return nothing.</p> |
||
| 766 | */ |
||
| 767 | 26 | public function offsetUnset($offset) |
|
| 768 | { |
||
| 769 | 26 | $this->generatorToArray(); |
|
| 770 | |||
| 771 | 26 | if ($this->array === []) { |
|
| 772 | 6 | return; |
|
| 773 | } |
||
| 774 | |||
| 775 | 21 | if ($this->keyExists($offset)) { |
|
| 776 | 14 | unset($this->array[$offset]); |
|
| 777 | |||
| 778 | 14 | return; |
|
| 779 | } |
||
| 780 | |||
| 781 | /** |
||
| 782 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 783 | * |
||
| 784 | * @psalm-suppress PossiblyInvalidArgument |
||
| 785 | * @psalm-suppress InvalidScalarArgument |
||
| 786 | */ |
||
| 787 | View Code Duplication | if ( |
|
| 788 | 10 | $this->pathSeparator |
|
| 789 | && |
||
| 790 | 10 | (string) $offset === $offset |
|
| 791 | && |
||
| 792 | 10 | \strpos($offset, $this->pathSeparator) !== false |
|
| 793 | ) { |
||
| 794 | 7 | $path = \explode($this->pathSeparator, (string) $offset); |
|
| 795 | |||
| 796 | 7 | if ($path !== false) { |
|
| 797 | 7 | $pathToUnset = \array_pop($path); |
|
| 798 | |||
| 799 | /** |
||
| 800 | * @psalm-suppress MissingClosureReturnType |
||
| 801 | * @psalm-suppress MissingClosureParamType |
||
| 802 | */ |
||
| 803 | 7 | $this->callAtPath( |
|
| 804 | 7 | \implode($this->pathSeparator, $path), |
|
| 805 | static function (&$offset) use ($pathToUnset) { |
||
| 806 | 6 | if (\is_array($offset)) { |
|
| 807 | 5 | unset($offset[$pathToUnset]); |
|
| 808 | } else { |
||
| 809 | 1 | $offset = null; |
|
| 810 | } |
||
| 811 | 7 | } |
|
| 812 | ); |
||
| 813 | } |
||
| 814 | } |
||
| 815 | |||
| 816 | 10 | unset($this->array[$offset]); |
|
| 817 | 10 | } |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Serialize the current "Arrayy"-object. |
||
| 821 | * |
||
| 822 | * EXAMPLE: <code> |
||
| 823 | * a([1, 4, 7])->serialize(); |
||
| 824 | * </code> |
||
| 825 | * |
||
| 826 | * @return string |
||
| 827 | */ |
||
| 828 | 2 | public function serialize(): string |
|
| 829 | { |
||
| 830 | 2 | $this->generatorToArray(); |
|
| 831 | |||
| 832 | 2 | if (\PHP_VERSION_ID < 70400) { |
|
| 833 | 2 | return parent::serialize(); |
|
| 834 | } |
||
| 835 | |||
| 836 | return \serialize($this); |
||
| 837 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Sets the iterator classname for the current "Arrayy"-object. |
||
| 841 | * |
||
| 842 | * @param string $iteratorClass |
||
| 843 | * |
||
| 844 | * @throws \InvalidArgumentException |
||
| 845 | * |
||
| 846 | * @return void |
||
| 847 | * |
||
| 848 | * @psalm-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 849 | */ |
||
| 850 | 1202 | public function setIteratorClass($iteratorClass) |
|
| 851 | { |
||
| 852 | 1202 | if (\class_exists($iteratorClass)) { |
|
| 853 | 1202 | $this->iteratorClass = $iteratorClass; |
|
| 854 | |||
| 855 | 1202 | return; |
|
| 856 | } |
||
| 857 | |||
| 858 | if (\strpos($iteratorClass, '\\') === 0) { |
||
| 859 | $iteratorClass = '\\' . $iteratorClass; |
||
| 860 | if (\class_exists($iteratorClass)) { |
||
| 861 | /** |
||
| 862 | * @psalm-suppress PropertyTypeCoercion |
||
| 863 | */ |
||
| 864 | $this->iteratorClass = $iteratorClass; |
||
| 865 | |||
| 866 | return; |
||
| 867 | } |
||
| 868 | } |
||
| 869 | |||
| 870 | throw new \InvalidArgumentException('The iterator class does not exist: ' . $iteratorClass); |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 875 | * |
||
| 876 | * @param callable $function |
||
| 877 | * |
||
| 878 | * @throws \InvalidArgumentException |
||
| 879 | * |
||
| 880 | * @return $this |
||
| 881 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 882 | * |
||
| 883 | * @psalm-return static<TKey,T> |
||
| 884 | */ |
||
| 885 | 8 | View Code Duplication | public function uasort($function): self |
| 886 | { |
||
| 887 | 8 | if (!\is_callable($function)) { |
|
| 888 | throw new \InvalidArgumentException('Passed function must be callable'); |
||
| 889 | } |
||
| 890 | |||
| 891 | 8 | $this->generatorToArray(); |
|
| 892 | |||
| 893 | 8 | \uasort($this->array, $function); |
|
| 894 | |||
| 895 | 8 | return $this; |
|
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 900 | * |
||
| 901 | * @param callable $function |
||
| 902 | * |
||
| 903 | * @throws \InvalidArgumentException |
||
| 904 | * |
||
| 905 | * @return $this |
||
| 906 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 907 | * |
||
| 908 | * @psalm-return static<TKey,T> |
||
| 909 | * @psalm-mutation-free |
||
| 910 | */ |
||
| 911 | 4 | public function uasortImmutable($function): self |
|
| 912 | { |
||
| 913 | 4 | $that = clone $this; |
|
| 914 | |||
| 915 | /** |
||
| 916 | * @psalm-suppress ImpureMethodCall - object is already cloned |
||
| 917 | */ |
||
| 918 | 4 | $that->uasort($function); |
|
| 919 | |||
| 920 | 4 | return $that; |
|
| 921 | } |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Sort the entries by keys using a user-defined comparison function. |
||
| 925 | * |
||
| 926 | * @param callable $function |
||
| 927 | * |
||
| 928 | * @throws \InvalidArgumentException |
||
| 929 | * |
||
| 930 | * @return static |
||
| 931 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 932 | * |
||
| 933 | * @psalm-return static<TKey,T> |
||
| 934 | */ |
||
| 935 | 5 | public function uksort($function): self |
|
| 936 | { |
||
| 937 | 5 | return $this->customSortKeys($function); |
|
| 938 | } |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Sort the entries by keys using a user-defined comparison function. |
||
| 942 | * |
||
| 943 | * @param callable $function |
||
| 944 | * |
||
| 945 | * @throws \InvalidArgumentException |
||
| 946 | * |
||
| 947 | * @return static |
||
| 948 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 949 | * |
||
| 950 | * @psalm-return static<TKey,T> |
||
| 951 | * @psalm-mutation-free |
||
| 952 | */ |
||
| 953 | 1 | public function uksortImmutable($function): self |
|
| 954 | { |
||
| 955 | 1 | return $this->customSortKeysImmutable($function); |
|
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Unserialize an string and return the instance of the "Arrayy"-class. |
||
| 960 | * |
||
| 961 | * EXAMPLE: <code> |
||
| 962 | * $serialized = a([1, 4, 7])->serialize(); |
||
| 963 | * a()->unserialize($serialized); |
||
| 964 | * </code> |
||
| 965 | * |
||
| 966 | * @param string $string |
||
| 967 | * |
||
| 968 | * @return $this |
||
| 969 | * |
||
| 970 | * @psalm-return static<TKey,T> |
||
| 971 | */ |
||
| 972 | 2 | public function unserialize($string): self |
|
| 973 | { |
||
| 974 | 2 | if (\PHP_VERSION_ID < 70400) { |
|
| 975 | 2 | parent::unserialize($string); |
|
| 976 | |||
| 977 | 2 | return $this; |
|
| 978 | } |
||
| 979 | |||
| 980 | return \unserialize($string, ['allowed_classes' => [__CLASS__, TypeCheckPhpDoc::class]]); |
||
| 981 | } |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Append a (key) + values to the current array. |
||
| 985 | * |
||
| 986 | * EXAMPLE: <code> |
||
| 987 | * a(['fòô' => ['bàř']])->appendArrayValues(['foo1', 'foo2'], 'fòô'); // Arrayy['fòô' => ['bàř', 'foo1', 'foo2']] |
||
| 988 | * </code> |
||
| 989 | * |
||
| 990 | * @param array $values |
||
| 991 | * @param mixed $key |
||
| 992 | * |
||
| 993 | * @return $this |
||
| 994 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 995 | * |
||
| 996 | * @psalm-param array<mixed,T> $values |
||
| 997 | * @psalm-param TKey|null $key |
||
| 998 | * @psalm-return static<TKey,T> |
||
| 999 | */ |
||
| 1000 | 1 | public function appendArrayValues(array $values, $key = null) |
|
| 1001 | { |
||
| 1002 | 1 | $this->generatorToArray(); |
|
| 1003 | |||
| 1004 | 1 | if ($key !== null) { |
|
| 1005 | if ( |
||
| 1006 | 1 | isset($this->array[$key]) |
|
| 1007 | && |
||
| 1008 | 1 | \is_array($this->array[$key]) |
|
| 1009 | ) { |
||
| 1010 | 1 | foreach ($values as $value) { |
|
| 1011 | 1 | $this->array[$key][] = $value; |
|
| 1012 | } |
||
| 1013 | } else { |
||
| 1014 | foreach ($values as $value) { |
||
| 1015 | 1 | $this->array[$key] = $value; |
|
| 1016 | } |
||
| 1017 | } |
||
| 1018 | } else { |
||
| 1019 | foreach ($values as $value) { |
||
| 1020 | $this->array[] = $value; |
||
| 1021 | } |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | 1 | return $this; |
|
| 1025 | } |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Add a suffix to each key. |
||
| 1029 | * |
||
| 1030 | * @param int|string $prefix |
||
| 1031 | * |
||
| 1032 | * @return static |
||
| 1033 | * <p>(Immutable) Return an Arrayy object, with the prefixed keys.</p> |
||
| 1034 | * |
||
| 1035 | * @psalm-return static<TKey,T> |
||
| 1036 | * @psalm-mutation-free |
||
| 1037 | */ |
||
| 1038 | 10 | View Code Duplication | public function appendToEachKey($prefix): self |
| 1039 | { |
||
| 1040 | // init |
||
| 1041 | 10 | $result = []; |
|
| 1042 | |||
| 1043 | 10 | foreach ($this->getGenerator() as $key => $item) { |
|
| 1044 | 9 | if ($item instanceof self) { |
|
| 1045 | $result[$prefix . $key] = $item->appendToEachKey($prefix); |
||
| 1046 | 9 | } elseif (\is_array($item)) { |
|
| 1047 | $result[$prefix . $key] = self::create($item, $this->iteratorClass, false) |
||
| 1048 | ->appendToEachKey($prefix) |
||
| 1049 | ->toArray(); |
||
| 1050 | } else { |
||
| 1051 | 9 | $result[$prefix . $key] = $item; |
|
| 1052 | } |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | 10 | return self::create($result, $this->iteratorClass, false); |
|
| 1056 | } |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Add a prefix to each value. |
||
| 1060 | * |
||
| 1061 | * @param mixed $prefix |
||
| 1062 | * |
||
| 1063 | * @return static |
||
| 1064 | * <p>(Immutable) Return an Arrayy object, with the prefixed values.</p> |
||
| 1065 | * |
||
| 1066 | * @psalm-return static<TKey,T> |
||
| 1067 | * @psalm-mutation-free |
||
| 1068 | */ |
||
| 1069 | 10 | View Code Duplication | public function appendToEachValue($prefix): self |
| 1070 | { |
||
| 1071 | // init |
||
| 1072 | 10 | $result = []; |
|
| 1073 | |||
| 1074 | 10 | foreach ($this->getGenerator() as $key => $item) { |
|
| 1075 | 9 | if ($item instanceof self) { |
|
| 1076 | $result[$key] = $item->appendToEachValue($prefix); |
||
| 1077 | 9 | } elseif (\is_array($item)) { |
|
| 1078 | $result[$key] = self::create($item, $this->iteratorClass, false)->appendToEachValue($prefix)->toArray(); |
||
| 1079 | 9 | } elseif (\is_object($item) === true) { |
|
| 1080 | 1 | $result[$key] = $item; |
|
| 1081 | } else { |
||
| 1082 | 9 | $result[$key] = $prefix . $item; |
|
| 1083 | } |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | 10 | return self::create($result, $this->iteratorClass, false); |
|
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Sort an array in reverse order and maintain index association. |
||
| 1091 | * |
||
| 1092 | * @return $this |
||
| 1093 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1094 | * |
||
| 1095 | * @psalm-return static<TKey,T> |
||
| 1096 | */ |
||
| 1097 | 4 | public function arsort(): self |
|
| 1098 | { |
||
| 1099 | 4 | $this->generatorToArray(); |
|
| 1100 | |||
| 1101 | 4 | \arsort($this->array); |
|
| 1102 | |||
| 1103 | 4 | return $this; |
|
| 1104 | } |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Sort an array in reverse order and maintain index association. |
||
| 1108 | * |
||
| 1109 | * @return $this |
||
| 1110 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 1111 | * |
||
| 1112 | * @psalm-return static<TKey,T> |
||
| 1113 | * @psalm-mutation-free |
||
| 1114 | */ |
||
| 1115 | 10 | public function arsortImmutable(): self |
|
| 1116 | { |
||
| 1117 | 10 | $that = clone $this; |
|
| 1118 | |||
| 1119 | 10 | $that->generatorToArray(); |
|
| 1120 | |||
| 1121 | 10 | \arsort($that->array); |
|
| 1122 | |||
| 1123 | 10 | return $that; |
|
| 1124 | } |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Iterate over the current array and execute a callback for each loop. |
||
| 1128 | * |
||
| 1129 | * EXAMPLE: <code> |
||
| 1130 | * $result = A::create(); |
||
| 1131 | * $closure = function ($value, $key) use ($result) { |
||
| 1132 | * $result[$key] = ':' . $value . ':'; |
||
| 1133 | * }; |
||
| 1134 | * a(['foo', 'bar' => 'bis'])->at($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 1135 | * </code> |
||
| 1136 | * |
||
| 1137 | * @param \Closure $closure |
||
| 1138 | * |
||
| 1139 | * @return static |
||
| 1140 | * <p>(Immutable)</p> |
||
| 1141 | * |
||
| 1142 | * @psalm-return static<TKey,T> |
||
| 1143 | * @psalm-mutation-free |
||
| 1144 | */ |
||
| 1145 | 3 | public function at(\Closure $closure): self |
|
| 1146 | { |
||
| 1147 | 3 | $that = clone $this; |
|
| 1148 | |||
| 1149 | 3 | foreach ($that->getGenerator() as $key => $value) { |
|
| 1150 | 3 | $closure($value, $key); |
|
| 1151 | } |
||
| 1152 | |||
| 1153 | 3 | return static::create( |
|
| 1154 | 3 | $that->toArray(), |
|
| 1155 | 3 | $this->iteratorClass, |
|
| 1156 | 3 | false |
|
| 1157 | ); |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Returns the average value of the current array. |
||
| 1162 | * |
||
| 1163 | * EXAMPLE: <code> |
||
| 1164 | * a([-9, -8, -7, 1.32])->average(2); // -5.67 |
||
| 1165 | * </code> |
||
| 1166 | * |
||
| 1167 | * @param int $decimals <p>The number of decimal-numbers to return.</p> |
||
| 1168 | * |
||
| 1169 | * @return float|int |
||
| 1170 | * <p>The average value.</p> |
||
| 1171 | * @psalm-mutation-free |
||
| 1172 | */ |
||
| 1173 | 10 | public function average($decimals = 0) |
|
| 1174 | { |
||
| 1175 | 10 | $count = \count($this->toArray(), \COUNT_NORMAL); |
|
| 1176 | |||
| 1177 | 10 | if (!$count) { |
|
| 1178 | 2 | return 0; |
|
| 1179 | } |
||
| 1180 | |||
| 1181 | 8 | if ((int) $decimals !== $decimals) { |
|
| 1182 | 3 | $decimals = 0; |
|
| 1183 | } |
||
| 1184 | |||
| 1185 | 8 | return \round(\array_sum($this->toArray()) / $count, $decimals); |
|
| 1186 | } |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Changes all keys in an array. |
||
| 1190 | * |
||
| 1191 | * @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br /> |
||
| 1192 | * or <strong>CASE_LOWER</strong> (default)</p> |
||
| 1193 | * |
||
| 1194 | * @return static |
||
| 1195 | * <p>(Immutable)</p> |
||
| 1196 | * |
||
| 1197 | * @psalm-return static<TKey,T> |
||
| 1198 | * @psalm-mutation-free |
||
| 1199 | */ |
||
| 1200 | 1 | public function changeKeyCase(int $case = \CASE_LOWER): self |
|
| 1201 | { |
||
| 1202 | if ( |
||
| 1203 | 1 | $case !== \CASE_LOWER |
|
| 1204 | && |
||
| 1205 | 1 | $case !== \CASE_UPPER |
|
| 1206 | ) { |
||
| 1207 | $case = \CASE_LOWER; |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | 1 | $return = []; |
|
| 1211 | 1 | foreach ($this->getGenerator() as $key => $value) { |
|
| 1212 | 1 | \assert(\is_string($key) || \is_int($key) || \is_float($key)); |
|
| 1213 | |||
| 1214 | 1 | if ($case === \CASE_LOWER) { |
|
| 1215 | 1 | $key = \mb_strtolower((string) $key); |
|
| 1216 | } else { |
||
| 1217 | 1 | $key = \mb_strtoupper((string) $key); |
|
| 1218 | } |
||
| 1219 | |||
| 1220 | 1 | $return[$key] = $value; |
|
| 1221 | } |
||
| 1222 | |||
| 1223 | 1 | return static::create( |
|
| 1224 | 1 | $return, |
|
| 1225 | 1 | $this->iteratorClass, |
|
| 1226 | 1 | false |
|
| 1227 | ); |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Change the path separator of the array wrapper. |
||
| 1232 | * |
||
| 1233 | * By default, the separator is: "." |
||
| 1234 | * |
||
| 1235 | * @param string $separator <p>Separator to set.</p> |
||
| 1236 | * |
||
| 1237 | * @return $this |
||
| 1238 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1239 | * |
||
| 1240 | * @psalm-return static<TKey,T> |
||
| 1241 | */ |
||
| 1242 | 11 | public function changeSeparator($separator): self |
|
| 1243 | { |
||
| 1244 | 11 | $this->pathSeparator = $separator; |
|
| 1245 | |||
| 1246 | 11 | return $this; |
|
| 1247 | } |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Create a chunked version of the current array. |
||
| 1251 | * |
||
| 1252 | * EXAMPLE: <code> |
||
| 1253 | * a([-9, -8, -7, 1.32])->chunk(2); // Arrayy[[-9, -8], [-7, 1.32]] |
||
| 1254 | * </code> |
||
| 1255 | * |
||
| 1256 | * @param int $size <p>Size of each chunk.</p> |
||
| 1257 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 1258 | * |
||
| 1259 | * @return static |
||
| 1260 | * <p>(Immutable) A new array of chunks from the original array.</p> |
||
| 1261 | * |
||
| 1262 | * @psalm-return static<TKey,T> |
||
| 1263 | * @psalm-mutation-free |
||
| 1264 | */ |
||
| 1265 | 5 | public function chunk($size, $preserveKeys = false): self |
|
| 1266 | { |
||
| 1267 | 5 | if ($preserveKeys) { |
|
| 1268 | $generator = function () use ($size) { |
||
| 1269 | $values = []; |
||
| 1270 | $tmpCounter = 0; |
||
| 1271 | foreach ($this->getGenerator() as $key => $value) { |
||
| 1272 | ++$tmpCounter; |
||
| 1273 | |||
| 1274 | $values[$key] = $value; |
||
| 1275 | if ($tmpCounter === $size) { |
||
| 1276 | yield $values; |
||
| 1277 | |||
| 1278 | $values = []; |
||
| 1279 | $tmpCounter = 0; |
||
| 1280 | } |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | if ($values !== []) { |
||
| 1284 | yield $values; |
||
| 1285 | } |
||
| 1286 | }; |
||
| 1287 | View Code Duplication | } else { |
|
| 1288 | $generator = function () use ($size) { |
||
| 1289 | 5 | $values = []; |
|
| 1290 | 5 | $tmpCounter = 0; |
|
| 1291 | 5 | foreach ($this->getGenerator() as $key => $value) { |
|
| 1292 | 5 | ++$tmpCounter; |
|
| 1293 | |||
| 1294 | 5 | $values[] = $value; |
|
| 1295 | 5 | if ($tmpCounter === $size) { |
|
| 1296 | 5 | yield $values; |
|
| 1297 | |||
| 1298 | 5 | $values = []; |
|
| 1299 | 5 | $tmpCounter = 0; |
|
| 1300 | } |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | 5 | if ($values !== []) { |
|
| 1304 | 4 | yield $values; |
|
| 1305 | } |
||
| 1306 | 5 | }; |
|
| 1307 | } |
||
| 1308 | |||
| 1309 | 5 | return static::create( |
|
| 1310 | 5 | $generator, |
|
| 1311 | 5 | $this->iteratorClass, |
|
| 1312 | 5 | false |
|
| 1313 | ); |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | /** |
||
| 1317 | * Clean all falsy values from the current array. |
||
| 1318 | * |
||
| 1319 | * EXAMPLE: <code> |
||
| 1320 | * a([-8 => -9, 1, 2 => false])->clean(); // Arrayy[-8 => -9, 1] |
||
| 1321 | * </code> |
||
| 1322 | * |
||
| 1323 | * @return static |
||
| 1324 | * <p>(Immutable)</p> |
||
| 1325 | * |
||
| 1326 | * @psalm-return static<TKey,T> |
||
| 1327 | * @psalm-mutation-free |
||
| 1328 | */ |
||
| 1329 | 8 | public function clean(): self |
|
| 1330 | { |
||
| 1331 | 8 | return $this->filter( |
|
| 1332 | static function ($value) { |
||
| 1333 | 7 | return (bool) $value; |
|
| 1334 | 8 | } |
|
| 1335 | ); |
||
| 1336 | } |
||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * WARNING!!! -> Clear the current full array or a $key of it. |
||
| 1340 | * |
||
| 1341 | * EXAMPLE: <code> |
||
| 1342 | * a([-8 => -9, 1, 2 => false])->clear(); // Arrayy[] |
||
| 1343 | * </code> |
||
| 1344 | * |
||
| 1345 | * @param int|int[]|string|string[]|null $key |
||
| 1346 | * |
||
| 1347 | * @return $this |
||
| 1348 | * <p>(Mutable) Return this Arrayy object, with an empty array.</p> |
||
| 1349 | * |
||
| 1350 | * @psalm-return static<TKey,T> |
||
| 1351 | */ |
||
| 1352 | 10 | public function clear($key = null): self |
|
| 1353 | { |
||
| 1354 | 10 | if ($key !== null) { |
|
| 1355 | 3 | if (\is_array($key)) { |
|
| 1356 | 1 | foreach ($key as $keyTmp) { |
|
| 1357 | 1 | $this->offsetUnset($keyTmp); |
|
| 1358 | } |
||
| 1359 | } else { |
||
| 1360 | 2 | $this->offsetUnset($key); |
|
| 1361 | } |
||
| 1362 | |||
| 1363 | 3 | return $this; |
|
| 1364 | } |
||
| 1365 | |||
| 1366 | 7 | $this->array = []; |
|
| 1367 | 7 | $this->generator = null; |
|
| 1368 | |||
| 1369 | 7 | return $this; |
|
| 1370 | } |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Check if an item is in the current array. |
||
| 1374 | * |
||
| 1375 | * EXAMPLE: <code> |
||
| 1376 | * a([1, true])->contains(true); // true |
||
| 1377 | * </code> |
||
| 1378 | * |
||
| 1379 | * @param float|int|string $value |
||
| 1380 | * @param bool $recursive |
||
| 1381 | * @param bool $strict |
||
| 1382 | * |
||
| 1383 | * @return bool |
||
| 1384 | * @psalm-mutation-free |
||
| 1385 | */ |
||
| 1386 | 23 | public function contains($value, bool $recursive = false, bool $strict = true): bool |
|
| 1387 | { |
||
| 1388 | 23 | if ($recursive === true) { |
|
| 1389 | 18 | return $this->in_array_recursive($value, $this->toArray(), $strict); |
|
| 1390 | } |
||
| 1391 | |||
| 1392 | /** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ |
||
| 1393 | 14 | foreach ($this->getGeneratorByReference() as &$valueFromArray) { |
|
| 1394 | 11 | if ($strict) { |
|
| 1395 | 11 | if ($value === $valueFromArray) { |
|
| 1396 | 11 | return true; |
|
| 1397 | } |
||
| 1398 | } else { |
||
| 1399 | /** @noinspection NestedPositiveIfStatementsInspection */ |
||
| 1400 | if ($value == $valueFromArray) { |
||
| 1401 | 7 | return true; |
|
| 1402 | } |
||
| 1403 | } |
||
| 1404 | } |
||
| 1405 | |||
| 1406 | 7 | return false; |
|
| 1407 | } |
||
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Check if an (case-insensitive) string is in the current array. |
||
| 1411 | * |
||
| 1412 | * EXAMPLE: <code> |
||
| 1413 | * a(['E', 'é'])->containsCaseInsensitive('É'); // true |
||
| 1414 | * </code> |
||
| 1415 | * |
||
| 1416 | * @param mixed $value |
||
| 1417 | * @param bool $recursive |
||
| 1418 | * |
||
| 1419 | * @return bool |
||
| 1420 | * @psalm-mutation-free |
||
| 1421 | * |
||
| 1422 | * @psalm-suppress InvalidCast - hack for int|float|bool support |
||
| 1423 | */ |
||
| 1424 | 26 | public function containsCaseInsensitive($value, $recursive = false): bool |
|
| 1425 | { |
||
| 1426 | 26 | if ($value === null) { |
|
| 1427 | 2 | return false; |
|
| 1428 | } |
||
| 1429 | |||
| 1430 | 24 | if ($recursive === true) { |
|
| 1431 | /** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ |
||
| 1432 | 24 | foreach ($this->getGeneratorByReference() as $key => &$valueTmp) { |
|
| 1433 | 22 | if (\is_array($valueTmp)) { |
|
| 1434 | 5 | $return = (new self($valueTmp))->containsCaseInsensitive($value, $recursive); |
|
| 1435 | 5 | if ($return === true) { |
|
| 1436 | 5 | return $return; |
|
| 1437 | } |
||
| 1438 | 22 | } elseif (\mb_strtoupper((string) $valueTmp) === \mb_strtoupper((string) $value)) { |
|
| 1439 | 22 | return true; |
|
| 1440 | } |
||
| 1441 | } |
||
| 1442 | |||
| 1443 | 8 | return false; |
|
| 1444 | } |
||
| 1445 | |||
| 1446 | /** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ |
||
| 1447 | 12 | foreach ($this->getGeneratorByReference() as $key => &$valueTmp) { |
|
| 1448 | 11 | if (\mb_strtoupper((string) $valueTmp) === \mb_strtoupper((string) $value)) { |
|
| 1449 | 11 | return true; |
|
| 1450 | } |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | 4 | return false; |
|
| 1454 | } |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Check if the given key/index exists in the array. |
||
| 1458 | * |
||
| 1459 | * EXAMPLE: <code> |
||
| 1460 | * a([1 => true])->containsKey(1); // true |
||
| 1461 | * </code> |
||
| 1462 | * |
||
| 1463 | * @param int|string $key <p>key/index to search for</p> |
||
| 1464 | * |
||
| 1465 | * @return bool |
||
| 1466 | * <p>Returns true if the given key/index exists in the array, false otherwise.</p> |
||
| 1467 | * |
||
| 1468 | * @psalm-mutation-free |
||
| 1469 | */ |
||
| 1470 | 4 | public function containsKey($key): bool |
|
| 1471 | { |
||
| 1472 | 4 | return $this->offsetExists($key); |
|
| 1473 | } |
||
| 1474 | |||
| 1475 | /** |
||
| 1476 | * Check if all given needles are present in the array as key/index. |
||
| 1477 | * |
||
| 1478 | * EXAMPLE: <code> |
||
| 1479 | * a([1 => true])->containsKeys(array(1 => 0)); // true |
||
| 1480 | * </code> |
||
| 1481 | * |
||
| 1482 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1483 | * @param bool $recursive |
||
| 1484 | * |
||
| 1485 | * @return bool |
||
| 1486 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1487 | * |
||
| 1488 | * @psalm-param array<mixed,mixed>|array<TKey> $needles |
||
| 1489 | * @psalm-mutation-free |
||
| 1490 | */ |
||
| 1491 | 2 | public function containsKeys(array $needles, $recursive = false): bool |
|
| 1492 | { |
||
| 1493 | 2 | if ($recursive === true) { |
|
| 1494 | return |
||
| 1495 | 2 | \count( |
|
| 1496 | 2 | \array_intersect( |
|
| 1497 | 2 | $needles, |
|
| 1498 | 2 | $this->keys(true)->toArray() |
|
| 1499 | ), |
||
| 1500 | 2 | \COUNT_RECURSIVE |
|
| 1501 | ) |
||
| 1502 | === |
||
| 1503 | 2 | \count( |
|
| 1504 | 2 | $needles, |
|
| 1505 | 2 | \COUNT_RECURSIVE |
|
| 1506 | ); |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | 1 | return \count( |
|
| 1510 | 1 | \array_intersect($needles, $this->keys()->toArray()), |
|
| 1511 | 1 | \COUNT_NORMAL |
|
| 1512 | ) |
||
| 1513 | === |
||
| 1514 | 1 | \count( |
|
| 1515 | 1 | $needles, |
|
| 1516 | 1 | \COUNT_NORMAL |
|
| 1517 | ); |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | /** |
||
| 1521 | * Check if all given needles are present in the array as key/index. |
||
| 1522 | * |
||
| 1523 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1524 | * |
||
| 1525 | * @return bool |
||
| 1526 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1527 | * |
||
| 1528 | * @psalm-param array<mixed,mixed>|array<TKey> $needles |
||
| 1529 | * @psalm-mutation-free |
||
| 1530 | */ |
||
| 1531 | 1 | public function containsKeysRecursive(array $needles): bool |
|
| 1532 | { |
||
| 1533 | 1 | return $this->containsKeys($needles, true); |
|
| 1534 | } |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * alias: for "Arrayy->contains()" |
||
| 1538 | * |
||
| 1539 | * @param float|int|string $value |
||
| 1540 | * |
||
| 1541 | * @return bool |
||
| 1542 | * |
||
| 1543 | * @see Arrayy::contains() |
||
| 1544 | * @psalm-mutation-free |
||
| 1545 | */ |
||
| 1546 | 9 | public function containsValue($value): bool |
|
| 1547 | { |
||
| 1548 | 9 | return $this->contains($value); |
|
| 1549 | } |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * alias: for "Arrayy->contains($value, true)" |
||
| 1553 | * |
||
| 1554 | * @param float|int|string $value |
||
| 1555 | * |
||
| 1556 | * @return bool |
||
| 1557 | * |
||
| 1558 | * @see Arrayy::contains() |
||
| 1559 | * @psalm-mutation-free |
||
| 1560 | */ |
||
| 1561 | 18 | public function containsValueRecursive($value): bool |
|
| 1562 | { |
||
| 1563 | 18 | return $this->contains($value, true); |
|
| 1564 | } |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Check if all given needles are present in the array. |
||
| 1568 | * |
||
| 1569 | * EXAMPLE: <code> |
||
| 1570 | * a([1, true])->containsValues(array(1, true)); // true |
||
| 1571 | * </code> |
||
| 1572 | * |
||
| 1573 | * @param array $needles |
||
| 1574 | * |
||
| 1575 | * @return bool |
||
| 1576 | * <p>Returns true if all the given values exists in the array, false otherwise.</p> |
||
| 1577 | * |
||
| 1578 | * @psalm-param array<mixed>|array<T> $needles |
||
| 1579 | * @psalm-mutation-free |
||
| 1580 | */ |
||
| 1581 | 1 | public function containsValues(array $needles): bool |
|
| 1582 | { |
||
| 1583 | 1 | return \count( |
|
| 1584 | 1 | \array_intersect( |
|
| 1585 | 1 | $needles, |
|
| 1586 | 1 | $this->toArray() |
|
| 1587 | ), |
||
| 1588 | 1 | \COUNT_NORMAL |
|
| 1589 | ) |
||
| 1590 | === |
||
| 1591 | 1 | \count( |
|
| 1592 | 1 | $needles, |
|
| 1593 | 1 | \COUNT_NORMAL |
|
| 1594 | ); |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * Counts all the values of an array |
||
| 1599 | * |
||
| 1600 | * @see http://php.net/manual/en/function.array-count-values.php |
||
| 1601 | * |
||
| 1602 | * @return static |
||
| 1603 | * <p> |
||
| 1604 | * (Immutable) |
||
| 1605 | * An associative Arrayy-object of values from input as |
||
| 1606 | * keys and their count as value. |
||
| 1607 | * </p> |
||
| 1608 | * |
||
| 1609 | * @psalm-return static<TKey,T> |
||
| 1610 | * @psalm-mutation-free |
||
| 1611 | */ |
||
| 1612 | 7 | public function countValues(): self |
|
| 1613 | { |
||
| 1614 | 7 | return self::create(\array_count_values($this->toArray()), $this->iteratorClass); |
|
| 1615 | } |
||
| 1616 | |||
| 1617 | /** |
||
| 1618 | * Creates an Arrayy object. |
||
| 1619 | * |
||
| 1620 | * @param mixed $data |
||
| 1621 | * @param string $iteratorClass |
||
| 1622 | * @param bool $checkPropertiesInConstructor |
||
| 1623 | * |
||
| 1624 | * @return static |
||
| 1625 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1626 | * |
||
| 1627 | * @psalm-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 1628 | * |
||
| 1629 | * @psalm-mutation-free |
||
| 1630 | */ |
||
| 1631 | 729 | public static function create( |
|
| 1632 | $data = [], |
||
| 1633 | string $iteratorClass = ArrayyIterator::class, |
||
| 1634 | bool $checkPropertiesInConstructor = true |
||
| 1635 | ) { |
||
| 1636 | 729 | return new static( |
|
| 1637 | 729 | $data, |
|
| 1638 | 729 | $iteratorClass, |
|
| 1639 | 729 | $checkPropertiesInConstructor |
|
| 1640 | ); |
||
| 1641 | } |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Flatten an array with the given character as a key delimiter. |
||
| 1645 | * |
||
| 1646 | * EXAMPLE: <code> |
||
| 1647 | * $dot = a(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]); |
||
| 1648 | * $flatten = $dot->flatten(); |
||
| 1649 | * $flatten['foo.abc']; // 'xyz' |
||
| 1650 | * $flatten['foo.bar.0']; // 'baz' |
||
| 1651 | * </code> |
||
| 1652 | * |
||
| 1653 | * @param string $delimiter |
||
| 1654 | * @param string $prepend |
||
| 1655 | * @param array|null $items |
||
| 1656 | * |
||
| 1657 | * @return array |
||
| 1658 | */ |
||
| 1659 | 2 | public function flatten($delimiter = '.', $prepend = '', $items = null) |
|
| 1660 | { |
||
| 1661 | // init |
||
| 1662 | 2 | $flatten = []; |
|
| 1663 | |||
| 1664 | 2 | if ($items === null) { |
|
| 1665 | 2 | $items = $this->getArray(); |
|
| 1666 | } |
||
| 1667 | |||
| 1668 | 2 | foreach ($items as $key => $value) { |
|
| 1669 | 2 | if (\is_array($value) && $value !== []) { |
|
| 1670 | 2 | $flatten[] = $this->flatten($delimiter, $prepend . $key . $delimiter, $value); |
|
| 1671 | } else { |
||
| 1672 | 2 | $flatten[] = [$prepend . $key => $value]; |
|
| 1673 | } |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | 2 | if (\count($flatten) === 0) { |
|
| 1677 | return []; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | 2 | return \array_merge_recursive([], ...$flatten); |
|
| 1681 | } |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * WARNING: Creates an Arrayy object by reference. |
||
| 1685 | * |
||
| 1686 | * @param array $array |
||
| 1687 | * |
||
| 1688 | * @return $this |
||
| 1689 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1690 | * |
||
| 1691 | * @psalm-param array<TKey,T> $array |
||
| 1692 | * |
||
| 1693 | * @internal This will not check any types because it's set directly as reference. |
||
| 1694 | */ |
||
| 1695 | 26 | public function createByReference(array &$array = []): self |
|
| 1696 | { |
||
| 1697 | 26 | $this->array = &$array; |
|
| 1698 | 26 | $this->generator = null; |
|
| 1699 | |||
| 1700 | 26 | return $this; |
|
| 1701 | } |
||
| 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<TKey,T> $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<TKey,T> $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 |
|
| 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 elements where the 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 | View Code Duplication | public function diff(array ...$array): self |
| 2066 | { |
||
| 2067 | 13 | if (\count($array) > 1) { |
|
| 2068 | 1 | $array = \array_merge([], ...$array); |
|
| 2069 | } else { |
||
| 2070 | 13 | $array = $array[0]; |
|
| 2071 | } |
||
| 2072 | |||
| 2073 | $generator = function () use ($array): \Generator { |
||
| 2074 | 13 | foreach ($this->getGenerator() as $key => $value) { |
|
| 2075 | 11 | if (\in_array($value, $array, true) === false) { |
|
| 2076 | 11 | yield $key => $value; |
|
| 2077 | } |
||
| 2078 | } |
||
| 2079 | 13 | }; |
|
| 2080 | |||
| 2081 | 13 | return static::create( |
|
| 2082 | 13 | $generator, |
|
| 2083 | 13 | $this->iteratorClass, |
|
| 2084 | 13 | false |
|
| 2085 | ); |
||
| 2086 | } |
||
| 2087 | |||
| 2088 | /** |
||
| 2089 | * Return elements where the keys are only in the current array. |
||
| 2090 | * |
||
| 2091 | * @param array ...$array |
||
| 2092 | * |
||
| 2093 | * @return static |
||
| 2094 | * <p>(Immutable)</p> |
||
| 2095 | * |
||
| 2096 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$array |
||
| 2097 | * @psalm-return static<TKey,T> |
||
| 2098 | * @psalm-mutation-free |
||
| 2099 | */ |
||
| 2100 | 9 | View Code Duplication | public function diffKey(array ...$array): self |
| 2122 | |||
| 2123 | /** |
||
| 2124 | * Return elements where the values and keys are only in the current array. |
||
| 2125 | * |
||
| 2126 | * @param array ...$array |
||
| 2127 | * |
||
| 2128 | * @return static |
||
| 2129 | * <p>(Immutable)</p> |
||
| 2130 | * |
||
| 2131 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2132 | * @psalm-return static<TKey,T> |
||
| 2133 | * @psalm-mutation-free |
||
| 2134 | */ |
||
| 2135 | 9 | public function diffKeyAndValue(array ...$array): self |
|
| 2143 | |||
| 2144 | /** |
||
| 2145 | * Return elements where the values are only in the current multi-dimensional array. |
||
| 2146 | * |
||
| 2147 | * EXAMPLE: <code> |
||
| 2148 | * a([1 => [1 => 1], 2 => [2 => 2]])->diffRecursive([1 => [1 => 1]]); // Arrayy[2 => [2 => 2]] |
||
| 2149 | * </code> |
||
| 2150 | * |
||
| 2151 | * @param array $array |
||
| 2152 | * @param array|\Generator|null $helperVariableForRecursion <p>(only for internal usage)</p> |
||
| 2153 | * |
||
| 2154 | * @return static |
||
| 2155 | * <p>(Immutable)</p> |
||
| 2156 | * |
||
| 2157 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2158 | * @psalm-param null|array<TKey,T>|\Generator<TKey,T> $helperVariableForRecursion |
||
| 2159 | * @psalm-return static<TKey,T> |
||
| 2160 | * @psalm-mutation-free |
||
| 2161 | */ |
||
| 2162 | 1 | public function diffRecursive(array $array = [], $helperVariableForRecursion = null): self |
|
| 2197 | |||
| 2198 | /** |
||
| 2199 | * Return elements where the values that are only in the new $array. |
||
| 2200 | * |
||
| 2201 | * EXAMPLE: <code> |
||
| 2202 | * a([1 => 1])->diffReverse([1 => 1, 2 => 2]); // Arrayy[2 => 2] |
||
| 2203 | * </code> |
||
| 2204 | * |
||
| 2205 | * @param array $array |
||
| 2206 | * |
||
| 2207 | * @return static |
||
| 2208 | * <p>(Immutable)</p> |
||
| 2209 | * |
||
| 2210 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2211 | * @psalm-return static<TKey,T> |
||
| 2212 | * @psalm-mutation-free |
||
| 2213 | */ |
||
| 2214 | 8 | public function diffReverse(array $array = []): self |
|
| 2222 | |||
| 2223 | /** |
||
| 2224 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 2225 | * |
||
| 2226 | * EXAMPLE: <code> |
||
| 2227 | * a(['a' => 1, 'b' => ''])->divide(); // Arrayy[Arrayy['a', 'b'], Arrayy[1, '']] |
||
| 2228 | * </code> |
||
| 2229 | * |
||
| 2230 | * @return static |
||
| 2231 | * <p>(Immutable)</p> |
||
| 2232 | * |
||
| 2233 | * @psalm-return static<TKey,T> |
||
| 2234 | * @psalm-mutation-free |
||
| 2235 | */ |
||
| 2236 | 1 | public function divide(): self |
|
| 2247 | |||
| 2248 | /** |
||
| 2249 | * Iterate over the current array and modify the array's value. |
||
| 2250 | * |
||
| 2251 | * EXAMPLE: <code> |
||
| 2252 | * $result = A::create(); |
||
| 2253 | * $closure = function ($value) { |
||
| 2254 | * return ':' . $value . ':'; |
||
| 2255 | * }; |
||
| 2256 | * a(['foo', 'bar' => 'bis'])->each($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 2257 | * </code> |
||
| 2258 | * |
||
| 2259 | * @param \Closure $closure |
||
| 2260 | * |
||
| 2261 | * @return static |
||
| 2262 | * <p>(Immutable)</p> |
||
| 2263 | * |
||
| 2264 | * @psalm-return static<TKey,T> |
||
| 2265 | * @psalm-mutation-free |
||
| 2266 | */ |
||
| 2267 | 5 | View Code Duplication | public function each(\Closure $closure): self |
| 2282 | |||
| 2283 | /** |
||
| 2284 | * Sets the internal iterator to the last element in the array and returns this element. |
||
| 2285 | * |
||
| 2286 | * @return mixed |
||
| 2287 | */ |
||
| 2288 | public function end() |
||
| 2292 | |||
| 2293 | /** |
||
| 2294 | * Check if a value is in the current array using a closure. |
||
| 2295 | * |
||
| 2296 | * EXAMPLE: <code> |
||
| 2297 | * $callable = function ($value, $key) { |
||
| 2298 | * return 2 === $key and 'two' === $value; |
||
| 2299 | * }; |
||
| 2300 | * a(['foo', 2 => 'two'])->exists($callable); // true |
||
| 2301 | * </code> |
||
| 2302 | * |
||
| 2303 | * @param \Closure $closure |
||
| 2304 | * |
||
| 2305 | * @return bool |
||
| 2306 | * <p>Returns true if the given value is found, false otherwise.</p> |
||
| 2307 | */ |
||
| 2308 | 4 | public function exists(\Closure $closure): bool |
|
| 2323 | |||
| 2324 | /** |
||
| 2325 | * Fill the array until "$num" with "$default" values. |
||
| 2326 | * |
||
| 2327 | * EXAMPLE: <code> |
||
| 2328 | * a(['bar'])->fillWithDefaults(3, 'foo'); // Arrayy['bar', 'foo', 'foo'] |
||
| 2329 | * </code> |
||
| 2330 | * |
||
| 2331 | * @param int $num |
||
| 2332 | * @param mixed $default |
||
| 2333 | * |
||
| 2334 | * @return static |
||
| 2335 | * <p>(Immutable)</p> |
||
| 2336 | * |
||
| 2337 | * @psalm-return static<TKey,T> |
||
| 2338 | * @psalm-mutation-free |
||
| 2339 | */ |
||
| 2340 | 8 | public function fillWithDefaults(int $num, $default = null): self |
|
| 2363 | |||
| 2364 | /** |
||
| 2365 | * Find all items in an array that pass the truth test. |
||
| 2366 | * |
||
| 2367 | * EXAMPLE: <code> |
||
| 2368 | * $closure = function ($value) { |
||
| 2369 | * return $value % 2 !== 0; |
||
| 2370 | * } |
||
| 2371 | * a([1, 2, 3, 4])->filter($closure); // Arrayy[0 => 1, 2 => 3] |
||
| 2372 | * </code> |
||
| 2373 | * |
||
| 2374 | * @param \Closure|null $closure [optional] <p> |
||
| 2375 | * The callback function to use |
||
| 2376 | * </p> |
||
| 2377 | * <p> |
||
| 2378 | * If no callback is supplied, all entries of |
||
| 2379 | * input equal to false (see |
||
| 2380 | * converting to |
||
| 2381 | * boolean) will be removed. |
||
| 2382 | * </p> |
||
| 2383 | * @param int $flag [optional] <p> |
||
| 2384 | * Flag determining what arguments are sent to <i>callback</i>: |
||
| 2385 | * </p> |
||
| 2386 | * <ul> |
||
| 2387 | * <li> |
||
| 2388 | * <b>ARRAY_FILTER_USE_KEY</b> (1) - pass key as the only argument |
||
| 2389 | * to <i>callback</i> instead of the value |
||
| 2390 | * </li> |
||
| 2391 | * <li> |
||
| 2392 | * <b>ARRAY_FILTER_USE_BOTH</b> (2) - pass both value and key as |
||
| 2393 | * arguments to <i>callback</i> instead of the value |
||
| 2394 | * </li> |
||
| 2395 | * </ul> |
||
| 2396 | * |
||
| 2397 | * @return static |
||
| 2398 | * <p>(Immutable)</p> |
||
| 2399 | * |
||
| 2400 | * @psalm-param null|\Closure(T=,TKey=):bool|\Closure(T=):bool||\Closure(TKey=):bool $closure |
||
| 2401 | * @psalm-return static<TKey,T> |
||
| 2402 | * @psalm-mutation-free |
||
| 2403 | */ |
||
| 2404 | 12 | public function filter($closure = null, int $flag = \ARRAY_FILTER_USE_BOTH) |
|
| 2442 | |||
| 2443 | /** |
||
| 2444 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular |
||
| 2445 | * property within that. |
||
| 2446 | * |
||
| 2447 | * @param string $property |
||
| 2448 | * @param mixed $value |
||
| 2449 | * @param string $comparisonOp |
||
| 2450 | * <p> |
||
| 2451 | * 'eq' (equals),<br /> |
||
| 2452 | * 'gt' (greater),<br /> |
||
| 2453 | * 'gte' || 'ge' (greater or equals),<br /> |
||
| 2454 | * 'lt' (less),<br /> |
||
| 2455 | * 'lte' || 'le' (less or equals),<br /> |
||
| 2456 | * 'ne' (not equals),<br /> |
||
| 2457 | * 'contains',<br /> |
||
| 2458 | * 'notContains',<br /> |
||
| 2459 | * 'newer' (via strtotime),<br /> |
||
| 2460 | * 'older' (via strtotime),<br /> |
||
| 2461 | * </p> |
||
| 2462 | * |
||
| 2463 | * @return static |
||
| 2464 | * <p>(Immutable)</p> |
||
| 2465 | * |
||
| 2466 | * @psalm-param mixed|T $value |
||
| 2467 | * @psalm-return static<TKey,T> |
||
| 2468 | * @psalm-mutation-free |
||
| 2469 | * |
||
| 2470 | * @psalm-suppress MissingClosureReturnType |
||
| 2471 | * @psalm-suppress MissingClosureParamType |
||
| 2472 | */ |
||
| 2473 | 1 | public function filterBy( |
|
| 2545 | |||
| 2546 | /** |
||
| 2547 | * Find the first item in an array that passes the truth test, otherwise return false. |
||
| 2548 | * |
||
| 2549 | * EXAMPLE: <code> |
||
| 2550 | * $search = 'foo'; |
||
| 2551 | * $closure = function ($value, $key) use ($search) { |
||
| 2552 | * return $value === $search; |
||
| 2553 | * }; |
||
| 2554 | * a(['foo', 'bar', 'lall'])->find($closure); // 'foo' |
||
| 2555 | * </code> |
||
| 2556 | * |
||
| 2557 | * @param \Closure $closure |
||
| 2558 | * |
||
| 2559 | * @return false|mixed |
||
| 2560 | * <p>Return false if we did not find the value.</p> |
||
| 2561 | */ |
||
| 2562 | 8 | View Code Duplication | public function find(\Closure $closure) |
| 2572 | |||
| 2573 | /** |
||
| 2574 | * find by ... |
||
| 2575 | * |
||
| 2576 | * EXAMPLE: <code> |
||
| 2577 | * $array = [ |
||
| 2578 | * 0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01'], |
||
| 2579 | * 1 => ['id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468, 'when' => '2014-07-15'], |
||
| 2580 | * ]; |
||
| 2581 | * a($array)->filterBy('name', 'foo'); // Arrayy[0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01']] |
||
| 2582 | * </code> |
||
| 2583 | * |
||
| 2584 | * @param string $property |
||
| 2585 | * @param mixed $value |
||
| 2586 | * @param string $comparisonOp |
||
| 2587 | * |
||
| 2588 | * @return static |
||
| 2589 | * <p>(Immutable)</p> |
||
| 2590 | * |
||
| 2591 | * @psalm-param mixed|T $value |
||
| 2592 | * @psalm-return static<TKey,T> |
||
| 2593 | * @psalm-mutation-free |
||
| 2594 | */ |
||
| 2595 | 1 | public function findBy(string $property, $value, string $comparisonOp = 'eq'): self |
|
| 2599 | |||
| 2600 | /** |
||
| 2601 | * Get the first value from the current array. |
||
| 2602 | * |
||
| 2603 | * EXAMPLE: <code> |
||
| 2604 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->first(); // 'foo' |
||
| 2605 | * </code> |
||
| 2606 | * |
||
| 2607 | * @return mixed |
||
| 2608 | * <p>Return null if there wasn't a element.</p> |
||
| 2609 | */ |
||
| 2610 | 22 | public function first() |
|
| 2619 | |||
| 2620 | /** |
||
| 2621 | * Get the first key from the current array. |
||
| 2622 | * |
||
| 2623 | * @return mixed |
||
| 2624 | * <p>Return null if there wasn't a element.</p> |
||
| 2625 | * @psalm-mutation-free |
||
| 2626 | */ |
||
| 2627 | 29 | public function firstKey() |
|
| 2633 | |||
| 2634 | /** |
||
| 2635 | * Get the first value(s) from the current array. |
||
| 2636 | * And will return an empty array if there was no first entry. |
||
| 2637 | * |
||
| 2638 | * EXAMPLE: <code> |
||
| 2639 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsImmutable(2); // Arrayy[0 => 'foo', 1 => 'bar'] |
||
| 2640 | * </code> |
||
| 2641 | * |
||
| 2642 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2643 | * |
||
| 2644 | * @return static |
||
| 2645 | * <p>(Immutable)</p> |
||
| 2646 | * |
||
| 2647 | * @psalm-return static<TKey,T> |
||
| 2648 | * @psalm-mutation-free |
||
| 2649 | */ |
||
| 2650 | 37 | public function firstsImmutable(int $number = null): self |
|
| 2666 | |||
| 2667 | /** |
||
| 2668 | * Get the first value(s) from the current array. |
||
| 2669 | * And will return an empty array if there was no first entry. |
||
| 2670 | * |
||
| 2671 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2672 | * |
||
| 2673 | * @return static |
||
| 2674 | * <p>(Immutable)</p> |
||
| 2675 | * |
||
| 2676 | * @psalm-return static<TKey,T> |
||
| 2677 | * @psalm-mutation-free |
||
| 2678 | */ |
||
| 2679 | 3 | View Code Duplication | public function firstsKeys(int $number = null): self |
| 2695 | |||
| 2696 | /** |
||
| 2697 | * Get and remove the first value(s) from the current array. |
||
| 2698 | * And will return an empty array if there was no first entry. |
||
| 2699 | * |
||
| 2700 | * EXAMPLE: <code> |
||
| 2701 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsMutable(); // 'foo' |
||
| 2702 | * </code> |
||
| 2703 | * |
||
| 2704 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2705 | * |
||
| 2706 | * @return $this |
||
| 2707 | * <p>(Mutable)</p> |
||
| 2708 | * |
||
| 2709 | * @psalm-return static<TKey,T> |
||
| 2710 | */ |
||
| 2711 | 34 | public function firstsMutable(int $number = null): self |
|
| 2723 | |||
| 2724 | /** |
||
| 2725 | * Exchanges all keys with their associated values in an array. |
||
| 2726 | * |
||
| 2727 | * EXAMPLE: <code> |
||
| 2728 | * a([0 => 'foo', 1 => 'bar'])->flip(); // Arrayy['foo' => 0, 'bar' => 1] |
||
| 2729 | * </code> |
||
| 2730 | * |
||
| 2731 | * @return static |
||
| 2732 | * <p>(Immutable)</p> |
||
| 2733 | * |
||
| 2734 | * @psalm-return static<array-key,TKey> |
||
| 2735 | * @psalm-mutation-free |
||
| 2736 | */ |
||
| 2737 | 1 | public function flip(): self |
|
| 2751 | |||
| 2752 | /** |
||
| 2753 | * Get a value from an array (optional using dot-notation). |
||
| 2754 | * |
||
| 2755 | * EXAMPLE: <code> |
||
| 2756 | * $arrayy = a(['user' => ['lastname' => 'Moelleken']]); |
||
| 2757 | * $arrayy->get('user.lastname'); // 'Moelleken' |
||
| 2758 | * // --- |
||
| 2759 | * $arrayy = new A(); |
||
| 2760 | * $arrayy['user'] = ['lastname' => 'Moelleken']; |
||
| 2761 | * $arrayy['user.firstname'] = 'Lars'; |
||
| 2762 | * $arrayy['user']['lastname']; // Moelleken |
||
| 2763 | * $arrayy['user.lastname']; // Moelleken |
||
| 2764 | * $arrayy['user.firstname']; // Lars |
||
| 2765 | * </code> |
||
| 2766 | * |
||
| 2767 | * @param mixed $key <p>The key to look for.</p> |
||
| 2768 | * @param mixed $fallback <p>Value to fallback to.</p> |
||
| 2769 | * @param array $array <p>The array to get from, if it's set to "null" we use the current array from the |
||
| 2770 | * class.</p> |
||
| 2771 | * @param bool $useByReference |
||
| 2772 | * |
||
| 2773 | * @return mixed|static |
||
| 2774 | * |
||
| 2775 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 2776 | * @psalm-mutation-free |
||
| 2777 | */ |
||
| 2778 | 248 | public function get( |
|
| 2950 | |||
| 2951 | /** |
||
| 2952 | * alias: for "Arrayy->toArray()" |
||
| 2953 | * |
||
| 2954 | * @return array |
||
| 2955 | * |
||
| 2956 | * @see Arrayy::getArray() |
||
| 2957 | * |
||
| 2958 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 2959 | */ |
||
| 2960 | 15 | public function getAll(): array |
|
| 2964 | |||
| 2965 | /** |
||
| 2966 | * Get the current array from the "Arrayy"-object. |
||
| 2967 | * |
||
| 2968 | * alias for "toArray()" |
||
| 2969 | * |
||
| 2970 | * @param bool $convertAllArrayyElements <p> |
||
| 2971 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 2972 | * </p> |
||
| 2973 | * @param bool $preserveKeys <p> |
||
| 2974 | * e.g.: A generator maybe return the same key more then once, |
||
| 2975 | * so maybe you will ignore the keys. |
||
| 2976 | * </p> |
||
| 2977 | * |
||
| 2978 | * @return array |
||
| 2979 | * |
||
| 2980 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 2981 | * @psalm-mutation-free |
||
| 2982 | * |
||
| 2983 | * @see Arrayy::toArray() |
||
| 2984 | */ |
||
| 2985 | 513 | public function getArray( |
|
| 2994 | |||
| 2995 | /** |
||
| 2996 | * @param string $json |
||
| 2997 | * |
||
| 2998 | * @return $this |
||
| 2999 | */ |
||
| 3000 | 3 | public static function createFromJsonMapper(string $json) |
|
| 3016 | |||
| 3017 | /** |
||
| 3018 | * @return array<int|string,TypeCheckInterface>|mixed|TypeCheckArray<int|string,TypeCheckInterface>|TypeInterface |
||
| 3019 | * |
||
| 3020 | * @internal |
||
| 3021 | */ |
||
| 3022 | 6 | public function getPhpDocPropertiesFromClass() |
|
| 3030 | |||
| 3031 | /** |
||
| 3032 | * Get the current array from the "Arrayy"-object as list. |
||
| 3033 | * |
||
| 3034 | * alias for "toList()" |
||
| 3035 | * |
||
| 3036 | * @param bool $convertAllArrayyElements <p> |
||
| 3037 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 3038 | * </p> |
||
| 3039 | * |
||
| 3040 | * @return array |
||
| 3041 | * |
||
| 3042 | * @psalm-return array<int,mixed>|array<int,T> |
||
| 3043 | * @psalm-mutation-free |
||
| 3044 | * |
||
| 3045 | * @see Arrayy::toList() |
||
| 3046 | */ |
||
| 3047 | 1 | public function getList(bool $convertAllArrayyElements = false): array |
|
| 3051 | |||
| 3052 | /** |
||
| 3053 | * Returns the values from a single column of the input array, identified by |
||
| 3054 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 3055 | * |
||
| 3056 | * EXAMPLE: <code> |
||
| 3057 | * a([['foo' => 'bar', 'id' => 1], ['foo => 'lall', 'id' => 2]])->getColumn('foo', 'id'); // Arrayy[1 => 'bar', 2 => 'lall'] |
||
| 3058 | * </code> |
||
| 3059 | * |
||
| 3060 | * INFO: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 3061 | * array by the values from the $indexKey column in the input array. |
||
| 3062 | * |
||
| 3063 | * @param int|string|null $columnKey |
||
| 3064 | * @param int|string|null $indexKey |
||
| 3065 | * |
||
| 3066 | * @return static |
||
| 3067 | * <p>(Immutable)</p> |
||
| 3068 | * |
||
| 3069 | * @psalm-return static<TKey,T> |
||
| 3070 | * @psalm-mutation-free |
||
| 3071 | */ |
||
| 3072 | 1 | public function getColumn($columnKey = null, $indexKey = null): self |
|
| 3132 | |||
| 3133 | /** |
||
| 3134 | * Get the current array from the "Arrayy"-object as generator by reference. |
||
| 3135 | * |
||
| 3136 | * @return \Generator |
||
| 3137 | * |
||
| 3138 | * @psalm-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3139 | */ |
||
| 3140 | 75 | public function &getGeneratorByReference(): \Generator |
|
| 3159 | |||
| 3160 | /** |
||
| 3161 | * Get the current array from the "Arrayy"-object as generator. |
||
| 3162 | * |
||
| 3163 | * @return \Generator |
||
| 3164 | * |
||
| 3165 | * @psalm-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3166 | * @psalm-mutation-free |
||
| 3167 | */ |
||
| 3168 | 1072 | public function getGenerator(): \Generator |
|
| 3178 | |||
| 3179 | /** |
||
| 3180 | * alias: for "Arrayy->keys()" |
||
| 3181 | * |
||
| 3182 | * @return static |
||
| 3183 | * <p>(Immutable)</p> |
||
| 3184 | * |
||
| 3185 | * @see Arrayy::keys() |
||
| 3186 | * |
||
| 3187 | * @psalm-return static<array-key,TKey> |
||
| 3188 | * @psalm-mutation-free |
||
| 3189 | */ |
||
| 3190 | 2 | public function getKeys() |
|
| 3194 | |||
| 3195 | /** |
||
| 3196 | * Get the current array from the "Arrayy"-object as object. |
||
| 3197 | * |
||
| 3198 | * @return \stdClass |
||
| 3199 | */ |
||
| 3200 | 4 | public function getObject(): \stdClass |
|
| 3204 | |||
| 3205 | /** |
||
| 3206 | * alias: for "Arrayy->randomImmutable()" |
||
| 3207 | * |
||
| 3208 | * @return static |
||
| 3209 | * <p>(Immutable)</p> |
||
| 3210 | * |
||
| 3211 | * @see Arrayy::randomImmutable() |
||
| 3212 | * |
||
| 3213 | * @psalm-return static<int|array-key,T> |
||
| 3214 | */ |
||
| 3215 | 4 | public function getRandom(): self |
|
| 3219 | |||
| 3220 | /** |
||
| 3221 | * alias: for "Arrayy->randomKey()" |
||
| 3222 | * |
||
| 3223 | * @return mixed |
||
| 3224 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 3225 | * |
||
| 3226 | * @see Arrayy::randomKey() |
||
| 3227 | */ |
||
| 3228 | 3 | public function getRandomKey() |
|
| 3232 | |||
| 3233 | /** |
||
| 3234 | * alias: for "Arrayy->randomKeys()" |
||
| 3235 | * |
||
| 3236 | * @param int $number |
||
| 3237 | * |
||
| 3238 | * @return static |
||
| 3239 | * <p>(Immutable)</p> |
||
| 3240 | * |
||
| 3241 | * @see Arrayy::randomKeys() |
||
| 3242 | * |
||
| 3243 | * @psalm-return static<TKey,T> |
||
| 3244 | */ |
||
| 3245 | 8 | public function getRandomKeys(int $number): self |
|
| 3249 | |||
| 3250 | /** |
||
| 3251 | * alias: for "Arrayy->randomValue()" |
||
| 3252 | * |
||
| 3253 | * @return mixed |
||
| 3254 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 3255 | * |
||
| 3256 | * @see Arrayy::randomValue() |
||
| 3257 | */ |
||
| 3258 | 3 | public function getRandomValue() |
|
| 3262 | |||
| 3263 | /** |
||
| 3264 | * alias: for "Arrayy->randomValues()" |
||
| 3265 | * |
||
| 3266 | * @param int $number |
||
| 3267 | * |
||
| 3268 | * @return static |
||
| 3269 | * <p>(Immutable)</p> |
||
| 3270 | * |
||
| 3271 | * @see Arrayy::randomValues() |
||
| 3272 | * |
||
| 3273 | * @psalm-return static<TKey,T> |
||
| 3274 | */ |
||
| 3275 | 6 | public function getRandomValues(int $number): self |
|
| 3279 | |||
| 3280 | /** |
||
| 3281 | * Gets all values. |
||
| 3282 | * |
||
| 3283 | * @return static |
||
| 3284 | * <p>The values of all elements in this array, in the order they |
||
| 3285 | * appear in the array.</p> |
||
| 3286 | * |
||
| 3287 | * @psalm-return static<TKey,T> |
||
| 3288 | */ |
||
| 3289 | 4 | public function getValues() |
|
| 3299 | |||
| 3300 | /** |
||
| 3301 | * Gets all values via Generator. |
||
| 3302 | * |
||
| 3303 | * @return \Generator |
||
| 3304 | * <p>The values of all elements in this array, in the order they |
||
| 3305 | * appear in the array as Generator.</p> |
||
| 3306 | * |
||
| 3307 | * @psalm-return \Generator<TKey,T> |
||
| 3308 | */ |
||
| 3309 | 4 | public function getValuesYield(): \Generator |
|
| 3313 | |||
| 3314 | /** |
||
| 3315 | * Group values from a array according to the results of a closure. |
||
| 3316 | * |
||
| 3317 | * @param callable|string $grouper <p>A callable function name.</p> |
||
| 3318 | * @param bool $saveKeys |
||
| 3319 | * |
||
| 3320 | * @return static |
||
| 3321 | * <p>(Immutable)</p> |
||
| 3322 | * |
||
| 3323 | * @psalm-return static<TKey,T> |
||
| 3324 | * @psalm-mutation-free |
||
| 3325 | */ |
||
| 3326 | 4 | public function group($grouper, bool $saveKeys = false): self |
|
| 3367 | |||
| 3368 | /** |
||
| 3369 | * Check if an array has a given key. |
||
| 3370 | * |
||
| 3371 | * @param mixed $key |
||
| 3372 | * |
||
| 3373 | * @return bool |
||
| 3374 | */ |
||
| 3375 | 30 | public function has($key): bool |
|
| 3401 | |||
| 3402 | /** |
||
| 3403 | * Check if an array has a given value. |
||
| 3404 | * |
||
| 3405 | * INFO: If you need to search recursive please use ```contains($value, true)```. |
||
| 3406 | * |
||
| 3407 | * @param mixed $value |
||
| 3408 | * |
||
| 3409 | * @return bool |
||
| 3410 | */ |
||
| 3411 | 1 | public function hasValue($value): bool |
|
| 3415 | |||
| 3416 | /** |
||
| 3417 | * Implodes the values of this array. |
||
| 3418 | * |
||
| 3419 | * EXAMPLE: <code> |
||
| 3420 | * a([0 => -9, 1, 2])->implode('|'); // '-9|1|2' |
||
| 3421 | * </code> |
||
| 3422 | * |
||
| 3423 | * @param string $glue |
||
| 3424 | * @param string $prefix |
||
| 3425 | * |
||
| 3426 | * @return string |
||
| 3427 | * @psalm-mutation-free |
||
| 3428 | */ |
||
| 3429 | 28 | public function implode(string $glue = '', string $prefix = ''): string |
|
| 3433 | |||
| 3434 | /** |
||
| 3435 | * Implodes the keys of this array. |
||
| 3436 | * |
||
| 3437 | * @param string $glue |
||
| 3438 | * |
||
| 3439 | * @return string |
||
| 3440 | * @psalm-mutation-free |
||
| 3441 | */ |
||
| 3442 | 8 | public function implodeKeys(string $glue = ''): string |
|
| 3446 | |||
| 3447 | /** |
||
| 3448 | * Given a list and an iterate-function that returns |
||
| 3449 | * a key for each element in the list (or a property name), |
||
| 3450 | * returns an object with an index of each item. |
||
| 3451 | * |
||
| 3452 | * @param mixed $key |
||
| 3453 | * |
||
| 3454 | * @return static |
||
| 3455 | * <p>(Immutable)</p> |
||
| 3456 | * |
||
| 3457 | * @psalm-return static<TKey,T> |
||
| 3458 | * @psalm-mutation-free |
||
| 3459 | */ |
||
| 3460 | 4 | View Code Duplication | public function indexBy($key): self |
| 3477 | |||
| 3478 | /** |
||
| 3479 | * alias: for "Arrayy->searchIndex()" |
||
| 3480 | * |
||
| 3481 | * @param mixed $value <p>The value to search for.</p> |
||
| 3482 | * |
||
| 3483 | * @return false|mixed |
||
| 3484 | * |
||
| 3485 | * @see Arrayy::searchIndex() |
||
| 3486 | */ |
||
| 3487 | 4 | public function indexOf($value) |
|
| 3491 | |||
| 3492 | /** |
||
| 3493 | * Get everything but the last..$to items. |
||
| 3494 | * |
||
| 3495 | * EXAMPLE: <code> |
||
| 3496 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->initial(2); // Arrayy[0 => 'foo'] |
||
| 3497 | * </code> |
||
| 3498 | * |
||
| 3499 | * @param int $to |
||
| 3500 | * |
||
| 3501 | * @return static |
||
| 3502 | * <p>(Immutable)</p> |
||
| 3503 | * |
||
| 3504 | * @psalm-return static<TKey,T> |
||
| 3505 | * @psalm-mutation-free |
||
| 3506 | */ |
||
| 3507 | 12 | public function initial(int $to = 1): self |
|
| 3511 | |||
| 3512 | /** |
||
| 3513 | * Return an array with all elements found in input array. |
||
| 3514 | * |
||
| 3515 | * EXAMPLE: <code> |
||
| 3516 | * a(['foo', 'bar'])->intersection(['bar', 'baz']); // Arrayy['bar'] |
||
| 3517 | * </code> |
||
| 3518 | * |
||
| 3519 | * @param array $search |
||
| 3520 | * @param bool $keepKeys |
||
| 3521 | * |
||
| 3522 | * @return static |
||
| 3523 | * <p>(Immutable)</p> |
||
| 3524 | * |
||
| 3525 | * @psalm-param array<mixed,mixed>|array<TKey,T> $search |
||
| 3526 | * @psalm-return static<TKey,T> |
||
| 3527 | * @psalm-mutation-free |
||
| 3528 | */ |
||
| 3529 | 4 | public function intersection(array $search, bool $keepKeys = false): self |
|
| 3555 | |||
| 3556 | /** |
||
| 3557 | * Return an array with all elements found in input array. |
||
| 3558 | * |
||
| 3559 | * @param array ...$array |
||
| 3560 | * |
||
| 3561 | * @return static |
||
| 3562 | * <p>(Immutable)</p> |
||
| 3563 | * |
||
| 3564 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$array |
||
| 3565 | * @psalm-return static<TKey,T> |
||
| 3566 | * @psalm-mutation-free |
||
| 3567 | */ |
||
| 3568 | 1 | public function intersectionMulti(...$array): self |
|
| 3576 | |||
| 3577 | /** |
||
| 3578 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 3579 | * |
||
| 3580 | * EXAMPLE: <code> |
||
| 3581 | * a(['foo', 'bar'])->intersects(['föö', 'bär']); // false |
||
| 3582 | * </code> |
||
| 3583 | * |
||
| 3584 | * @param array $search |
||
| 3585 | * |
||
| 3586 | * @return bool |
||
| 3587 | * |
||
| 3588 | * @psalm-param array<mixed,mixed>|array<TKey,T> $search |
||
| 3589 | */ |
||
| 3590 | 1 | public function intersects(array $search): bool |
|
| 3594 | |||
| 3595 | /** |
||
| 3596 | * Invoke a function on all of an array's values. |
||
| 3597 | * |
||
| 3598 | * @param callable $callable |
||
| 3599 | * @param mixed $arguments |
||
| 3600 | * |
||
| 3601 | * @return static |
||
| 3602 | * <p>(Immutable)</p> |
||
| 3603 | * |
||
| 3604 | * @psalm-param callable(T=,mixed):mixed $callable |
||
| 3605 | * @psalm-return static<TKey,T> |
||
| 3606 | * @psalm-mutation-free |
||
| 3607 | */ |
||
| 3608 | 1 | public function invoke($callable, $arguments = []): self |
|
| 3632 | |||
| 3633 | /** |
||
| 3634 | * Check whether array is associative or not. |
||
| 3635 | * |
||
| 3636 | * EXAMPLE: <code> |
||
| 3637 | * a(['foo' => 'bar', 2, 3])->isAssoc(); // true |
||
| 3638 | * </code> |
||
| 3639 | * |
||
| 3640 | * @param bool $recursive |
||
| 3641 | * |
||
| 3642 | * @return bool |
||
| 3643 | * <p>Returns true if associative, false otherwise.</p> |
||
| 3644 | */ |
||
| 3645 | 15 | View Code Duplication | public function isAssoc(bool $recursive = false): bool |
| 3660 | |||
| 3661 | /** |
||
| 3662 | * Check if a given key or keys are empty. |
||
| 3663 | * |
||
| 3664 | * @param int|int[]|string|string[]|null $keys |
||
| 3665 | * |
||
| 3666 | * @return bool |
||
| 3667 | * <p>Returns true if empty, false otherwise.</p> |
||
| 3668 | * @psalm-mutation-free |
||
| 3669 | */ |
||
| 3670 | 45 | public function isEmpty($keys = null): bool |
|
| 3688 | |||
| 3689 | /** |
||
| 3690 | * Check if the current array is equal to the given "$array" or not. |
||
| 3691 | * |
||
| 3692 | * EXAMPLE: <code> |
||
| 3693 | * a(['💩'])->isEqual(['💩']); // true |
||
| 3694 | * </code> |
||
| 3695 | * |
||
| 3696 | * @param array $array |
||
| 3697 | * |
||
| 3698 | * @return bool |
||
| 3699 | * |
||
| 3700 | * @psalm-param array<mixed,mixed> $array |
||
| 3701 | */ |
||
| 3702 | 1 | public function isEqual(array $array): bool |
|
| 3706 | |||
| 3707 | /** |
||
| 3708 | * Check if the current array is a multi-array. |
||
| 3709 | * |
||
| 3710 | * EXAMPLE: <code> |
||
| 3711 | * a(['foo' => [1, 2 , 3]])->isMultiArray(); // true |
||
| 3712 | * </code> |
||
| 3713 | * |
||
| 3714 | * @return bool |
||
| 3715 | */ |
||
| 3716 | 22 | public function isMultiArray(): bool |
|
| 3726 | |||
| 3727 | /** |
||
| 3728 | * Check whether array is numeric or not. |
||
| 3729 | * |
||
| 3730 | * @return bool |
||
| 3731 | * <p>Returns true if numeric, false otherwise.</p> |
||
| 3732 | */ |
||
| 3733 | 5 | View Code Duplication | public function isNumeric(): bool |
| 3748 | |||
| 3749 | /** |
||
| 3750 | * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not. |
||
| 3751 | * |
||
| 3752 | * EXAMPLE: <code> |
||
| 3753 | * a([0 => 'foo', 1 => 'lall', 2 => 'foobar'])->isSequential(); // true |
||
| 3754 | * </code> |
||
| 3755 | * |
||
| 3756 | * INFO: If the array is empty we count it as non-sequential. |
||
| 3757 | * |
||
| 3758 | * @param bool $recursive |
||
| 3759 | * |
||
| 3760 | * @return bool |
||
| 3761 | * @psalm-mutation-free |
||
| 3762 | */ |
||
| 3763 | 10 | public function isSequential(bool $recursive = false): bool |
|
| 3793 | |||
| 3794 | /** |
||
| 3795 | * @return array |
||
| 3796 | * |
||
| 3797 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 3798 | */ |
||
| 3799 | 2 | public function jsonSerialize(): array |
|
| 3803 | |||
| 3804 | /** |
||
| 3805 | * Gets the key/index of the element at the current internal iterator position. |
||
| 3806 | * |
||
| 3807 | * @return int|string|null |
||
| 3808 | */ |
||
| 3809 | public function key() |
||
| 3813 | |||
| 3814 | /** |
||
| 3815 | * Checks if the given key exists in the provided array. |
||
| 3816 | * |
||
| 3817 | * INFO: This method only use "array_key_exists()" if you want to use "dot"-notation, |
||
| 3818 | * then you need to use "Arrayy->offsetExists()". |
||
| 3819 | * |
||
| 3820 | * @param int|string $key the key to look for |
||
| 3821 | * |
||
| 3822 | * @return bool |
||
| 3823 | * @psalm-mutation-free |
||
| 3824 | */ |
||
| 3825 | 174 | public function keyExists($key): bool |
|
| 3835 | |||
| 3836 | /** |
||
| 3837 | * Get all keys from the current array. |
||
| 3838 | * |
||
| 3839 | * EXAMPLE: <code> |
||
| 3840 | * a([1 => 'foo', 2 => 'foo2', 3 => 'bar'])->keys(); // Arrayy[1, 2, 3] |
||
| 3841 | * </code> |
||
| 3842 | * |
||
| 3843 | * @param bool $recursive [optional] <p> |
||
| 3844 | * Get all keys, also from all sub-arrays from an multi-dimensional array. |
||
| 3845 | * </p> |
||
| 3846 | * @param mixed|null $search_values [optional] <p> |
||
| 3847 | * If specified, then only keys containing these values are returned. |
||
| 3848 | * </p> |
||
| 3849 | * @param bool $strict [optional] <p> |
||
| 3850 | * Determines if strict comparison (===) should be used during the search. |
||
| 3851 | * </p> |
||
| 3852 | * |
||
| 3853 | * @return static |
||
| 3854 | * <p>(Immutable) An array of all the keys in input.</p> |
||
| 3855 | * |
||
| 3856 | * @psalm-return static<array-key,TKey> |
||
| 3857 | * @psalm-mutation-free |
||
| 3858 | */ |
||
| 3859 | 29 | public function keys( |
|
| 3930 | |||
| 3931 | /** |
||
| 3932 | * Sort an array by key in reverse order. |
||
| 3933 | * |
||
| 3934 | * @param int $sort_flags [optional] <p> |
||
| 3935 | * You may modify the behavior of the sort using the optional |
||
| 3936 | * parameter sort_flags, for details |
||
| 3937 | * see sort. |
||
| 3938 | * </p> |
||
| 3939 | * |
||
| 3940 | * @return $this |
||
| 3941 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 3942 | * |
||
| 3943 | * @psalm-return static<TKey,T> |
||
| 3944 | */ |
||
| 3945 | 4 | public function krsort(int $sort_flags = 0): self |
|
| 3953 | |||
| 3954 | /** |
||
| 3955 | * Sort an array by key in reverse order. |
||
| 3956 | * |
||
| 3957 | * @param int $sort_flags [optional] <p> |
||
| 3958 | * You may modify the behavior of the sort using the optional |
||
| 3959 | * parameter sort_flags, for details |
||
| 3960 | * see sort. |
||
| 3961 | * </p> |
||
| 3962 | * |
||
| 3963 | * @return $this |
||
| 3964 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 3965 | * |
||
| 3966 | * @psalm-return static<TKey,T> |
||
| 3967 | * @psalm-mutation-free |
||
| 3968 | */ |
||
| 3969 | 4 | public function krsortImmutable(int $sort_flags = 0): self |
|
| 3980 | |||
| 3981 | /** |
||
| 3982 | * Get the last value from the current array. |
||
| 3983 | * |
||
| 3984 | * EXAMPLE: <code> |
||
| 3985 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->last(); // 'lall' |
||
| 3986 | * </code> |
||
| 3987 | * |
||
| 3988 | * @return mixed|null |
||
| 3989 | * <p>Return null if there wasn't a element.</p> |
||
| 3990 | * @psalm-mutation-free |
||
| 3991 | */ |
||
| 3992 | 17 | public function last() |
|
| 4001 | |||
| 4002 | /** |
||
| 4003 | * Get the last key from the current array. |
||
| 4004 | * |
||
| 4005 | * @return mixed|null |
||
| 4006 | * <p>Return null if there wasn't a element.</p> |
||
| 4007 | * @psalm-mutation-free |
||
| 4008 | */ |
||
| 4009 | 21 | public function lastKey() |
|
| 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 static |
||
| 4026 | * <p>(Immutable)</p> |
||
| 4027 | * |
||
| 4028 | * @psalm-return static<TKey,T> |
||
| 4029 | * @psalm-mutation-free |
||
| 4030 | */ |
||
| 4031 | 13 | public function lastsImmutable(int $number = null): self |
|
| 4061 | |||
| 4062 | /** |
||
| 4063 | * Get the last value(s) from the current array. |
||
| 4064 | * |
||
| 4065 | * EXAMPLE: <code> |
||
| 4066 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall'] |
||
| 4067 | * </code> |
||
| 4068 | * |
||
| 4069 | * @param int|null $number |
||
| 4070 | * |
||
| 4071 | * @return $this |
||
| 4072 | * <p>(Mutable)</p> |
||
| 4073 | * |
||
| 4074 | * @psalm-return static<TKey,T> |
||
| 4075 | */ |
||
| 4076 | 13 | public function lastsMutable(int $number = null): self |
|
| 4087 | |||
| 4088 | /** |
||
| 4089 | * Count the values from the current array. |
||
| 4090 | * |
||
| 4091 | * alias: for "Arrayy->count()" |
||
| 4092 | * |
||
| 4093 | * @param int $mode |
||
| 4094 | * |
||
| 4095 | * @return int |
||
| 4096 | * |
||
| 4097 | * @see Arrayy::count() |
||
| 4098 | */ |
||
| 4099 | 20 | public function length(int $mode = \COUNT_NORMAL): int |
|
| 4103 | |||
| 4104 | /** |
||
| 4105 | * Apply the given function to the every element of the array, |
||
| 4106 | * collecting the results. |
||
| 4107 | * |
||
| 4108 | * EXAMPLE: <code> |
||
| 4109 | * a(['foo', 'Foo'])->map('mb_strtoupper'); // Arrayy['FOO', 'FOO'] |
||
| 4110 | * </code> |
||
| 4111 | * |
||
| 4112 | * @param callable $callable |
||
| 4113 | * @param bool $useKeyAsSecondParameter |
||
| 4114 | * @param mixed ...$arguments |
||
| 4115 | * |
||
| 4116 | * @return static |
||
| 4117 | * <p>(Immutable) Arrayy object with modified elements.</p> |
||
| 4118 | * |
||
| 4119 | * @psalm-param callable(T,TKey=,mixed=):mixed $callable |
||
| 4120 | * @psalm-return static<TKey,T> |
||
| 4121 | * @psalm-mutation-free |
||
| 4122 | */ |
||
| 4123 | 6 | public function map( |
|
| 4156 | |||
| 4157 | /** |
||
| 4158 | * Check if all items in current array match a truth test. |
||
| 4159 | * |
||
| 4160 | * EXAMPLE: <code> |
||
| 4161 | * $closure = function ($value, $key) { |
||
| 4162 | * return ($value % 2 === 0); |
||
| 4163 | * }; |
||
| 4164 | * a([2, 4, 8])->matches($closure); // true |
||
| 4165 | * </code> |
||
| 4166 | * |
||
| 4167 | * @param \Closure $closure |
||
| 4168 | * |
||
| 4169 | * @return bool |
||
| 4170 | */ |
||
| 4171 | 15 | View Code Duplication | public function matches(\Closure $closure): bool |
| 4187 | |||
| 4188 | /** |
||
| 4189 | * Check if any item in the current array matches a truth test. |
||
| 4190 | * |
||
| 4191 | * EXAMPLE: <code> |
||
| 4192 | * $closure = function ($value, $key) { |
||
| 4193 | * return ($value % 2 === 0); |
||
| 4194 | * }; |
||
| 4195 | * a([1, 4, 7])->matches($closure); // true |
||
| 4196 | * </code> |
||
| 4197 | * |
||
| 4198 | * @param \Closure $closure |
||
| 4199 | * |
||
| 4200 | * @return bool |
||
| 4201 | */ |
||
| 4202 | 14 | View Code Duplication | public function matchesAny(\Closure $closure): bool |
| 4218 | |||
| 4219 | /** |
||
| 4220 | * Get the max value from an array. |
||
| 4221 | * |
||
| 4222 | * EXAMPLE: <code> |
||
| 4223 | * a([-9, -8, -7, 1.32])->max(); // 1.32 |
||
| 4224 | * </code> |
||
| 4225 | * |
||
| 4226 | * @return false|mixed |
||
| 4227 | * <p>Will return false if there are no values.</p> |
||
| 4228 | */ |
||
| 4229 | 10 | View Code Duplication | public function max() |
| 4249 | |||
| 4250 | /** |
||
| 4251 | * Merge the new $array into the current array. |
||
| 4252 | * |
||
| 4253 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 4254 | * |
||
| 4255 | * EXAMPLE: <code> |
||
| 4256 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4257 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4258 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[1 => 'one', 'foo' => 'bar2', 3 => 'three'] |
||
| 4259 | * // --- |
||
| 4260 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4261 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4262 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2'] |
||
| 4263 | * </code> |
||
| 4264 | * |
||
| 4265 | * @param array $array |
||
| 4266 | * @param bool $recursive |
||
| 4267 | * |
||
| 4268 | * @return static |
||
| 4269 | * <p>(Immutable)</p> |
||
| 4270 | * |
||
| 4271 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4272 | * @psalm-return static<int|TKey,T> |
||
| 4273 | * @psalm-mutation-free |
||
| 4274 | */ |
||
| 4275 | 33 | View Code Duplication | public function mergeAppendKeepIndex(array $array = [], bool $recursive = false): self |
| 4290 | |||
| 4291 | /** |
||
| 4292 | * Merge the new $array into the current array. |
||
| 4293 | * |
||
| 4294 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
| 4295 | * - create new indexes |
||
| 4296 | * |
||
| 4297 | * EXAMPLE: <code> |
||
| 4298 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4299 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4300 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 'foo' => 'bar2', 1 => 'three'] |
||
| 4301 | * // --- |
||
| 4302 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4303 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4304 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 1 => 'foo', 2 => 'foo', 3 => 'bar2'] |
||
| 4305 | * </code> |
||
| 4306 | * |
||
| 4307 | * @param array $array |
||
| 4308 | * @param bool $recursive |
||
| 4309 | * |
||
| 4310 | * @return static |
||
| 4311 | * <p>(Immutable)</p> |
||
| 4312 | * |
||
| 4313 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4314 | * @psalm-return static<TKey,T> |
||
| 4315 | * @psalm-mutation-free |
||
| 4316 | */ |
||
| 4317 | 20 | View Code Duplication | public function mergeAppendNewIndex(array $array = [], bool $recursive = false): self |
| 4332 | |||
| 4333 | /** |
||
| 4334 | * Merge the the current array into the $array. |
||
| 4335 | * |
||
| 4336 | * - use key,value from the new $array, also if the index is in the current array |
||
| 4337 | * |
||
| 4338 | * EXAMPLE: <code> |
||
| 4339 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4340 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4341 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy['foo' => 'bar1', 3 => 'three', 1 => 'one'] |
||
| 4342 | * // --- |
||
| 4343 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4344 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4345 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy[0 => 'one', 1 => 'foo'] |
||
| 4346 | * </code> |
||
| 4347 | * |
||
| 4348 | * @param array $array |
||
| 4349 | * @param bool $recursive |
||
| 4350 | * |
||
| 4351 | * @return static |
||
| 4352 | * <p>(Immutable)</p> |
||
| 4353 | * |
||
| 4354 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4355 | * @psalm-return static<TKey,T> |
||
| 4356 | * @psalm-mutation-free |
||
| 4357 | */ |
||
| 4358 | 17 | View Code Duplication | public function mergePrependKeepIndex(array $array = [], bool $recursive = false): self |
| 4373 | |||
| 4374 | /** |
||
| 4375 | * Merge the current array into the new $array. |
||
| 4376 | * |
||
| 4377 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
| 4378 | * - create new indexes |
||
| 4379 | * |
||
| 4380 | * EXAMPLE: <code> |
||
| 4381 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4382 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4383 | * a($array1)->mergePrependNewIndex($array2); // Arrayy['foo' => 'bar1', 0 => 'three', 1 => 'one'] |
||
| 4384 | * // --- |
||
| 4385 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4386 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4387 | * a($array1)->mergePrependNewIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2', 2 => 'one', 3 => 'foo'] |
||
| 4388 | * </code> |
||
| 4389 | * |
||
| 4390 | * @param array $array |
||
| 4391 | * @param bool $recursive |
||
| 4392 | * |
||
| 4393 | * @return static |
||
| 4394 | * <p>(Immutable)</p> |
||
| 4395 | * |
||
| 4396 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 4397 | * @psalm-return static<TKey,T> |
||
| 4398 | * @psalm-mutation-free |
||
| 4399 | */ |
||
| 4400 | 21 | View Code Duplication | public function mergePrependNewIndex(array $array = [], bool $recursive = false): self |
| 4415 | |||
| 4416 | /** |
||
| 4417 | * @return ArrayyMeta|mixed|static |
||
| 4418 | */ |
||
| 4419 | 18 | public static function meta() |
|
| 4423 | |||
| 4424 | /** |
||
| 4425 | * Get the min value from an array. |
||
| 4426 | * |
||
| 4427 | * EXAMPLE: <code> |
||
| 4428 | * a([-9, -8, -7, 1.32])->min(); // -9 |
||
| 4429 | * </code> |
||
| 4430 | * |
||
| 4431 | * @return false|mixed |
||
| 4432 | * <p>Will return false if there are no values.</p> |
||
| 4433 | */ |
||
| 4434 | 10 | View Code Duplication | public function min() |
| 4454 | |||
| 4455 | /** |
||
| 4456 | * Get the most used value from the array. |
||
| 4457 | * |
||
| 4458 | * @return mixed|null |
||
| 4459 | * <p>(Immutable) Return null if there wasn't a element.</p> |
||
| 4460 | * @psalm-mutation-free |
||
| 4461 | */ |
||
| 4462 | 3 | public function mostUsedValue() |
|
| 4466 | |||
| 4467 | /** |
||
| 4468 | * Get the most used value from the array. |
||
| 4469 | * |
||
| 4470 | * @param int|null $number <p>How many values you will take?</p> |
||
| 4471 | * |
||
| 4472 | * @return static |
||
| 4473 | * <p>(Immutable)</p> |
||
| 4474 | * |
||
| 4475 | * @psalm-return static<TKey,T> |
||
| 4476 | * @psalm-mutation-free |
||
| 4477 | */ |
||
| 4478 | 3 | public function mostUsedValues(int $number = null): self |
|
| 4482 | |||
| 4483 | /** |
||
| 4484 | * Move an array element to a new index. |
||
| 4485 | * |
||
| 4486 | * EXAMPLE: <code> |
||
| 4487 | * $arr2 = new A(['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e']); |
||
| 4488 | * $newArr2 = $arr2->moveElement('D', 1); // Arrayy['A' => 'a', 'D' => 'd', 'B' => 'b', 'C' => 'c', 'E' => 'e'] |
||
| 4489 | * </code> |
||
| 4490 | * |
||
| 4491 | * @param int|string $from |
||
| 4492 | * @param int $to |
||
| 4493 | * |
||
| 4494 | * @return static |
||
| 4495 | * <p>(Immutable)</p> |
||
| 4496 | * |
||
| 4497 | * @psalm-return static<TKey,T> |
||
| 4498 | * @psalm-mutation-free |
||
| 4499 | */ |
||
| 4500 | 1 | public function moveElement($from, $to): self |
|
| 4533 | |||
| 4534 | /** |
||
| 4535 | * Move an array element to the first place. |
||
| 4536 | * |
||
| 4537 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4538 | * loss the keys of an indexed array. |
||
| 4539 | * |
||
| 4540 | * @param int|string $key |
||
| 4541 | * |
||
| 4542 | * @return static |
||
| 4543 | * <p>(Immutable)</p> |
||
| 4544 | * |
||
| 4545 | * @psalm-return static<TKey,T> |
||
| 4546 | * @psalm-mutation-free |
||
| 4547 | */ |
||
| 4548 | 1 | View Code Duplication | public function moveElementToFirstPlace($key): self |
| 4564 | |||
| 4565 | /** |
||
| 4566 | * Move an array element to the last place. |
||
| 4567 | * |
||
| 4568 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4569 | * loss the keys of an indexed array. |
||
| 4570 | * |
||
| 4571 | * @param int|string $key |
||
| 4572 | * |
||
| 4573 | * @return static |
||
| 4574 | * <p>(Immutable)</p> |
||
| 4575 | * |
||
| 4576 | * @psalm-return static<TKey,T> |
||
| 4577 | * @psalm-mutation-free |
||
| 4578 | */ |
||
| 4579 | 1 | View Code Duplication | public function moveElementToLastPlace($key): self |
| 4595 | |||
| 4596 | /** |
||
| 4597 | * Moves the internal iterator position to the next element and returns this element. |
||
| 4598 | * |
||
| 4599 | * @return false|mixed |
||
| 4600 | * <p>(Mutable) Will return false if there are no values.</p> |
||
| 4601 | */ |
||
| 4602 | public function next() |
||
| 4606 | |||
| 4607 | /** |
||
| 4608 | * Get the next nth keys and values from the array. |
||
| 4609 | * |
||
| 4610 | * @param int $step |
||
| 4611 | * @param int $offset |
||
| 4612 | * |
||
| 4613 | * @return static |
||
| 4614 | * <p>(Immutable)</p> |
||
| 4615 | * |
||
| 4616 | * @psalm-return static<TKey,T> |
||
| 4617 | * @psalm-mutation-free |
||
| 4618 | */ |
||
| 4619 | 1 | public function nth(int $step, int $offset = 0): self |
|
| 4638 | |||
| 4639 | /** |
||
| 4640 | * Get a subset of the items from the given array. |
||
| 4641 | * |
||
| 4642 | * @param int[]|string[] $keys |
||
| 4643 | * |
||
| 4644 | * @return static |
||
| 4645 | * <p>(Immutable)</p> |
||
| 4646 | * |
||
| 4647 | * @psalm-param array-key[] $keys |
||
| 4648 | * @psalm-return static<TKey,T> |
||
| 4649 | * @psalm-mutation-free |
||
| 4650 | */ |
||
| 4651 | 1 | View Code Duplication | public function only(array $keys): self |
| 4669 | |||
| 4670 | /** |
||
| 4671 | * Pad array to the specified size with a given value. |
||
| 4672 | * |
||
| 4673 | * @param int $size <p>Size of the result array.</p> |
||
| 4674 | * @param mixed $value <p>Empty value by default.</p> |
||
| 4675 | * |
||
| 4676 | * @return static |
||
| 4677 | * <p>(Immutable) Arrayy object padded to $size with $value.</p> |
||
| 4678 | * |
||
| 4679 | * @psalm-return static<TKey,T> |
||
| 4680 | * @psalm-mutation-free |
||
| 4681 | */ |
||
| 4682 | 5 | public function pad(int $size, $value): self |
|
| 4690 | |||
| 4691 | /** |
||
| 4692 | * Partitions this array in two array according to a predicate. |
||
| 4693 | * Keys are preserved in the resulting array. |
||
| 4694 | * |
||
| 4695 | * @param \Closure $closure |
||
| 4696 | * <p>The predicate on which to partition.</p> |
||
| 4697 | * |
||
| 4698 | * @return array<int, static> |
||
| 4699 | * <p>An array with two elements. The first element contains the array |
||
| 4700 | * of elements where the predicate returned TRUE, the second element |
||
| 4701 | * contains the array of elements where the predicate returned FALSE.</p> |
||
| 4702 | * |
||
| 4703 | * @psalm-return array<int, static<TKey,T>> |
||
| 4704 | */ |
||
| 4705 | 1 | public function partition(\Closure $closure): array |
|
| 4721 | |||
| 4722 | /** |
||
| 4723 | * Pop a specified value off the end of the current array. |
||
| 4724 | * |
||
| 4725 | * @return mixed|null |
||
| 4726 | * <p>(Mutable) The popped element from the current array or null if the array is e.g. empty.</p> |
||
| 4727 | */ |
||
| 4728 | 5 | public function pop() |
|
| 4734 | |||
| 4735 | /** |
||
| 4736 | * Prepend a (key) + value to the current array. |
||
| 4737 | * |
||
| 4738 | * EXAMPLE: <code> |
||
| 4739 | * a(['fòô' => 'bàř'])->prepend('foo'); // Arrayy[0 => 'foo', 'fòô' => 'bàř'] |
||
| 4740 | * </code> |
||
| 4741 | * |
||
| 4742 | * @param mixed $value |
||
| 4743 | * @param mixed $key |
||
| 4744 | * |
||
| 4745 | * @return $this |
||
| 4746 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4747 | * |
||
| 4748 | * @psalm-return static<TKey,T> |
||
| 4749 | */ |
||
| 4750 | 11 | public function prepend($value, $key = null) |
|
| 4766 | |||
| 4767 | /** |
||
| 4768 | * Prepend a (key) + value to the current array. |
||
| 4769 | * |
||
| 4770 | * EXAMPLE: <code> |
||
| 4771 | * a(['fòô' => 'bàř'])->prependImmutable('foo')->getArray(); // [0 => 'foo', 'fòô' => 'bàř'] |
||
| 4772 | * </code> |
||
| 4773 | * |
||
| 4774 | * @param mixed $value |
||
| 4775 | * @param mixed $key |
||
| 4776 | * |
||
| 4777 | * @return $this |
||
| 4778 | * <p>(Immutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4779 | * |
||
| 4780 | * @psalm-return static<TKey,T> |
||
| 4781 | * @psalm-mutation-free |
||
| 4782 | */ |
||
| 4783 | 1 | View Code Duplication | public function prependImmutable($value, $key = null) |
| 4808 | |||
| 4809 | /** |
||
| 4810 | * Add a suffix to each key. |
||
| 4811 | * |
||
| 4812 | * @param mixed $suffix |
||
| 4813 | * |
||
| 4814 | * @return static |
||
| 4815 | * <p>(Immutable) Return an Arrayy object, with the prepended keys.</p> |
||
| 4816 | * |
||
| 4817 | * @psalm-return static<TKey,T> |
||
| 4818 | * @psalm-mutation-free |
||
| 4819 | */ |
||
| 4820 | 10 | View Code Duplication | public function prependToEachKey($suffix): self |
| 4846 | |||
| 4847 | /** |
||
| 4848 | * Add a suffix to each value. |
||
| 4849 | * |
||
| 4850 | * @param mixed $suffix |
||
| 4851 | * |
||
| 4852 | * @return static |
||
| 4853 | * <p>(Immutable) Return an Arrayy object, with the prepended values.</p> |
||
| 4854 | * |
||
| 4855 | * @psalm-return static<TKey,T> |
||
| 4856 | * @psalm-mutation-free |
||
| 4857 | */ |
||
| 4858 | 10 | View Code Duplication | public function prependToEachValue($suffix): self |
| 4886 | |||
| 4887 | /** |
||
| 4888 | * Return the value of a given key and |
||
| 4889 | * delete the key. |
||
| 4890 | * |
||
| 4891 | * @param int|int[]|string|string[]|null $keyOrKeys |
||
| 4892 | * @param mixed $fallback |
||
| 4893 | * |
||
| 4894 | * @return mixed |
||
| 4895 | */ |
||
| 4896 | 6 | public function pull($keyOrKeys = null, $fallback = null) |
|
| 4918 | |||
| 4919 | /** |
||
| 4920 | * Push one or more values onto the end of array at once. |
||
| 4921 | * |
||
| 4922 | * @param mixed ...$args |
||
| 4923 | * |
||
| 4924 | * @return $this |
||
| 4925 | * <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p> |
||
| 4926 | * |
||
| 4927 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 4928 | * |
||
| 4929 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$args |
||
| 4930 | * @psalm-return static<TKey,T> |
||
| 4931 | */ |
||
| 4932 | 9 | View Code Duplication | public function push(...$args) |
| 4950 | |||
| 4951 | /** |
||
| 4952 | * Get a random value from the current array. |
||
| 4953 | * |
||
| 4954 | * EXAMPLE: <code> |
||
| 4955 | * a([1, 2, 3, 4])->randomImmutable(2); // e.g.: Arrayy[1, 4] |
||
| 4956 | * </code> |
||
| 4957 | * |
||
| 4958 | * @param int|null $number <p>How many values you will take?</p> |
||
| 4959 | * |
||
| 4960 | * @return static |
||
| 4961 | * <p>(Immutable)</p> |
||
| 4962 | * |
||
| 4963 | * @psalm-return static<int|array-key,T> |
||
| 4964 | */ |
||
| 4965 | 19 | public function randomImmutable(int $number = null): self |
|
| 4998 | |||
| 4999 | /** |
||
| 5000 | * Pick a random key/index from the keys of this array. |
||
| 5001 | * |
||
| 5002 | * EXAMPLE: <code> |
||
| 5003 | * $arrayy = A::create([1 => 'one', 2 => 'two']); |
||
| 5004 | * $arrayy->randomKey(); // e.g. 2 |
||
| 5005 | * </code> |
||
| 5006 | * |
||
| 5007 | * @throws \RangeException If array is empty |
||
| 5008 | * |
||
| 5009 | * @return mixed |
||
| 5010 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 5011 | */ |
||
| 5012 | 4 | public function randomKey() |
|
| 5022 | |||
| 5023 | /** |
||
| 5024 | * Pick a given number of random keys/indexes out of this array. |
||
| 5025 | * |
||
| 5026 | * EXAMPLE: <code> |
||
| 5027 | * a([1 => 'one', 2 => 'two'])->randomKeys(); // e.g. Arrayy[1, 2] |
||
| 5028 | * </code> |
||
| 5029 | * |
||
| 5030 | * @param int $number <p>The number of keys/indexes (should be <= \count($this->array))</p> |
||
| 5031 | * |
||
| 5032 | * @throws \RangeException If array is empty |
||
| 5033 | * |
||
| 5034 | * @return static |
||
| 5035 | * <p>(Immutable)</p> |
||
| 5036 | * |
||
| 5037 | * @psalm-return static<TKey,T> |
||
| 5038 | */ |
||
| 5039 | 13 | public function randomKeys(int $number): self |
|
| 5067 | |||
| 5068 | /** |
||
| 5069 | * Get a random value from the current array. |
||
| 5070 | * |
||
| 5071 | * EXAMPLE: <code> |
||
| 5072 | * a([1, 2, 3, 4])->randomMutable(2); // e.g.: Arrayy[1, 4] |
||
| 5073 | * </code> |
||
| 5074 | * |
||
| 5075 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5076 | * |
||
| 5077 | * @return $this |
||
| 5078 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5079 | * |
||
| 5080 | * @psalm-return static<TKey,T> |
||
| 5081 | */ |
||
| 5082 | 17 | public function randomMutable(int $number = null): self |
|
| 5107 | |||
| 5108 | /** |
||
| 5109 | * Pick a random value from the values of this array. |
||
| 5110 | * |
||
| 5111 | * EXAMPLE: <code> |
||
| 5112 | * a([1 => 'one', 2 => 'two'])->randomValue(); // e.g. 'one' |
||
| 5113 | * </code> |
||
| 5114 | * |
||
| 5115 | * @return mixed |
||
| 5116 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 5117 | */ |
||
| 5118 | 4 | public function randomValue() |
|
| 5128 | |||
| 5129 | /** |
||
| 5130 | * Pick a given number of random values out of this array. |
||
| 5131 | * |
||
| 5132 | * EXAMPLE: <code> |
||
| 5133 | * a([1 => 'one', 2 => 'two'])->randomValues(); // e.g. Arrayy['one', 'two'] |
||
| 5134 | * </code> |
||
| 5135 | * |
||
| 5136 | * @param int $number |
||
| 5137 | * |
||
| 5138 | * @return static |
||
| 5139 | * <p>(Mutable)</p> |
||
| 5140 | * |
||
| 5141 | * @psalm-return static<TKey,T> |
||
| 5142 | */ |
||
| 5143 | 7 | public function randomValues(int $number): self |
|
| 5147 | |||
| 5148 | /** |
||
| 5149 | * Get a random value from an array, with the ability to skew the results. |
||
| 5150 | * |
||
| 5151 | * EXAMPLE: <code> |
||
| 5152 | * a([0 => 3, 1 => 4])->randomWeighted([1 => 4]); // e.g.: Arrayy[4] (has a 66% chance of returning 4) |
||
| 5153 | * </code> |
||
| 5154 | * |
||
| 5155 | * @param array $array |
||
| 5156 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5157 | * |
||
| 5158 | * @return static<int,mixed> |
||
| 5159 | * <p>(Immutable)</p> |
||
| 5160 | * |
||
| 5161 | * @psalm-param array<mixed,mixed> $array |
||
| 5162 | * @psalm-return static<int|array-key,T> |
||
| 5163 | */ |
||
| 5164 | 9 | public function randomWeighted(array $array, int $number = null): self |
|
| 5179 | |||
| 5180 | /** |
||
| 5181 | * Reduce the current array via callable e.g. anonymous-function and return the end result. |
||
| 5182 | * |
||
| 5183 | * EXAMPLE: <code> |
||
| 5184 | * a([1, 2, 3, 4])->reduce( |
||
| 5185 | * function ($carry, $item) { |
||
| 5186 | * return $carry * $item; |
||
| 5187 | * }, |
||
| 5188 | * 1 |
||
| 5189 | * ); // Arrayy[24] |
||
| 5190 | * </code> |
||
| 5191 | * |
||
| 5192 | * @param callable $callable |
||
| 5193 | * @param mixed $initial |
||
| 5194 | * |
||
| 5195 | * @return static |
||
| 5196 | * <p>(Immutable)</p> |
||
| 5197 | * |
||
| 5198 | * @psalm-return static<TKey,T> |
||
| 5199 | * @psalm-mutation-free |
||
| 5200 | */ |
||
| 5201 | 18 | View Code Duplication | public function reduce($callable, $initial = []): self |
| 5213 | |||
| 5214 | /** |
||
| 5215 | * @param bool $unique |
||
| 5216 | * |
||
| 5217 | * @return static |
||
| 5218 | * <p>(Immutable)</p> |
||
| 5219 | * |
||
| 5220 | * @psalm-return static<TKey,T> |
||
| 5221 | * @psalm-mutation-free |
||
| 5222 | */ |
||
| 5223 | 14 | public function reduce_dimension(bool $unique = true): self |
|
| 5246 | |||
| 5247 | /** |
||
| 5248 | * Create a numerically re-indexed Arrayy object. |
||
| 5249 | * |
||
| 5250 | * EXAMPLE: <code> |
||
| 5251 | * a([2 => 1, 3 => 2])->reindex(); // Arrayy[0 => 1, 1 => 2] |
||
| 5252 | * </code> |
||
| 5253 | * |
||
| 5254 | * @return $this |
||
| 5255 | * <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p> |
||
| 5256 | * |
||
| 5257 | * @psalm-return static<TKey,T> |
||
| 5258 | */ |
||
| 5259 | 9 | public function reindex(): self |
|
| 5267 | |||
| 5268 | /** |
||
| 5269 | * Return all items that fail the truth test. |
||
| 5270 | * |
||
| 5271 | * EXAMPLE: <code> |
||
| 5272 | * $closure = function ($value) { |
||
| 5273 | * return $value % 2 !== 0; |
||
| 5274 | * } |
||
| 5275 | * a([1, 2, 3, 4])->reject($closure); // Arrayy[1 => 2, 3 => 4] |
||
| 5276 | * </code> |
||
| 5277 | * |
||
| 5278 | * @param \Closure $closure |
||
| 5279 | * |
||
| 5280 | * @return static |
||
| 5281 | * <p>(Immutable)</p> |
||
| 5282 | * |
||
| 5283 | * @psalm-return static<TKey,T> |
||
| 5284 | * @psalm-mutation-free |
||
| 5285 | */ |
||
| 5286 | 1 | View Code Duplication | public function reject(\Closure $closure): self |
| 5303 | |||
| 5304 | /** |
||
| 5305 | * Remove a value from the current array (optional using dot-notation). |
||
| 5306 | * |
||
| 5307 | * EXAMPLE: <code> |
||
| 5308 | * a([1 => 'bar', 'foo' => 'foo'])->remove(1); // Arrayy['foo' => 'foo'] |
||
| 5309 | * </code> |
||
| 5310 | * |
||
| 5311 | * @param mixed $key |
||
| 5312 | * |
||
| 5313 | * @return static |
||
| 5314 | * <p>(Mutable)</p> |
||
| 5315 | * |
||
| 5316 | * @psalm-param TKey $key |
||
| 5317 | * @psalm-return static<TKey,T> |
||
| 5318 | */ |
||
| 5319 | 22 | public function remove($key) |
|
| 5342 | |||
| 5343 | /** |
||
| 5344 | * alias: for "Arrayy->removeValue()" |
||
| 5345 | * |
||
| 5346 | * @param mixed $element |
||
| 5347 | * |
||
| 5348 | * @return static |
||
| 5349 | * <p>(Immutable)</p> |
||
| 5350 | * |
||
| 5351 | * @psalm-param T $element |
||
| 5352 | * @psalm-return static<TKey,T> |
||
| 5353 | * @psalm-mutation-free |
||
| 5354 | */ |
||
| 5355 | 8 | public function removeElement($element) |
|
| 5359 | |||
| 5360 | /** |
||
| 5361 | * Remove the first value from the current array. |
||
| 5362 | * |
||
| 5363 | * EXAMPLE: <code> |
||
| 5364 | * a([1 => 'bar', 'foo' => 'foo'])->removeFirst(); // Arrayy['foo' => 'foo'] |
||
| 5365 | * </code> |
||
| 5366 | * |
||
| 5367 | * @return static |
||
| 5368 | * <p>(Immutable)</p> |
||
| 5369 | * |
||
| 5370 | * @psalm-return static<TKey,T> |
||
| 5371 | * @psalm-mutation-free |
||
| 5372 | */ |
||
| 5373 | 7 | View Code Duplication | public function removeFirst(): self |
| 5385 | |||
| 5386 | /** |
||
| 5387 | * Remove the last value from the current array. |
||
| 5388 | * |
||
| 5389 | * EXAMPLE: <code> |
||
| 5390 | * a([1 => 'bar', 'foo' => 'foo'])->removeLast(); // Arrayy[1 => 'bar'] |
||
| 5391 | * </code> |
||
| 5392 | * |
||
| 5393 | * @return static |
||
| 5394 | * <p>(Immutable)</p> |
||
| 5395 | * |
||
| 5396 | * @psalm-return static<TKey,T> |
||
| 5397 | * @psalm-mutation-free |
||
| 5398 | */ |
||
| 5399 | 7 | View Code Duplication | public function removeLast(): self |
| 5411 | |||
| 5412 | /** |
||
| 5413 | * Removes a particular value from an array (numeric or associative). |
||
| 5414 | * |
||
| 5415 | * EXAMPLE: <code> |
||
| 5416 | * a([1 => 'bar', 'foo' => 'foo'])->removeValue('foo'); // Arrayy[1 => 'bar'] |
||
| 5417 | * </code> |
||
| 5418 | * |
||
| 5419 | * @param mixed $value |
||
| 5420 | * |
||
| 5421 | * @return static |
||
| 5422 | * <p>(Immutable)</p> |
||
| 5423 | * |
||
| 5424 | * @psalm-param T $value |
||
| 5425 | * @psalm-return static<TKey,T> |
||
| 5426 | * @psalm-mutation-free |
||
| 5427 | */ |
||
| 5428 | 8 | public function removeValue($value): self |
|
| 5451 | |||
| 5452 | /** |
||
| 5453 | * Generate array of repeated arrays. |
||
| 5454 | * |
||
| 5455 | * @param int $times <p>How many times has to be repeated.</p> |
||
| 5456 | * |
||
| 5457 | * @return static |
||
| 5458 | * <p>(Immutable)</p> |
||
| 5459 | * |
||
| 5460 | * @psalm-return static<TKey,T> |
||
| 5461 | * @psalm-mutation-free |
||
| 5462 | */ |
||
| 5463 | 1 | public function repeat($times): self |
|
| 5475 | |||
| 5476 | /** |
||
| 5477 | * Replace a key with a new key/value pair. |
||
| 5478 | * |
||
| 5479 | * EXAMPLE: <code> |
||
| 5480 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 5481 | * $arrayy->replace(2, 'notfoo', 'notbar'); // Arrayy[1 => 'foo', 'notfoo' => 'notbar', 3 => 'bar'] |
||
| 5482 | * </code> |
||
| 5483 | * |
||
| 5484 | * @param mixed $oldKey |
||
| 5485 | * @param mixed $newKey |
||
| 5486 | * @param mixed $newValue |
||
| 5487 | * |
||
| 5488 | * @return static |
||
| 5489 | * <p>(Immutable)</p> |
||
| 5490 | * |
||
| 5491 | * @psalm-return static<TKey,T> |
||
| 5492 | * @psalm-mutation-free |
||
| 5493 | */ |
||
| 5494 | 5 | public function replace($oldKey, $newKey, $newValue): self |
|
| 5504 | |||
| 5505 | /** |
||
| 5506 | * Create an array using the current array as values and the other array as keys. |
||
| 5507 | * |
||
| 5508 | * EXAMPLE: <code> |
||
| 5509 | * $firstArray = [ |
||
| 5510 | * 1 => 'one', |
||
| 5511 | * 2 => 'two', |
||
| 5512 | * 3 => 'three', |
||
| 5513 | * ]; |
||
| 5514 | * $secondArray = [ |
||
| 5515 | * 'one' => 1, |
||
| 5516 | * 1 => 'one', |
||
| 5517 | * 2 => 2, |
||
| 5518 | * ]; |
||
| 5519 | * $arrayy = a($firstArray); |
||
| 5520 | * $arrayy->replaceAllKeys($secondArray); // Arrayy[1 => "one", 'one' => "two", 2 => "three"] |
||
| 5521 | * </code> |
||
| 5522 | * |
||
| 5523 | * @param array $keys <p>An array of keys.</p> |
||
| 5524 | * |
||
| 5525 | * @return static |
||
| 5526 | * <p>(Immutable) Arrayy object with keys from the other array.</p> |
||
| 5527 | * |
||
| 5528 | * @psalm-param array<mixed,mixed>|array<mixed,TKey> $keys |
||
| 5529 | * @psalm-return static<TKey,T> |
||
| 5530 | * @psalm-mutation-free |
||
| 5531 | */ |
||
| 5532 | 2 | public function replaceAllKeys(array $keys): self |
|
| 5540 | |||
| 5541 | /** |
||
| 5542 | * Create an array using the current array as keys and the other array as values. |
||
| 5543 | * |
||
| 5544 | * EXAMPLE: <code> |
||
| 5545 | * $firstArray = [ |
||
| 5546 | * 1 => 'one', |
||
| 5547 | * 2 => 'two', |
||
| 5548 | * 3 => 'three', |
||
| 5549 | * ]; |
||
| 5550 | * $secondArray = [ |
||
| 5551 | * 'one' => 1, |
||
| 5552 | * 1 => 'one', |
||
| 5553 | * 2 => 2, |
||
| 5554 | * ]; |
||
| 5555 | * $arrayy = a($firstArray); |
||
| 5556 | * $arrayy->replaceAllValues($secondArray); // Arrayy['one' => 1, 'two' => 'one', 'three' => 2] |
||
| 5557 | * </code> |
||
| 5558 | * |
||
| 5559 | * @param array $array <p>An array of values.</p> |
||
| 5560 | * |
||
| 5561 | * @return static |
||
| 5562 | * <p>(Immutable) Arrayy object with values from the other array.</p> |
||
| 5563 | * |
||
| 5564 | * @psalm-param array<mixed,T> $array |
||
| 5565 | * @psalm-return static<TKey,T> |
||
| 5566 | * @psalm-mutation-free |
||
| 5567 | */ |
||
| 5568 | 2 | public function replaceAllValues(array $array): self |
|
| 5576 | |||
| 5577 | /** |
||
| 5578 | * Replace the keys in an array with another set. |
||
| 5579 | * |
||
| 5580 | * EXAMPLE: <code> |
||
| 5581 | * a([1 => 'bar', 'foo' => 'foo'])->replaceKeys([1 => 2, 'foo' => 'replaced']); // Arrayy[2 => 'bar', 'replaced' => 'foo'] |
||
| 5582 | * </code> |
||
| 5583 | * |
||
| 5584 | * @param array $keys <p>An array of keys matching the array's size</p> |
||
| 5585 | * |
||
| 5586 | * @return static |
||
| 5587 | * <p>(Immutable)</p> |
||
| 5588 | * |
||
| 5589 | * @psalm-param array<mixed,mixed>|array<mixed,TKey> $keys |
||
| 5590 | * @psalm-return static<TKey,T> |
||
| 5591 | * @psalm-mutation-free |
||
| 5592 | */ |
||
| 5593 | 1 | public function replaceKeys(array $keys): self |
|
| 5604 | |||
| 5605 | /** |
||
| 5606 | * Replace the first matched value in an array. |
||
| 5607 | * |
||
| 5608 | * EXAMPLE: <code> |
||
| 5609 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5610 | * a($testArray)->replaceOneValue('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'foobar'] |
||
| 5611 | * </code> |
||
| 5612 | * |
||
| 5613 | * @param mixed $search <p>The value to replace.</p> |
||
| 5614 | * @param mixed $replacement <p>The value to replace.</p> |
||
| 5615 | * |
||
| 5616 | * @return static |
||
| 5617 | * <p>(Immutable)</p> |
||
| 5618 | * |
||
| 5619 | * @psalm-return static<TKey,T> |
||
| 5620 | * @psalm-mutation-free |
||
| 5621 | */ |
||
| 5622 | 3 | public function replaceOneValue($search, $replacement = ''): self |
|
| 5637 | |||
| 5638 | /** |
||
| 5639 | * Replace values in the current array. |
||
| 5640 | * |
||
| 5641 | * EXAMPLE: <code> |
||
| 5642 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5643 | * a($testArray)->replaceValues('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'replacedbar'] |
||
| 5644 | * </code> |
||
| 5645 | * |
||
| 5646 | * @param mixed $search <p>The value to replace.</p> |
||
| 5647 | * @param mixed $replacement <p>What to replace it with.</p> |
||
| 5648 | * |
||
| 5649 | * @return static |
||
| 5650 | * <p>(Immutable)</p> |
||
| 5651 | * |
||
| 5652 | * @psalm-return static<TKey,T> |
||
| 5653 | * @psalm-mutation-free |
||
| 5654 | */ |
||
| 5655 | 1 | public function replaceValues($search, $replacement = ''): self |
|
| 5667 | |||
| 5668 | /** |
||
| 5669 | * Get the last elements from index $from until the end of this array. |
||
| 5670 | * |
||
| 5671 | * EXAMPLE: <code> |
||
| 5672 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->rest(2); // Arrayy[0 => 'lall'] |
||
| 5673 | * </code> |
||
| 5674 | * |
||
| 5675 | * @param int $from |
||
| 5676 | * |
||
| 5677 | * @return static |
||
| 5678 | * <p>(Immutable)</p> |
||
| 5679 | * |
||
| 5680 | * @psalm-return static<TKey,T> |
||
| 5681 | * @psalm-mutation-free |
||
| 5682 | */ |
||
| 5683 | 15 | View Code Duplication | public function rest(int $from = 1): self |
| 5693 | |||
| 5694 | /** |
||
| 5695 | * Return the array in the reverse order. |
||
| 5696 | * |
||
| 5697 | * EXAMPLE: <code> |
||
| 5698 | * a([1, 2, 3])->reverse(); // self[3, 2, 1] |
||
| 5699 | * </code> |
||
| 5700 | * |
||
| 5701 | * @return $this |
||
| 5702 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5703 | * |
||
| 5704 | * @psalm-return static<TKey,T> |
||
| 5705 | */ |
||
| 5706 | 9 | public function reverse(): self |
|
| 5714 | |||
| 5715 | /** |
||
| 5716 | * Sort an array in reverse order. |
||
| 5717 | * |
||
| 5718 | * @param int $sort_flags [optional] <p> |
||
| 5719 | * You may modify the behavior of the sort using the optional |
||
| 5720 | * parameter sort_flags, for details |
||
| 5721 | * see sort. |
||
| 5722 | * </p> |
||
| 5723 | * |
||
| 5724 | * @return $this |
||
| 5725 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5726 | * |
||
| 5727 | * @psalm-return static<TKey,T> |
||
| 5728 | */ |
||
| 5729 | 4 | public function rsort(int $sort_flags = 0): self |
|
| 5737 | |||
| 5738 | /** |
||
| 5739 | * Sort an array in reverse order. |
||
| 5740 | * |
||
| 5741 | * @param int $sort_flags [optional] <p> |
||
| 5742 | * You may modify the behavior of the sort using the optional |
||
| 5743 | * parameter sort_flags, for details |
||
| 5744 | * see sort. |
||
| 5745 | * </p> |
||
| 5746 | * |
||
| 5747 | * @return $this |
||
| 5748 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 5749 | * |
||
| 5750 | * @psalm-return static<TKey,T> |
||
| 5751 | * @psalm-mutation-free |
||
| 5752 | */ |
||
| 5753 | 4 | public function rsortImmutable(int $sort_flags = 0): self |
|
| 5764 | |||
| 5765 | /** |
||
| 5766 | * Search for the first index of the current array via $value. |
||
| 5767 | * |
||
| 5768 | * EXAMPLE: <code> |
||
| 5769 | * a(['fòô' => 'bàř', 'lall' => 'bàř'])->searchIndex('bàř'); // Arrayy[0 => 'fòô'] |
||
| 5770 | * </code> |
||
| 5771 | * |
||
| 5772 | * @param mixed $value |
||
| 5773 | * |
||
| 5774 | * @return false|float|int|string |
||
| 5775 | * <p>Will return <b>FALSE</b> if the value can't be found.</p> |
||
| 5776 | * @psalm-mutation-free |
||
| 5777 | */ |
||
| 5778 | 21 | public function searchIndex($value) |
|
| 5788 | |||
| 5789 | /** |
||
| 5790 | * Search for the value of the current array via $index. |
||
| 5791 | * |
||
| 5792 | * EXAMPLE: <code> |
||
| 5793 | * a(['fòô' => 'bàř'])->searchValue('fòô'); // Arrayy[0 => 'bàř'] |
||
| 5794 | * </code> |
||
| 5795 | * |
||
| 5796 | * @param mixed $index |
||
| 5797 | * |
||
| 5798 | * @return static |
||
| 5799 | * <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p> |
||
| 5800 | * |
||
| 5801 | * @psalm-return static<TKey,T> |
||
| 5802 | * @psalm-mutation-free |
||
| 5803 | */ |
||
| 5804 | 9 | public function searchValue($index): self |
|
| 5834 | |||
| 5835 | /** |
||
| 5836 | * Set a value for the current array (optional using dot-notation). |
||
| 5837 | * |
||
| 5838 | * EXAMPLE: <code> |
||
| 5839 | * $arrayy = a(['Lars' => ['lastname' => 'Moelleken']]); |
||
| 5840 | * $arrayy->set('Lars.lastname', 'Müller'); // Arrayy['Lars', ['lastname' => 'Müller']]] |
||
| 5841 | * </code> |
||
| 5842 | * |
||
| 5843 | * @param string $key <p>The key to set.</p> |
||
| 5844 | * @param mixed $value <p>Its value.</p> |
||
| 5845 | * |
||
| 5846 | * @return $this |
||
| 5847 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5848 | * |
||
| 5849 | * @psalm-param TKey $key |
||
| 5850 | * @psalm-param T $value |
||
| 5851 | * @psalm-return static<TKey,T> |
||
| 5852 | */ |
||
| 5853 | 28 | public function set($key, $value): self |
|
| 5859 | |||
| 5860 | /** |
||
| 5861 | * Get a value from a array and set it if it was not. |
||
| 5862 | * |
||
| 5863 | * WARNING: this method only set the value, if the $key is not already set |
||
| 5864 | * |
||
| 5865 | * EXAMPLE: <code> |
||
| 5866 | * $arrayy = a([1 => 1, 2 => 2, 3 => 3]); |
||
| 5867 | * $arrayy->setAndGet(1, 4); // 1 |
||
| 5868 | * $arrayy->setAndGet(0, 4); // 4 |
||
| 5869 | * </code> |
||
| 5870 | * |
||
| 5871 | * @param mixed $key <p>The key</p> |
||
| 5872 | * @param mixed $fallback <p>The default value to set if it isn't.</p> |
||
| 5873 | * |
||
| 5874 | * @return mixed |
||
| 5875 | * <p>(Mutable)</p> |
||
| 5876 | */ |
||
| 5877 | 11 | public function setAndGet($key, $fallback = null) |
|
| 5888 | |||
| 5889 | /** |
||
| 5890 | * Shifts a specified value off the beginning of array. |
||
| 5891 | * |
||
| 5892 | * @return mixed |
||
| 5893 | * <p>(Mutable) A shifted element from the current array.</p> |
||
| 5894 | */ |
||
| 5895 | 5 | public function shift() |
|
| 5901 | |||
| 5902 | /** |
||
| 5903 | * Shuffle the current array. |
||
| 5904 | * |
||
| 5905 | * EXAMPLE: <code> |
||
| 5906 | * a([1 => 'bar', 'foo' => 'foo'])->shuffle(); // e.g.: Arrayy[['foo' => 'foo', 1 => 'bar']] |
||
| 5907 | * </code> |
||
| 5908 | * |
||
| 5909 | * @param bool $secure <p>using a CSPRNG | @link https://paragonie.com/b/JvICXzh_jhLyt4y3</p> |
||
| 5910 | * @param array $array [optional] |
||
| 5911 | * |
||
| 5912 | * @return static |
||
| 5913 | * <p>(Immutable)</p> |
||
| 5914 | * |
||
| 5915 | * @psalm-param array<mixed,mixed>|array<TKey,T> $array |
||
| 5916 | * @psalm-return static<TKey,T> |
||
| 5917 | * |
||
| 5918 | * @noinspection BadExceptionsProcessingInspection |
||
| 5919 | * @noinspection NonSecureShuffleUsageInspection |
||
| 5920 | */ |
||
| 5921 | 2 | public function shuffle(bool $secure = false, array $array = null): self |
|
| 5959 | |||
| 5960 | /** |
||
| 5961 | * Count the values from the current array. |
||
| 5962 | * |
||
| 5963 | * alias: for "Arrayy->count()" |
||
| 5964 | * |
||
| 5965 | * @param int $mode |
||
| 5966 | * |
||
| 5967 | * @return int |
||
| 5968 | */ |
||
| 5969 | 20 | public function size(int $mode = \COUNT_NORMAL): int |
|
| 5973 | |||
| 5974 | /** |
||
| 5975 | * Checks whether array has exactly $size items. |
||
| 5976 | * |
||
| 5977 | * @param int $size |
||
| 5978 | * |
||
| 5979 | * @return bool |
||
| 5980 | */ |
||
| 5981 | 1 | public function sizeIs(int $size): bool |
|
| 5997 | |||
| 5998 | /** |
||
| 5999 | * Checks whether array has between $fromSize to $toSize items. $toSize can be |
||
| 6000 | * smaller than $fromSize. |
||
| 6001 | * |
||
| 6002 | * @param int $fromSize |
||
| 6003 | * @param int $toSize |
||
| 6004 | * |
||
| 6005 | * @return bool |
||
| 6006 | */ |
||
| 6007 | 1 | public function sizeIsBetween(int $fromSize, int $toSize): bool |
|
| 6027 | |||
| 6028 | /** |
||
| 6029 | * Checks whether array has more than $size items. |
||
| 6030 | * |
||
| 6031 | * @param int $size |
||
| 6032 | * |
||
| 6033 | * @return bool |
||
| 6034 | */ |
||
| 6035 | 1 | View Code Duplication | public function sizeIsGreaterThan(int $size): bool |
| 6049 | |||
| 6050 | /** |
||
| 6051 | * Checks whether array has less than $size items. |
||
| 6052 | * |
||
| 6053 | * @param int $size |
||
| 6054 | * |
||
| 6055 | * @return bool |
||
| 6056 | */ |
||
| 6057 | 1 | View Code Duplication | public function sizeIsLessThan(int $size): bool |
| 6071 | |||
| 6072 | /** |
||
| 6073 | * Counts all elements in an array, or something in an object. |
||
| 6074 | * |
||
| 6075 | * <p> |
||
| 6076 | * For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}. |
||
| 6077 | * The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() |
||
| 6078 | * function. Please see the {@see Array} section of the manual for a detailed explanation of how arrays are |
||
| 6079 | * implemented and used in PHP. |
||
| 6080 | * </p> |
||
| 6081 | * |
||
| 6082 | * @return int |
||
| 6083 | * <p> |
||
| 6084 | * The number of elements in var, which is |
||
| 6085 | * typically an array, since anything else will have one |
||
| 6086 | * element. |
||
| 6087 | * </p> |
||
| 6088 | * <p> |
||
| 6089 | * If var is not an array or an object with |
||
| 6090 | * implemented Countable interface, |
||
| 6091 | * 1 will be returned. |
||
| 6092 | * There is one exception, if var is &null;, |
||
| 6093 | * 0 will be returned. |
||
| 6094 | * </p> |
||
| 6095 | * <p> |
||
| 6096 | * Caution: count may return 0 for a variable that isn't set, |
||
| 6097 | * but it may also return 0 for a variable that has been initialized with an |
||
| 6098 | * empty array. Use isset to test if a variable is set. |
||
| 6099 | * </p> |
||
| 6100 | */ |
||
| 6101 | 10 | public function sizeRecursive(): int |
|
| 6105 | |||
| 6106 | /** |
||
| 6107 | * Extract a slice of the array. |
||
| 6108 | * |
||
| 6109 | * @param int $offset <p>Slice begin index.</p> |
||
| 6110 | * @param int|null $length <p>Length of the slice.</p> |
||
| 6111 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 6112 | * |
||
| 6113 | * @return static |
||
| 6114 | * <p>(Immutable) A slice of the original array with length $length.</p> |
||
| 6115 | * |
||
| 6116 | * @psalm-return static<TKey,T> |
||
| 6117 | * @psalm-mutation-free |
||
| 6118 | */ |
||
| 6119 | 5 | public function slice(int $offset, int $length = null, bool $preserveKeys = false) |
|
| 6132 | |||
| 6133 | /** |
||
| 6134 | * Sort the current array and optional you can keep the keys. |
||
| 6135 | * |
||
| 6136 | * EXAMPLE: <code> |
||
| 6137 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sort(SORT_ASC, SORT_NATURAL, false); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6138 | * </code> |
||
| 6139 | * |
||
| 6140 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6141 | * @param int $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6142 | * <strong>SORT_NATURAL</strong></p> |
||
| 6143 | * @param bool $keepKeys |
||
| 6144 | * |
||
| 6145 | * @return static |
||
| 6146 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6147 | * |
||
| 6148 | * @psalm-return static<TKey,T> |
||
| 6149 | */ |
||
| 6150 | 20 | public function sort( |
|
| 6164 | |||
| 6165 | /** |
||
| 6166 | * Sort the current array and optional you can keep the keys. |
||
| 6167 | * |
||
| 6168 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6169 | * @param int $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6170 | * <strong>SORT_NATURAL</strong></p> |
||
| 6171 | * @param bool $keepKeys |
||
| 6172 | * |
||
| 6173 | * @return static |
||
| 6174 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6175 | * |
||
| 6176 | * @psalm-return static<TKey,T> |
||
| 6177 | */ |
||
| 6178 | 12 | public function sortImmutable( |
|
| 6194 | |||
| 6195 | /** |
||
| 6196 | * Sort the current array by key. |
||
| 6197 | * |
||
| 6198 | * EXAMPLE: <code> |
||
| 6199 | * a([1 => 2, 0 => 1])->sortKeys(\SORT_ASC); // Arrayy[0 => 1, 1 => 2] |
||
| 6200 | * </code> |
||
| 6201 | * |
||
| 6202 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6203 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6204 | * |
||
| 6205 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6206 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6207 | * <strong>SORT_NATURAL</strong></p> |
||
| 6208 | * |
||
| 6209 | * @return $this |
||
| 6210 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6211 | * |
||
| 6212 | * @psalm-return static<TKey,T> |
||
| 6213 | */ |
||
| 6214 | 18 | public function sortKeys( |
|
| 6224 | |||
| 6225 | /** |
||
| 6226 | * Sort the current array by key. |
||
| 6227 | * |
||
| 6228 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6229 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6230 | * |
||
| 6231 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6232 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6233 | * <strong>SORT_NATURAL</strong></p> |
||
| 6234 | * |
||
| 6235 | * @return $this |
||
| 6236 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6237 | * |
||
| 6238 | * @psalm-return static<TKey,T> |
||
| 6239 | * @psalm-mutation-free |
||
| 6240 | */ |
||
| 6241 | 8 | public function sortKeysImmutable( |
|
| 6254 | |||
| 6255 | /** |
||
| 6256 | * Sort the current array by value. |
||
| 6257 | * |
||
| 6258 | * EXAMPLE: <code> |
||
| 6259 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueKeepIndex(SORT_ASC, SORT_REGULAR); // Arrayy[0 => 'a', 3 => 'd', 2 => 'f'] |
||
| 6260 | * </code> |
||
| 6261 | * |
||
| 6262 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6263 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6264 | * <strong>SORT_NATURAL</strong></p> |
||
| 6265 | * |
||
| 6266 | * @return static |
||
| 6267 | * <p>(Mutable)</p> |
||
| 6268 | * |
||
| 6269 | * @psalm-return static<TKey,T> |
||
| 6270 | */ |
||
| 6271 | 1 | public function sortValueKeepIndex( |
|
| 6277 | |||
| 6278 | /** |
||
| 6279 | * Sort the current array by value. |
||
| 6280 | * |
||
| 6281 | * EXAMPLE: <code> |
||
| 6282 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueNewIndex(SORT_ASC, SORT_NATURAL); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6283 | * </code> |
||
| 6284 | * |
||
| 6285 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6286 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6287 | * <strong>SORT_NATURAL</strong></p> |
||
| 6288 | * |
||
| 6289 | * @return static |
||
| 6290 | * <p>(Mutable)</p> |
||
| 6291 | * |
||
| 6292 | * @psalm-return static<TKey,T> |
||
| 6293 | */ |
||
| 6294 | 1 | public function sortValueNewIndex($direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6298 | |||
| 6299 | /** |
||
| 6300 | * Sort a array by value or by a closure. |
||
| 6301 | * |
||
| 6302 | * - If the sorter is null, the array is sorted naturally. |
||
| 6303 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
| 6304 | * |
||
| 6305 | * EXAMPLE: <code> |
||
| 6306 | * $testArray = range(1, 5); |
||
| 6307 | * $under = a($testArray)->sorter( |
||
| 6308 | * function ($value) { |
||
| 6309 | * return $value % 2 === 0; |
||
| 6310 | * } |
||
| 6311 | * ); |
||
| 6312 | * var_dump($under); // Arrayy[1, 3, 5, 2, 4] |
||
| 6313 | * </code> |
||
| 6314 | * |
||
| 6315 | * @param callable|mixed|null $sorter |
||
| 6316 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or |
||
| 6317 | * <strong>SORT_DESC</strong></p> |
||
| 6318 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6319 | * <strong>SORT_NATURAL</strong></p> |
||
| 6320 | * |
||
| 6321 | * @return static |
||
| 6322 | * <p>(Immutable)</p> |
||
| 6323 | * |
||
| 6324 | * @pslam-param callable|T|null $sorter |
||
| 6325 | * @psalm-return static<TKey,T> |
||
| 6326 | * @psalm-mutation-free |
||
| 6327 | */ |
||
| 6328 | 1 | public function sorter($sorter = null, $direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6369 | |||
| 6370 | /** |
||
| 6371 | * @param int $offset |
||
| 6372 | * @param int|null $length |
||
| 6373 | * @param array $replacement |
||
| 6374 | * |
||
| 6375 | * @return static |
||
| 6376 | * <p>(Immutable)</p> |
||
| 6377 | * |
||
| 6378 | * @psalm-param array<mixed,mixed>|array<mixed,T> $replacement |
||
| 6379 | * @psalm-return static<TKey,T> |
||
| 6380 | * @psalm-mutation-free |
||
| 6381 | */ |
||
| 6382 | 1 | public function splice(int $offset, int $length = null, $replacement = []): self |
|
| 6399 | |||
| 6400 | /** |
||
| 6401 | * Split an array in the given amount of pieces. |
||
| 6402 | * |
||
| 6403 | * EXAMPLE: <code> |
||
| 6404 | * a(['a' => 1, 'b' => 2])->split(2, true); // Arrayy[['a' => 1], ['b' => 2]] |
||
| 6405 | * </code> |
||
| 6406 | * |
||
| 6407 | * @param int $numberOfPieces |
||
| 6408 | * @param bool $keepKeys |
||
| 6409 | * |
||
| 6410 | * @return static |
||
| 6411 | * <p>(Immutable)</p> |
||
| 6412 | * |
||
| 6413 | * @psalm-return static<TKey,T> |
||
| 6414 | * @psalm-mutation-free |
||
| 6415 | */ |
||
| 6416 | 1 | public function split(int $numberOfPieces = 2, bool $keepKeys = false): self |
|
| 6472 | |||
| 6473 | /** |
||
| 6474 | * Strip all empty items from the current array. |
||
| 6475 | * |
||
| 6476 | * EXAMPLE: <code> |
||
| 6477 | * a(['a' => 1, 'b' => ''])->stripEmpty(); // Arrayy[['a' => 1]] |
||
| 6478 | * </code> |
||
| 6479 | * |
||
| 6480 | * @return static |
||
| 6481 | * <p>(Immutable)</p> |
||
| 6482 | * |
||
| 6483 | * @psalm-return static<TKey,T> |
||
| 6484 | * @psalm-mutation-free |
||
| 6485 | */ |
||
| 6486 | 1 | public function stripEmpty(): self |
|
| 6498 | |||
| 6499 | /** |
||
| 6500 | * Swap two values between positions by key. |
||
| 6501 | * |
||
| 6502 | * EXAMPLE: <code> |
||
| 6503 | * a(['a' => 1, 'b' => ''])->swap('a', 'b'); // Arrayy[['a' => '', 'b' => 1]] |
||
| 6504 | * </code> |
||
| 6505 | * |
||
| 6506 | * @param int|string $swapA <p>a key in the array</p> |
||
| 6507 | * @param int|string $swapB <p>a key in the array</p> |
||
| 6508 | * |
||
| 6509 | * @return static |
||
| 6510 | * <p>(Immutable)</p> |
||
| 6511 | * |
||
| 6512 | * @psalm-return static<TKey,T> |
||
| 6513 | * @psalm-mutation-free |
||
| 6514 | */ |
||
| 6515 | 1 | public function swap($swapA, $swapB): self |
|
| 6527 | |||
| 6528 | /** |
||
| 6529 | * Get the current array from the "Arrayy"-object. |
||
| 6530 | * alias for "getArray()" |
||
| 6531 | * |
||
| 6532 | * @param bool $convertAllArrayyElements <p> |
||
| 6533 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6534 | * </p> |
||
| 6535 | * @param bool $preserveKeys <p> |
||
| 6536 | * e.g.: A generator maybe return the same key more then once, |
||
| 6537 | * so maybe you will ignore the keys. |
||
| 6538 | * </p> |
||
| 6539 | * |
||
| 6540 | * @return array |
||
| 6541 | * |
||
| 6542 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 6543 | * @psalm-mutation-free |
||
| 6544 | */ |
||
| 6545 | 941 | public function toArray( |
|
| 6573 | |||
| 6574 | /** |
||
| 6575 | * Get the current array from the "Arrayy"-object as list. |
||
| 6576 | * |
||
| 6577 | * @param bool $convertAllArrayyElements <p> |
||
| 6578 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6579 | * </p> |
||
| 6580 | * |
||
| 6581 | * @return array |
||
| 6582 | * |
||
| 6583 | * @psalm-return list<array<TKey,T>> |
||
| 6584 | * @psalm-mutation-free |
||
| 6585 | */ |
||
| 6586 | 1 | public function toList(bool $convertAllArrayyElements = false): array |
|
| 6593 | |||
| 6594 | /** |
||
| 6595 | * Convert the current array to JSON. |
||
| 6596 | * |
||
| 6597 | * EXAMPLE: <code> |
||
| 6598 | * a(['bar', ['foo']])->toJson(); // '["bar",{"1":"foo"}]' |
||
| 6599 | * </code> |
||
| 6600 | * |
||
| 6601 | * @param int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p> |
||
| 6602 | * @param int $depth [optional] <p>Set the maximum depth. Must be greater than zero.</p> |
||
| 6603 | * |
||
| 6604 | * @return string |
||
| 6605 | */ |
||
| 6606 | 12 | public function toJson(int $options = 0, int $depth = 512): string |
|
| 6615 | |||
| 6616 | /** |
||
| 6617 | * @param string[]|null $items [optional] |
||
| 6618 | * @param string[] $helper [optional] |
||
| 6619 | * |
||
| 6620 | * @return static|static[] |
||
| 6621 | * |
||
| 6622 | * @psalm-return static<int, static<TKey,T>> |
||
| 6623 | */ |
||
| 6624 | 1 | public function toPermutation(array $items = null, array $helper = []): self |
|
| 6657 | |||
| 6658 | /** |
||
| 6659 | * Implodes array to a string with specified separator. |
||
| 6660 | * |
||
| 6661 | * @param string $separator [optional] <p>The element's separator.</p> |
||
| 6662 | * |
||
| 6663 | * @return string |
||
| 6664 | * <p>The string representation of array, separated by ",".</p> |
||
| 6665 | */ |
||
| 6666 | 19 | public function toString(string $separator = ','): string |
|
| 6670 | |||
| 6671 | /** |
||
| 6672 | * Return a duplicate free copy of the current array. |
||
| 6673 | * |
||
| 6674 | * EXAMPLE: <code> |
||
| 6675 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[1, 2] |
||
| 6676 | * </code> |
||
| 6677 | * |
||
| 6678 | * @return $this |
||
| 6679 | * <p>(Mutable)</p> |
||
| 6680 | * |
||
| 6681 | * @psalm-return static<TKey,T> |
||
| 6682 | */ |
||
| 6683 | 13 | public function uniqueNewIndex(): self |
|
| 6705 | |||
| 6706 | /** |
||
| 6707 | * Return a duplicate free copy of the current array. (with the old keys) |
||
| 6708 | * |
||
| 6709 | * EXAMPLE: <code> |
||
| 6710 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[2 => 1, 3 => 2] |
||
| 6711 | * </code> |
||
| 6712 | * |
||
| 6713 | * @return $this |
||
| 6714 | * <p>(Mutable)</p> |
||
| 6715 | * |
||
| 6716 | * @psalm-return static<TKey,T> |
||
| 6717 | */ |
||
| 6718 | 11 | public function uniqueKeepIndex(): self |
|
| 6744 | |||
| 6745 | /** |
||
| 6746 | * alias: for "Arrayy->uniqueNewIndex()" |
||
| 6747 | * |
||
| 6748 | * @return static |
||
| 6749 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 6750 | * |
||
| 6751 | * @see Arrayy::unique() |
||
| 6752 | * |
||
| 6753 | * @psalm-return static<TKey,T> |
||
| 6754 | */ |
||
| 6755 | 13 | public function unique(): self |
|
| 6759 | |||
| 6760 | /** |
||
| 6761 | * Prepends one or more values to the beginning of array at once. |
||
| 6762 | * |
||
| 6763 | * @param mixed ...$args |
||
| 6764 | * |
||
| 6765 | * @return $this |
||
| 6766 | * <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p> |
||
| 6767 | * |
||
| 6768 | * @psalm-param array<mixed,mixed>|array<TKey,T> ...$args |
||
| 6769 | * @psalm-return static<TKey,T> |
||
| 6770 | */ |
||
| 6771 | 6 | View Code Duplication | public function unshift(...$args): self |
| 6789 | |||
| 6790 | /** |
||
| 6791 | * Tests whether the given closure return something valid for all elements of this array. |
||
| 6792 | * |
||
| 6793 | * @param \Closure $closure the predicate |
||
| 6794 | * |
||
| 6795 | * @return bool |
||
| 6796 | * <p>TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.</p> |
||
| 6797 | */ |
||
| 6798 | 1 | View Code Duplication | public function validate(\Closure $closure): bool |
| 6808 | |||
| 6809 | /** |
||
| 6810 | * Get all values from a array. |
||
| 6811 | * |
||
| 6812 | * EXAMPLE: <code> |
||
| 6813 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 6814 | * $arrayyTmp->values(); // Arrayy[0 => 'foo', 1 => 'foo2', 2 => 'bar'] |
||
| 6815 | * </code> |
||
| 6816 | * |
||
| 6817 | * @return static |
||
| 6818 | * <p>(Immutable)</p> |
||
| 6819 | * |
||
| 6820 | * @psalm-return static<TKey,T> |
||
| 6821 | * @psalm-mutation-free |
||
| 6822 | */ |
||
| 6823 | 2 | public function values(): self |
|
| 6836 | |||
| 6837 | /** |
||
| 6838 | * Apply the given function to every element in the array, discarding the results. |
||
| 6839 | * |
||
| 6840 | * EXAMPLE: <code> |
||
| 6841 | * $callable = function (&$value, $key) { |
||
| 6842 | * $value = $key; |
||
| 6843 | * }; |
||
| 6844 | * $arrayy = a([1, 2, 3]); |
||
| 6845 | * $arrayy->walk($callable); // Arrayy[0, 1, 2] |
||
| 6846 | * </code> |
||
| 6847 | * |
||
| 6848 | * @param callable $callable |
||
| 6849 | * @param bool $recursive [optional] <p>Whether array will be walked recursively or no</p> |
||
| 6850 | * @param mixed $userData [optional] <p> |
||
| 6851 | * If the optional $userData parameter is supplied, |
||
| 6852 | * it will be passed as the third parameter to the $callable. |
||
| 6853 | * </p> |
||
| 6854 | * |
||
| 6855 | * @return $this |
||
| 6856 | * <p>(Mutable) Return this Arrayy object, with modified elements.</p> |
||
| 6857 | * |
||
| 6858 | * @psalm-return static<TKey,T> |
||
| 6859 | */ |
||
| 6860 | 12 | public function walk( |
|
| 6886 | |||
| 6887 | /** |
||
| 6888 | * Returns a collection of matching items. |
||
| 6889 | * |
||
| 6890 | * @param string $keyOrPropertyOrMethod the property or method to evaluate |
||
| 6891 | * @param mixed $value the value to match |
||
| 6892 | * |
||
| 6893 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 6894 | * |
||
| 6895 | * @return static |
||
| 6896 | * |
||
| 6897 | * @psalm-return static<TKey,T> |
||
| 6898 | */ |
||
| 6899 | 1 | public function where(string $keyOrPropertyOrMethod, $value): self |
|
| 6912 | |||
| 6913 | /** |
||
| 6914 | * Convert an array into a object. |
||
| 6915 | * |
||
| 6916 | * @param array $array |
||
| 6917 | * |
||
| 6918 | * @return \stdClass |
||
| 6919 | * |
||
| 6920 | * @psalm-param array<mixed,mixed> $array |
||
| 6921 | */ |
||
| 6922 | 4 | final protected static function arrayToObject(array $array = []): \stdClass |
|
| 6941 | |||
| 6942 | /** |
||
| 6943 | * @param array|\Generator|null $input <p> |
||
| 6944 | * An array containing keys to return. |
||
| 6945 | * </p> |
||
| 6946 | * @param mixed|null $search_values [optional] <p> |
||
| 6947 | * If specified, then only keys containing these values are returned. |
||
| 6948 | * </p> |
||
| 6949 | * @param bool $strict [optional] <p> |
||
| 6950 | * Determines if strict comparison (===) should be used during the |
||
| 6951 | * search. |
||
| 6952 | * </p> |
||
| 6953 | * |
||
| 6954 | * @return array |
||
| 6955 | * <p>an array of all the keys in input</p> |
||
| 6956 | * |
||
| 6957 | * @psalm-param array<mixed,mixed>|\Generator<TKey,T>|null $input |
||
| 6958 | * @psalm-return array<TKey|mixed> |
||
| 6959 | * @psalm-mutation-free |
||
| 6960 | */ |
||
| 6961 | 11 | protected function array_keys_recursive( |
|
| 7022 | |||
| 7023 | /** |
||
| 7024 | * @param mixed $path |
||
| 7025 | * @param callable $callable |
||
| 7026 | * @param array|null $currentOffset |
||
| 7027 | * |
||
| 7028 | * @return void |
||
| 7029 | * |
||
| 7030 | * @psalm-param array<mixed,mixed>|array<TKey,T>|null $currentOffset |
||
| 7031 | * @psalm-mutation-free |
||
| 7032 | */ |
||
| 7033 | 10 | protected function callAtPath($path, $callable, &$currentOffset = null) |
|
| 7062 | |||
| 7063 | /** |
||
| 7064 | * Extracts the value of the given property or method from the object. |
||
| 7065 | * |
||
| 7066 | * @param static $object <p>The object to extract the value from.</p> |
||
| 7067 | * @param string $keyOrPropertyOrMethod <p>The property or method for which the |
||
| 7068 | * value should be extracted.</p> |
||
| 7069 | * |
||
| 7070 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 7071 | * |
||
| 7072 | * @return mixed |
||
| 7073 | * <p>The value extracted from the specified property or method.</p> |
||
| 7074 | * |
||
| 7075 | * @psalm-param self<TKey,T> $object |
||
| 7076 | */ |
||
| 7077 | 1 | final protected function extractValue(self $object, string $keyOrPropertyOrMethod) |
|
| 7099 | |||
| 7100 | /** |
||
| 7101 | * create a fallback for array |
||
| 7102 | * |
||
| 7103 | * 1. use the current array, if it's a array |
||
| 7104 | * 2. fallback to empty array, if there is nothing |
||
| 7105 | * 3. call "getArray()" on object, if there is a "Arrayy"-object |
||
| 7106 | * 4. call "createFromObject()" on object, if there is a "\Traversable"-object |
||
| 7107 | * 5. call "__toArray()" on object, if the method exists |
||
| 7108 | * 6. cast a string or object with "__toString()" into an array |
||
| 7109 | * 7. throw a "InvalidArgumentException"-Exception |
||
| 7110 | * |
||
| 7111 | * @param mixed $data |
||
| 7112 | * |
||
| 7113 | * @throws \InvalidArgumentException |
||
| 7114 | * |
||
| 7115 | * @return array |
||
| 7116 | * |
||
| 7117 | * @psalm-return array<mixed,mixed>|array<TKey,T> |
||
| 7118 | */ |
||
| 7119 | 1212 | protected function fallbackForArray(&$data): array |
|
| 7129 | |||
| 7130 | /** |
||
| 7131 | * @param bool $preserveKeys <p> |
||
| 7132 | * e.g.: A generator maybe return the same key more then once, |
||
| 7133 | * so maybe you will ignore the keys. |
||
| 7134 | * </p> |
||
| 7135 | * |
||
| 7136 | * @return bool |
||
| 7137 | * |
||
| 7138 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7139 | * @psalm-mutation-free :/ |
||
| 7140 | */ |
||
| 7141 | 1121 | protected function generatorToArray(bool $preserveKeys = true) |
|
| 7152 | |||
| 7153 | /** |
||
| 7154 | * Get correct PHP constant for direction. |
||
| 7155 | * |
||
| 7156 | * @param int|string $direction |
||
| 7157 | * |
||
| 7158 | * @return int |
||
| 7159 | * @psalm-mutation-free |
||
| 7160 | */ |
||
| 7161 | 43 | protected function getDirection($direction): int |
|
| 7183 | |||
| 7184 | /** |
||
| 7185 | * @return TypeCheckInterface[] |
||
| 7186 | * |
||
| 7187 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7188 | */ |
||
| 7189 | 24 | protected function getPropertiesFromPhpDoc() |
|
| 7244 | |||
| 7245 | /** |
||
| 7246 | * @param mixed $glue |
||
| 7247 | * @param mixed $pieces |
||
| 7248 | * @param bool $useKeys |
||
| 7249 | * |
||
| 7250 | * @return string |
||
| 7251 | * @psalm-mutation-free |
||
| 7252 | */ |
||
| 7253 | 36 | protected function implode_recursive( |
|
| 7286 | |||
| 7287 | /** |
||
| 7288 | * @param mixed $needle <p> |
||
| 7289 | * The searched value. |
||
| 7290 | * </p> |
||
| 7291 | * <p> |
||
| 7292 | * If needle is a string, the comparison is done |
||
| 7293 | * in a case-sensitive manner. |
||
| 7294 | * </p> |
||
| 7295 | * @param array|\Generator|null $haystack <p> |
||
| 7296 | * The array. |
||
| 7297 | * </p> |
||
| 7298 | * @param bool $strict [optional] <p> |
||
| 7299 | * If the third parameter strict is set to true |
||
| 7300 | * then the in_array function will also check the |
||
| 7301 | * types of the |
||
| 7302 | * needle in the haystack. |
||
| 7303 | * </p> |
||
| 7304 | * |
||
| 7305 | * @return bool |
||
| 7306 | * <p>true if needle is found in the array, false otherwise</p> |
||
| 7307 | * |
||
| 7308 | * @psalm-param array<mixed,mixed>|\Generator<TKey,T>|null $haystack |
||
| 7309 | * @psalm-mutation-free |
||
| 7310 | */ |
||
| 7311 | 18 | protected function in_array_recursive($needle, $haystack = null, $strict = true): bool |
|
| 7336 | |||
| 7337 | /** |
||
| 7338 | * @param mixed $data |
||
| 7339 | * |
||
| 7340 | * @return array|null |
||
| 7341 | * |
||
| 7342 | * @psalm-return array<mixed,mixed>|array<TKey,T>|null |
||
| 7343 | */ |
||
| 7344 | 1212 | protected function internalGetArray(&$data) |
|
| 7395 | |||
| 7396 | /** |
||
| 7397 | * Internal mechanics of remove method. |
||
| 7398 | * |
||
| 7399 | * @param mixed $key |
||
| 7400 | * |
||
| 7401 | * @return bool |
||
| 7402 | */ |
||
| 7403 | 22 | protected function internalRemove($key): bool |
|
| 7436 | |||
| 7437 | /** |
||
| 7438 | * Internal mechanic of set method. |
||
| 7439 | * |
||
| 7440 | * @param int|string|null $key |
||
| 7441 | * @param mixed $value |
||
| 7442 | * @param bool $checkProperties |
||
| 7443 | * |
||
| 7444 | * @return bool |
||
| 7445 | */ |
||
| 7446 | 1062 | protected function internalSet( |
|
| 7505 | |||
| 7506 | /** |
||
| 7507 | * Convert a object into an array. |
||
| 7508 | * |
||
| 7509 | * @param mixed|object $object |
||
| 7510 | * |
||
| 7511 | * @return array|mixed |
||
| 7512 | * |
||
| 7513 | * @psalm-mutation-free |
||
| 7514 | */ |
||
| 7515 | 5 | protected static function objectToArray($object) |
|
| 7528 | |||
| 7529 | /** |
||
| 7530 | * @param array $data |
||
| 7531 | * @param bool $checkPropertiesInConstructor |
||
| 7532 | * |
||
| 7533 | * @return void |
||
| 7534 | * |
||
| 7535 | * @psalm-param array<mixed,T> $data |
||
| 7536 | */ |
||
| 7537 | 1210 | protected function setInitialValuesAndProperties(array &$data, bool $checkPropertiesInConstructor) |
|
| 7582 | |||
| 7583 | /** |
||
| 7584 | * sorting keys |
||
| 7585 | * |
||
| 7586 | * @param array $elements |
||
| 7587 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7588 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7589 | * <strong>SORT_NATURAL</strong></p> |
||
| 7590 | * |
||
| 7591 | * @return $this |
||
| 7592 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7593 | * |
||
| 7594 | * @psalm-param array<mixed,mixed>|array<mixed|TKey,T> $elements |
||
| 7595 | * @psalm-return static<TKey,T> |
||
| 7596 | */ |
||
| 7597 | 18 | protected function sorterKeys( |
|
| 7618 | |||
| 7619 | /** |
||
| 7620 | * @param array $elements <p>Warning: used as reference</p> |
||
| 7621 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7622 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7623 | * <strong>SORT_NATURAL</strong></p> |
||
| 7624 | * @param bool $keepKeys |
||
| 7625 | * |
||
| 7626 | * @return $this |
||
| 7627 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7628 | * |
||
| 7629 | * @psalm-param array<mixed,mixed>|array<mixed|TKey,T> $elements |
||
| 7630 | * @psalm-return static<TKey,T> |
||
| 7631 | */ |
||
| 7632 | 24 | protected function sorting( |
|
| 7666 | |||
| 7667 | /** |
||
| 7668 | * @param array $array |
||
| 7669 | * |
||
| 7670 | * @return array |
||
| 7671 | * |
||
| 7672 | * @psalm-mutation-free |
||
| 7673 | */ |
||
| 7674 | 25 | private function getArrayRecursiveHelperArrayy(array $array) |
|
| 7696 | |||
| 7697 | /** |
||
| 7698 | * @param int|string|null $key |
||
| 7699 | * @param mixed $value |
||
| 7700 | * |
||
| 7701 | * @return void |
||
| 7702 | */ |
||
| 7703 | 117 | private function checkType($key, $value) |
|
| 7721 | } |
||
| 7722 |
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..