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