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 |
||
16 | class Arrayy extends \ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $array = array(); |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $iteratorClass; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $pathSeparator = '.'; |
||
32 | |||
33 | /** @noinspection MagicMethodsValidityInspection */ |
||
34 | /** |
||
35 | * Initializes |
||
36 | * |
||
37 | * @param array $array |
||
38 | * @param string $iteratorClass |
||
39 | */ |
||
40 | 775 | public function __construct($array = array(), $iteratorClass = '\\Arrayy\\ArrayyIterator') |
|
47 | |||
48 | /** |
||
49 | * Get a value by key. |
||
50 | * |
||
51 | * @param $key |
||
52 | * |
||
53 | * @return mixed <p>Get a Value from the current array.</p> |
||
54 | */ |
||
55 | 2 | public function &__get($key) |
|
65 | |||
66 | /** |
||
67 | * Call object as function. |
||
68 | * |
||
69 | * @param mixed $key |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 1 | public function __invoke($key = null) |
|
85 | |||
86 | /** |
||
87 | * Whether or not an element exists by key. |
||
88 | * |
||
89 | * @param mixed $key |
||
90 | * |
||
91 | * @return bool <p>True is the key/index exists, otherwise false.</p> |
||
92 | */ |
||
93 | public function __isset($key) |
||
97 | |||
98 | /** |
||
99 | * Assigns a value to the specified element. |
||
100 | * |
||
101 | * @param mixed $key |
||
102 | * @param mixed $value |
||
103 | */ |
||
104 | 2 | public function __set($key, $value) |
|
108 | |||
109 | /** |
||
110 | * magic to string |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 16 | public function __toString() |
|
118 | |||
119 | /** |
||
120 | * Unset element by key. |
||
121 | * |
||
122 | * @param mixed $key |
||
123 | */ |
||
124 | public function __unset($key) |
||
128 | |||
129 | /** |
||
130 | * alias: for "Arrayy->append()" |
||
131 | * |
||
132 | * @see Arrayy::append() |
||
133 | * |
||
134 | * @param mixed $value |
||
135 | * |
||
136 | * @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
137 | */ |
||
138 | 1 | public function add($value) |
|
142 | |||
143 | /** |
||
144 | * Append a (key) + value to the current array. |
||
145 | * |
||
146 | * @param mixed $value |
||
147 | * @param mixed $key |
||
148 | * |
||
149 | * @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
150 | 9 | */ |
|
151 | public function append($value, $key = null) |
||
161 | |||
162 | /** |
||
163 | * Sort the entries by value. |
||
164 | * |
||
165 | * @param int $sort_flags [optional] <p> |
||
166 | * You may modify the behavior of the sort using the optional |
||
167 | * parameter sort_flags, for details |
||
168 | 4 | * see sort. |
|
169 | * </p> |
||
170 | 4 | * |
|
171 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
172 | 4 | */ |
|
173 | public function asort($sort_flags = null) |
||
179 | |||
180 | /** |
||
181 | * Count the values from the current array. |
||
182 | * |
||
183 | * alias: for "Arrayy->size()" |
||
184 | 93 | * |
|
185 | * @see Arrayy::size() |
||
186 | 93 | * |
|
187 | * @return int |
||
188 | */ |
||
189 | public function count() |
||
193 | |||
194 | /** |
||
195 | * Exchange the array for another one. |
||
196 | * |
||
197 | * @param array|Arrayy $data |
||
198 | * |
||
199 | * @return array |
||
200 | 1 | */ |
|
201 | public function exchangeArray($data) |
||
207 | |||
208 | /** |
||
209 | * Creates a copy of the ArrayyObject. |
||
210 | * |
||
211 | * @return array |
||
212 | 1 | */ |
|
213 | public function getArrayCopy() |
||
217 | |||
218 | /** |
||
219 | * Returns a new ArrayyIterator, thus implementing the IteratorAggregate interface. |
||
220 | * |
||
221 | * @return ArrayyIterator <p>An iterator for the values in the array.</p> |
||
222 | */ |
||
223 | public function getIterator() |
||
229 | |||
230 | /** |
||
231 | * Gets the iterator classname for the ArrayObject. |
||
232 | * |
||
233 | * @return string |
||
234 | 20 | */ |
|
235 | public function getIteratorClass() |
||
239 | |||
240 | /** |
||
241 | * Sort the entries by key |
||
242 | * |
||
243 | * @param int $sort_flags [optional] <p> |
||
244 | * You may modify the behavior of the sort using the optional |
||
245 | * parameter sort_flags, for details |
||
246 | 20 | * see sort. |
|
247 | * </p> |
||
248 | 20 | * |
|
249 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
250 | */ |
||
251 | public function ksort($sort_flags = null) |
||
257 | |||
258 | /** |
||
259 | * Sort an array using a case insensitive "natural order" algorithm |
||
260 | * |
||
261 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
262 | 4 | */ |
|
263 | public function natcasesort() |
||
269 | |||
270 | /** |
||
271 | * Sort entries using a "natural order" algorithm |
||
272 | * |
||
273 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
274 | */ |
||
275 | public function natsort() |
||
281 | |||
282 | /** |
||
283 | * Whether or not an offset exists. |
||
284 | * |
||
285 | * @param int|float|string $offset |
||
286 | 1 | * |
|
287 | * @return bool |
||
288 | 1 | */ |
|
289 | public function offsetExists($offset) |
||
333 | |||
334 | 3 | /** |
|
335 | 3 | * Returns the value at specified offset. |
|
336 | * |
||
337 | 3 | * @param mixed $offset |
|
338 | 3 | * |
|
339 | 3 | * @return mixed <p>Will return null if the offset did not exists.</p> |
|
340 | 3 | */ |
|
341 | public function offsetGet($offset) |
||
345 | |||
346 | /** |
||
347 | * Assigns a value to the specified offset. |
||
348 | * |
||
349 | * @param mixed $offset |
||
350 | * @param mixed $value |
||
351 | */ |
||
352 | 26 | public function offsetSet($offset, $value) |
|
360 | |||
361 | /** |
||
362 | * Unset an offset. |
||
363 | 17 | * |
|
364 | * @param mixed $offset |
||
365 | 17 | */ |
|
366 | 4 | public function offsetUnset($offset) |
|
392 | 2 | ||
393 | /** |
||
394 | 2 | * Serialize the current "Arrayy"-object. |
|
395 | 2 | * |
|
396 | * @return string |
||
397 | 2 | */ |
|
398 | 2 | public function serialize() |
|
402 | 3 | ||
403 | /** |
||
404 | * Sets the iterator classname for the current "Arrayy"-object. |
||
405 | * |
||
406 | * @param string $class |
||
407 | * |
||
408 | * @return void |
||
409 | 1 | * |
|
410 | * @throws \InvalidArgumentException |
||
411 | 1 | */ |
|
412 | public function setIteratorClass($class) |
||
431 | |||
432 | /** |
||
433 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
434 | * |
||
435 | * @param callable $function |
||
436 | * |
||
437 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
438 | * |
||
439 | * @throws \InvalidArgumentException |
||
440 | */ |
||
441 | View Code Duplication | public function uasort($function) |
|
453 | |||
454 | /** |
||
455 | * Sort the entries by keys using a user-defined comparison function. |
||
456 | * |
||
457 | * @param callable $function |
||
458 | * |
||
459 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
460 | * |
||
461 | * @throws \InvalidArgumentException |
||
462 | */ |
||
463 | public function uksort($function) |
||
467 | |||
468 | /** |
||
469 | * Unserialize an string and return this object. |
||
470 | * |
||
471 | * @param string $string |
||
472 | * |
||
473 | * @return static <p>(Mutable)</p> |
||
474 | 5 | */ |
|
475 | public function unserialize($string) |
||
481 | |||
482 | /** |
||
483 | * Add a suffix to each key. |
||
484 | * |
||
485 | * @param string $prefix |
||
486 | 1 | * |
|
487 | * @return static <p>(Immutable) Return an Arrayy object, with the prefixed keys.</p> |
||
488 | 1 | */ |
|
489 | View Code Duplication | public function appendToEachKey($prefix) |
|
504 | |||
505 | /** |
||
506 | * Add a prefix to each value. |
||
507 | * |
||
508 | * @param mixed $prefix |
||
509 | * |
||
510 | * @return static <p>(Immutable) Return an Arrayy object, with the prefixed values.</p> |
||
511 | */ |
||
512 | 2 | View Code Duplication | public function appendToEachValue($prefix) |
529 | |||
530 | 10 | /** |
|
531 | * Convert an array into a object. |
||
532 | 10 | * |
|
533 | * @param array $array PHP array |
||
534 | 10 | * |
|
535 | 2 | * @return \stdClass (object) |
|
536 | */ |
||
537 | protected static function arrayToObject(array $array = array()) |
||
555 | |||
556 | 4 | /** |
|
557 | 4 | * Sort an array in reverse order and maintain index association. |
|
558 | * |
||
559 | 4 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
|
560 | */ |
||
561 | public function arsort() |
||
567 | 1 | ||
568 | 1 | /** |
|
569 | 1 | * Iterate over the current array and execute a callback for each loop. |
|
570 | 4 | * |
|
571 | * @param \Closure $closure |
||
572 | 4 | * |
|
573 | * @return static <p>(Immutable)</p> |
||
574 | */ |
||
575 | View Code Duplication | public function at(\Closure $closure) |
|
585 | |||
586 | /** |
||
587 | * Returns the average value of the current array. |
||
588 | * |
||
589 | * @param int $decimals <p>The number of decimal-numbers to return.</p> |
||
590 | * |
||
591 | * @return int|double <p>The average value.</p> |
||
592 | */ |
||
593 | public function average($decimals = 0) |
||
607 | |||
608 | /** |
||
609 | * @param mixed $path |
||
610 | * @param callable $callable |
||
611 | 4 | * @param null|array $currentOffset |
|
612 | */ |
||
613 | 4 | protected function callAtPath($path, $callable, &$currentOffset = null) |
|
636 | |||
637 | 4 | /** |
|
638 | * Changes all keys in an array. |
||
639 | 4 | * |
|
640 | * @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br /> |
||
641 | 4 | * or <strong>CASE_LOWER</strong> (default)</p> |
|
642 | * |
||
643 | * @return static <p>(Immutable)</p> |
||
644 | */ |
||
645 | public function changeKeyCase($case = CASE_LOWER) |
||
649 | |||
650 | /** |
||
651 | 13 | * Change the path separator of the array wrapper. |
|
652 | * |
||
653 | 13 | * By default, the separator is: "." |
|
654 | * |
||
655 | * @param string $separator <p>Separator to set.</p> |
||
656 | * |
||
657 | * @return static <p>Mutable</p> |
||
658 | */ |
||
659 | public function changeSeparator($separator) |
||
665 | 13 | ||
666 | 13 | /** |
|
667 | 13 | * Create a chunked version of the current array. |
|
668 | * |
||
669 | 13 | * @param int $size <p>Size of each chunk.</p> |
|
670 | 13 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
|
671 | 13 | * |
|
672 | 13 | * @return static <p>(Immutable) A new array of chunks from the original array.</p> |
|
673 | 13 | */ |
|
674 | public function chunk($size, $preserveKeys = false) |
||
680 | |||
681 | /** |
||
682 | * Clean all falsy values from the current array. |
||
683 | * |
||
684 | * @return static <p>(Immutable)</p> |
||
685 | 4 | */ |
|
686 | public function clean() |
||
694 | |||
695 | /** |
||
696 | * WARNING!!! -> Clear the current array. |
||
697 | 1 | * |
|
698 | * @return static <p>(Mutable) Return this Arrayy object, with an empty array.</p> |
||
699 | 1 | */ |
|
700 | public function clear() |
||
706 | |||
707 | /** |
||
708 | * Check if an item is in the current array. |
||
709 | * |
||
710 | * @param string|int|float $value |
||
711 | 9 | * |
|
712 | * @return bool |
||
713 | 9 | */ |
|
714 | public function contains($value) |
||
718 | |||
719 | /** |
||
720 | * Check if an (case-insensitive) string is in the current array. |
||
721 | * |
||
722 | * @param string $value |
||
723 | 1 | * |
|
724 | * @return bool |
||
725 | 1 | */ |
|
726 | public function containsCaseInsensitive($value) |
||
740 | |||
741 | /** |
||
742 | * Check if the given key/index exists in the array. |
||
743 | * |
||
744 | * @param string|int|float $key <p>key/index to search for</p> |
||
745 | * |
||
746 | * @return bool <p>Returns true if the given key/index exists in the array, false otherwise.</p> |
||
747 | 1 | */ |
|
748 | public function containsKey($key) |
||
752 | |||
753 | 1 | /** |
|
754 | * Check if all given needles are present in the array as key/index. |
||
755 | * |
||
756 | * @param array $needles |
||
757 | * |
||
758 | * @return bool <p>Returns true if the given keys/indexes exists in the array, false otherwise.</p> |
||
759 | */ |
||
760 | public function containsKeys(array $needles) |
||
764 | |||
765 | 5 | /** |
|
766 | * alias: for "Arrayy->contains()" |
||
767 | 5 | * |
|
768 | * @see Arrayy::contains() |
||
769 | * |
||
770 | * @param string|int|float $value |
||
771 | * |
||
772 | * @return bool |
||
773 | */ |
||
774 | public function containsValue($value) |
||
778 | |||
779 | 4 | /** |
|
780 | 4 | * Check if all given needles are present in the array. |
|
781 | * |
||
782 | 3 | * @param array $needles |
|
783 | 4 | * |
|
784 | * @return bool <p>Returns true if the given values exists in the array, false otherwise.</p> |
||
785 | 4 | */ |
|
786 | public function containsValues(array $needles) |
||
790 | |||
791 | /** |
||
792 | * Counts all the values of an array |
||
793 | * |
||
794 | * @link http://php.net/manual/en/function.array-count-values.php |
||
795 | 5 | * |
|
796 | * @return static <p> |
||
797 | 5 | * (Immutable) |
|
798 | * An associative Arrayy-object of values from input as |
||
799 | * keys and their count as value. |
||
800 | * </p> |
||
801 | */ |
||
802 | public function countValues() |
||
806 | |||
807 | 5 | /** |
|
808 | * Creates an Arrayy object. |
||
809 | 5 | * |
|
810 | 4 | * @param array $array |
|
811 | * |
||
812 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
813 | 5 | */ |
|
814 | 5 | public static function create($array = array()) |
|
818 | |||
819 | /** |
||
820 | * WARNING: Creates an Arrayy object by reference. |
||
821 | * |
||
822 | * @param array $array |
||
823 | * |
||
824 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
825 | */ |
||
826 | public function createByReference(&$array = array()) |
||
834 | 1 | ||
835 | 1 | /** |
|
836 | 1 | * Create an new Arrayy object via JSON. |
|
837 | * |
||
838 | 1 | * @param string $json |
|
839 | 7 | * |
|
840 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
841 | */ |
||
842 | public static function createFromJson($json) |
||
848 | 8 | ||
849 | 8 | /** |
|
850 | 8 | * Create an new instance filled with values from an object that have implemented ArrayAccess. |
|
851 | 8 | * |
|
852 | * @param \ArrayAccess $object <p>Object that implements ArrayAccess</p> |
||
853 | 8 | * |
|
854 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
855 | */ |
||
856 | public static function createFromObject(\ArrayAccess $object) |
||
866 | |||
867 | 1 | /** |
|
868 | * Create an new instance filled with values from an object. |
||
869 | * |
||
870 | * @param object $object |
||
871 | * |
||
872 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
873 | */ |
||
874 | public static function createFromObjectVars($object) |
||
878 | |||
879 | /** |
||
880 | * Create an new Arrayy object via string. |
||
881 | 5 | * |
|
882 | * @param string $str <p>The input string.</p> |
||
883 | 5 | * @param string|null $delimiter <p>The boundary string.</p> |
|
884 | * @param string|null $regEx <p>Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be |
||
885 | * used.</p> |
||
886 | * |
||
887 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
888 | */ |
||
889 | 5 | public static function createFromString($str, $delimiter, $regEx = null) |
|
915 | 5 | ||
916 | /** |
||
917 | * Create an new instance containing a range of elements. |
||
918 | * |
||
919 | * @param mixed $low <p>First value of the sequence.</p> |
||
920 | * @param mixed $high <p>The sequence is ended upon reaching the end value.</p> |
||
921 | * @param int $step <p>Used as the increment between elements in the sequence.</p> |
||
922 | * |
||
923 | * @return static <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
924 | */ |
||
925 | 12 | public static function createWithRange($low, $high, $step = 1) |
|
929 | 12 | ||
930 | /** |
||
931 | * Custom sort by index via "uksort". |
||
932 | * |
||
933 | * @link http://php.net/manual/en/function.uksort.php |
||
934 | * |
||
935 | * @param callable $function |
||
936 | * |
||
937 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
938 | * |
||
939 | * @throws \InvalidArgumentException |
||
940 | 1 | */ |
|
941 | View Code Duplication | public function customSortKeys($function) |
|
953 | |||
954 | 1 | /** |
|
955 | 1 | * Custom sort by value via "usort". |
|
956 | 1 | * |
|
957 | 1 | * @link http://php.net/manual/en/function.usort.php |
|
958 | 1 | * |
|
959 | 1 | * @param callable $function |
|
960 | 1 | * |
|
961 | 1 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
|
962 | 1 | * |
|
963 | 1 | * @throws \InvalidArgumentException |
|
964 | 1 | */ |
|
965 | View Code Duplication | public function customSortValues($function) |
|
977 | |||
978 | /** |
||
979 | * Return values that are only in the current array. |
||
980 | * |
||
981 | 8 | * @param array $array |
|
982 | * |
||
983 | 8 | * @return static <p>(Immutable)</p> |
|
984 | */ |
||
985 | 8 | public function diff(array $array = array()) |
|
991 | |||
992 | /** |
||
993 | 1 | * Return values that are only in the current multi-dimensional array. |
|
994 | * |
||
995 | 1 | * @param array $array |
|
996 | * @param null|array $helperVariableForRecursion <p>(only for internal usage)</p> |
||
997 | 1 | * |
|
998 | 1 | * @return static <p>(Immutable)</p> |
|
999 | */ |
||
1000 | 1 | public function diffRecursive(array $array = array(), $helperVariableForRecursion = null) |
|
1033 | 1 | ||
1034 | 1 | /** |
|
1035 | * Return values that are only in the new $array. |
||
1036 | 4 | * |
|
1037 | * @param array $array |
||
1038 | 4 | * |
|
1039 | * @return static <p>(Immutable)</p> |
||
1040 | */ |
||
1041 | public function diffReverse(array $array = array()) |
||
1047 | |||
1048 | /** |
||
1049 | * Divide an array into two arrays. One with keys and the other with values. |
||
1050 | * |
||
1051 | * @return static <p>(Immutable)</p> |
||
1052 | */ |
||
1053 | public function divide() |
||
1062 | |||
1063 | /** |
||
1064 | 11 | * Iterate over the current array and modify the array's value. |
|
1065 | 1 | * |
|
1066 | * @param \Closure $closure |
||
1067 | * |
||
1068 | 10 | * @return static <p>(Immutable)</p> |
|
1069 | 6 | */ |
|
1070 | View Code Duplication | public function each(\Closure $closure) |
|
1080 | |||
1081 | /** |
||
1082 | * Check if a value is in the current array using a closure. |
||
1083 | 9 | * |
|
1084 | * @param \Closure $closure |
||
1085 | * |
||
1086 | * @return bool <p>Returns true if the given value is found, false otherwise.</p> |
||
1087 | */ |
||
1088 | public function exists(\Closure $closure) |
||
1100 | |||
1101 | /** |
||
1102 | * create a fallback for array |
||
1103 | * |
||
1104 | * 1. use the current array, if it's a array |
||
1105 | * 2. call "getArray()" on object, if there is a "Arrayy"-object |
||
1106 | * 3. fallback to empty array, if there is nothing |
||
1107 | * 4. call "createFromObject()" on object, if there is a "\ArrayAccess"-object |
||
1108 | * 5. call "__toArray()" on object, if the method exists |
||
1109 | * 6. cast a string or object with "__toString()" into an array |
||
1110 | * 7. throw a "InvalidArgumentException"-Exception |
||
1111 | * |
||
1112 | * @param $array |
||
1113 | * |
||
1114 | * @return array |
||
1115 | * |
||
1116 | * @throws \InvalidArgumentException |
||
1117 | */ |
||
1118 | protected function fallbackForArray(&$array) |
||
1160 | 1 | ||
1161 | 1 | /** |
|
1162 | 1 | * Find all items in an array that pass the truth test. |
|
1163 | 1 | * |
|
1164 | 1 | * @param \Closure|null $closure [optional] <p> |
|
1165 | 1 | * The callback function to use |
|
1166 | * </p> |
||
1167 | * <p> |
||
1168 | 9 | * If no callback is supplied, all entries of |
|
1169 | * input equal to false (see |
||
1170 | * converting to |
||
1171 | * boolean) will be removed. |
||
1172 | * </p> |
||
1173 | * |
||
1174 | * * @param int $flag [optional] <p> |
||
1175 | * Flag determining what arguments are sent to <i>callback</i>: |
||
1176 | * </p><ul> |
||
1177 | * <li> |
||
1178 | * <b>ARRAY_FILTER_USE_KEY</b> [1] - pass key as the only argument |
||
1179 | * to <i>callback</i> instead of the value</span> |
||
1180 | * </li> |
||
1181 | * <li> |
||
1182 | * <b>ARRAY_FILTER_USE_BOTH</b> [2] - pass both value and key as |
||
1183 | * arguments to <i>callback</i> instead of the value</span> |
||
1184 | * </li> |
||
1185 | * </ul> |
||
1186 | * |
||
1187 | * @return static <p>(Immutable)</p> |
||
1188 | */ |
||
1189 | public function filter($closure = null, $flag = 0) |
||
1230 | |||
1231 | /** |
||
1232 | 1 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
|
1233 | * within that. |
||
1234 | * |
||
1235 | 1 | * @param string $property |
|
1236 | 1 | * @param string $value |
|
1237 | * @param string $comparisonOp |
||
1238 | 1 | * <p> |
|
1239 | 1 | * 'eq' (equals),<br /> |
|
1240 | 1 | * 'gt' (greater),<br /> |
|
1241 | * 'gte' || 'ge' (greater or equals),<br /> |
||
1242 | 1 | * 'lt' (less),<br /> |
|
1243 | 1 | * 'lte' || 'le' (less or equals),<br /> |
|
1244 | 1 | * 'ne' (not equals),<br /> |
|
1245 | 1 | * 'contains',<br /> |
|
1246 | * 'notContains',<br /> |
||
1247 | 1 | * 'newer' (via strtotime),<br /> |
|
1248 | 1 | * 'older' (via strtotime),<br /> |
|
1249 | 1 | * </p> |
|
1250 | * |
||
1251 | 1 | * @return static <p>(Immutable)</p> |
|
1252 | */ |
||
1253 | 1 | public function filterBy($property, $value, $comparisonOp = null) |
|
1318 | 28 | ||
1319 | 7 | /** |
|
1320 | 7 | * Find the first item in an array that passes the truth test, |
|
1321 | 7 | * otherwise return false |
|
1322 | 21 | * |
|
1323 | 21 | * @param \Closure $closure |
|
1324 | 21 | * |
|
1325 | * @return mixed|false <p>Return false if we did not find the value.</p> |
||
1326 | */ |
||
1327 | 28 | public function find(\Closure $closure) |
|
1337 | 26 | ||
1338 | /** |
||
1339 | 26 | * find by ... |
|
1340 | 11 | * |
|
1341 | 11 | * @param string $property |
|
1342 | 15 | * @param string $value |
|
1343 | 15 | * @param string $comparisonOp |
|
1344 | * |
||
1345 | * @return static <p>(Immutable)</p> |
||
1346 | 26 | */ |
|
1347 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
1351 | |||
1352 | /** |
||
1353 | * Get the first value from the current array. |
||
1354 | 1 | * |
|
1355 | * @return mixed <p>Return null if there wasn't a element.</p> |
||
1356 | 1 | */ |
|
1357 | public function first() |
||
1368 | |||
1369 | /** |
||
1370 | * Get the first value(s) from the current array. |
||
1371 | 61 | * |
|
1372 | * @param int|null $number <p>How many values you will take?</p> |
||
1373 | * |
||
1374 | * @return static <p>(Immutable)</p> |
||
1375 | 61 | */ |
|
1376 | 3 | public function firstsImmutable($number = null) |
|
1389 | 2 | ||
1390 | 2 | /** |
|
1391 | * Get the first value(s) from the current array. |
||
1392 | 61 | * |
|
1393 | 51 | * @param int|null $number <p>How many values you will take?</p> |
|
1394 | 6 | * |
|
1395 | * @return static <p>(Mutable)</p> |
||
1396 | */ |
||
1397 | 47 | public function firstsMutable($number = null) |
|
1408 | |||
1409 | 5 | /** |
|
1410 | * Exchanges all keys with their associated values in an array. |
||
1411 | * |
||
1412 | * @return static <p>(Immutable)</p> |
||
1413 | 5 | */ |
|
1414 | public function flip() |
||
1420 | |||
1421 | 496 | /** |
|
1422 | * Get a value from an array (optional using dot-notation). |
||
1423 | 496 | * |
|
1424 | * @param string $key <p>The key to look for.</p> |
||
1425 | 496 | * @param mixed $fallback <p>Value to fallback to.</p> |
|
1426 | * @param array $array <p>The array to get from, if it's set to "null" we use the current array from the |
||
1427 | * class.</p> |
||
1428 | * |
||
1429 | * @return mixed |
||
1430 | */ |
||
1431 | public function get($key, $fallback = null, $array = null) |
||
1475 | |||
1476 | 1 | /** |
|
1477 | * Get the current array from the "Arrayy"-object. |
||
1478 | 1 | * |
|
1479 | * @return array |
||
1480 | 1 | */ |
|
1481 | public function getArray() |
||
1487 | |||
1488 | /** |
||
1489 | * Returns the values from a single column of the input array, identified by |
||
1490 | 38 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
|
1491 | * |
||
1492 | 38 | * Info: Optionally, you may provide an $indexKey to index the values in the returned |
|
1493 | 10 | * array by the values from the $indexKey column in the input array. |
|
1494 | * |
||
1495 | 10 | * @param mixed $columnKey |
|
1496 | 2 | * @param mixed $indexKey |
|
1497 | 2 | * |
|
1498 | 8 | * @return static <p>(Immutable)</p> |
|
1499 | */ |
||
1500 | 10 | public function getColumn($columnKey = null, $indexKey = null) |
|
1506 | 38 | ||
1507 | /** |
||
1508 | * Get correct PHP constant for direction. |
||
1509 | * |
||
1510 | 38 | * @param int|string $direction |
|
1511 | * |
||
1512 | * @return int |
||
1513 | */ |
||
1514 | protected function getDirection($direction) |
||
1536 | |||
1537 | /** |
||
1538 | * alias: for "Arrayy->keys()" |
||
1539 | * |
||
1540 | * @see Arrayy::keys() |
||
1541 | * |
||
1542 | * @return static <p>(Immutable)</p> |
||
1543 | */ |
||
1544 | 3 | public function getKeys() |
|
1548 | |||
1549 | /** |
||
1550 | * Get the current array from the "Arrayy"-object as object. |
||
1551 | * |
||
1552 | * @return \stdClass (object) |
||
1553 | */ |
||
1554 | public function getObject() |
||
1558 | 9 | ||
1559 | /** |
||
1560 | 9 | * alias: for "Arrayy->randomImmutable()" |
|
1561 | * |
||
1562 | * @see Arrayy::randomImmutable() |
||
1563 | * |
||
1564 | * @return static <p>(Immutable)</p> |
||
1565 | */ |
||
1566 | public function getRandom() |
||
1570 | 3 | ||
1571 | /** |
||
1572 | 3 | * alias: for "Arrayy->randomKey()" |
|
1573 | * |
||
1574 | * @see Arrayy::randomKey() |
||
1575 | * |
||
1576 | * @return mixed <p>Get a key/index or null if there wasn't a key/index.</p> |
||
1577 | */ |
||
1578 | public function getRandomKey() |
||
1582 | |||
1583 | /** |
||
1584 | 6 | * alias: for "Arrayy->randomKeys()" |
|
1585 | * |
||
1586 | 6 | * @see Arrayy::randomKeys() |
|
1587 | * |
||
1588 | * @param int $number |
||
1589 | * |
||
1590 | * @return static <p>(Immutable)</p> |
||
1591 | */ |
||
1592 | public function getRandomKeys($number) |
||
1596 | |||
1597 | 3 | /** |
|
1598 | * alias: for "Arrayy->randomValue()" |
||
1599 | 3 | * |
|
1600 | 3 | * @see Arrayy::randomValue() |
|
1601 | * |
||
1602 | * @return mixed <p>get a random value or null if there wasn't a value.</p> |
||
1603 | 3 | */ |
|
1604 | public function getRandomValue() |
||
1608 | 3 | ||
1609 | /** |
||
1610 | * alias: for "Arrayy->randomValues()" |
||
1611 | * |
||
1612 | 3 | * @see Arrayy::randomValues() |
|
1613 | 3 | * |
|
1614 | 3 | * @param int $number |
|
1615 | * |
||
1616 | * @return static <p>(Immutable)</p> |
||
1617 | 3 | */ |
|
1618 | 2 | public function getRandomValues($number) |
|
1622 | 1 | ||
1623 | 1 | /** |
|
1624 | * Group values from a array according to the results of a closure. |
||
1625 | 2 | * |
|
1626 | * @param string $grouper <p>A callable function name.</p> |
||
1627 | 3 | * @param bool $saveKeys |
|
1628 | * |
||
1629 | 3 | * @return static <p>(Immutable)</p> |
|
1630 | */ |
||
1631 | public function group($grouper, $saveKeys = false) |
||
1665 | 28 | ||
1666 | 28 | /** |
|
1667 | 28 | * Check if an array has a given key. |
|
1668 | * |
||
1669 | 28 | * @param mixed $key |
|
1670 | 28 | * |
|
1671 | 28 | * @return bool |
|
1672 | 28 | */ |
|
1673 | 28 | public function has($key) |
|
1680 | |||
1681 | /** |
||
1682 | * Implodes an array. |
||
1683 | * |
||
1684 | * @param string $glue |
||
1685 | * |
||
1686 | * @return string |
||
1687 | */ |
||
1688 | public function implode($glue = '') |
||
1692 | |||
1693 | 3 | /** |
|
1694 | * @param string $glue |
||
1695 | 3 | * @param string|array $pieces |
|
1696 | * |
||
1697 | 3 | * @return string |
|
1698 | 3 | */ |
|
1699 | 2 | protected static function implode_recursive($glue = '', $pieces) |
|
1716 | |||
1717 | 4 | /** |
|
1718 | * Given a list and an iterate-function that returns |
||
1719 | * a key for each element in the list (or a property name), |
||
1720 | * returns an object with an index of each item. |
||
1721 | * |
||
1722 | * Just like groupBy, but for when you know your keys are unique. |
||
1723 | * |
||
1724 | * @param mixed $key |
||
1725 | * |
||
1726 | * @return static <p>(Immutable)</p> |
||
1727 | 12 | */ |
|
1728 | public function indexBy($key) |
||
1740 | |||
1741 | /** |
||
1742 | * alias: for "Arrayy->searchIndex()" |
||
1743 | * |
||
1744 | * @see Arrayy::searchIndex() |
||
1745 | * |
||
1746 | * @param mixed $value <p>The value to search for.</p> |
||
1747 | * |
||
1748 | * @return mixed |
||
1749 | 410 | */ |
|
1750 | public function indexOf($value) |
||
1754 | |||
1755 | /** |
||
1756 | * Get everything but the last..$to items. |
||
1757 | * |
||
1758 | * @param int $to |
||
1759 | * |
||
1760 | * @return static <p>(Immutable)</p> |
||
1761 | */ |
||
1762 | 18 | public function initial($to = 1) |
|
1768 | |||
1769 | /** |
||
1770 | * @param mixed $value |
||
1771 | */ |
||
1772 | protected function internalGetArray(&$value) |
||
1789 | |||
1790 | /** |
||
1791 | * Internal mechanics of remove method. |
||
1792 | 30 | * |
|
1793 | * @param string $key |
||
1794 | 30 | * |
|
1795 | * @return boolean |
||
1796 | */ |
||
1797 | protected function internalRemove($key) |
||
1818 | 30 | ||
1819 | /** |
||
1820 | * Internal mechanic of set method. |
||
1821 | * |
||
1822 | * @param string $key |
||
1823 | * @param mixed $value |
||
1824 | * |
||
1825 | * @return bool |
||
1826 | */ |
||
1827 | protected function internalSet($key, $value) |
||
1855 | |||
1856 | 1 | /** |
|
1857 | 1 | * Return an array with all elements found in input array. |
|
1858 | 1 | * |
|
1859 | * @param array $search |
||
1860 | * |
||
1861 | 1 | * @return static <p>(Immutable)</p> |
|
1862 | 1 | */ |
|
1863 | 1 | public function intersection(array $search) |
|
1867 | 1 | ||
1868 | /** |
||
1869 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
1870 | * |
||
1871 | * @param array $search |
||
1872 | * |
||
1873 | * @return bool |
||
1874 | */ |
||
1875 | 15 | public function intersects(array $search) |
|
1879 | |||
1880 | /** |
||
1881 | 13 | * Invoke a function on all of an array's values. |
|
1882 | 13 | * |
|
1883 | 11 | * @param mixed $callable |
|
1884 | * @param mixed $arguments |
||
1885 | 3 | * |
|
1886 | * @return static <p>(Immutable)</p> |
||
1887 | 3 | */ |
|
1888 | public function invoke($callable, $arguments = array()) |
||
1904 | |||
1905 | /** |
||
1906 | * Check whether array is associative or not. |
||
1907 | * |
||
1908 | * @return bool <p>Returns true if associative, false otherwise.</p> |
||
1909 | */ |
||
1910 | public function isAssoc() |
||
1924 | |||
1925 | /** |
||
1926 | * Check whether the array is empty or not. |
||
1927 | 5 | * |
|
1928 | * @return bool <p>Returns true if empty, false otherwise.</p> |
||
1929 | 5 | */ |
|
1930 | 2 | public function isEmpty() |
|
1934 | 4 | ||
1935 | 2 | /** |
|
1936 | * Check if the current array is equal to the given "$array" or not. |
||
1937 | 3 | * |
|
1938 | * @param array $array |
||
1939 | 2 | * |
|
1940 | * @return bool |
||
1941 | */ |
||
1942 | public function isEqual(array $array) |
||
1946 | |||
1947 | 1 | /** |
|
1948 | * Check if the current array is a multi-array. |
||
1949 | 1 | * |
|
1950 | * @return bool |
||
1951 | */ |
||
1952 | public function isMultiArray() |
||
1956 | |||
1957 | /** |
||
1958 | * Check whether array is numeric or not. |
||
1959 | * |
||
1960 | * @return bool <p>Returns true if numeric, false otherwise.</p> |
||
1961 | */ |
||
1962 | public function isNumeric() |
||
1976 | |||
1977 | /** |
||
1978 | * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not. |
||
1979 | * |
||
1980 | * @return bool |
||
1981 | 4 | */ |
|
1982 | public function isSequential() |
||
1986 | |||
1987 | /** |
||
1988 | * @return array |
||
1989 | */ |
||
1990 | public function jsonSerialize() |
||
1994 | |||
1995 | 4 | /** |
|
1996 | * Get all keys from the current array. |
||
1997 | * |
||
1998 | * @return static <p>(Immutable)</p> |
||
1999 | */ |
||
2000 | public function keys() |
||
2004 | |||
2005 | 13 | /** |
|
2006 | * Sort an array by key in reverse order. |
||
2007 | 13 | * |
|
2008 | 1 | * @param int $sort_flags [optional] <p> |
|
2009 | * You may modify the behavior of the sort using the optional |
||
2010 | * parameter sort_flags, for details |
||
2011 | 12 | * see sort. |
|
2012 | 8 | * </p> |
|
2013 | * |
||
2014 | 8 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
|
2015 | 1 | */ |
|
2016 | 1 | public function krsort($sort_flags = null) |
|
2022 | 4 | ||
2023 | 4 | /** |
|
2024 | * Get the last value from the current array. |
||
2025 | * |
||
2026 | 12 | * @return mixed <p>Return null if there wasn't a element.</p> |
|
2027 | */ |
||
2028 | public function last() |
||
2032 | |||
2033 | /** |
||
2034 | * Get the last value(s) from the current array. |
||
2035 | * |
||
2036 | 13 | * @param int|null $number |
|
2037 | * |
||
2038 | 13 | * @return static <p>(Immutable)</p> |
|
2039 | 1 | */ |
|
2040 | public function lastsImmutable($number = null) |
||
2063 | |||
2064 | /** |
||
2065 | * Get the last value(s) from the current array. |
||
2066 | * |
||
2067 | * @param int|null $number |
||
2068 | * |
||
2069 | 10 | * @return static <p>(Mutable)</p> |
|
2070 | */ |
||
2071 | 10 | public function lastsMutable($number = null) |
|
2094 | |||
2095 | /** |
||
2096 | 15 | * Count the values from the current array. |
|
2097 | * |
||
2098 | 15 | * alias: for "Arrayy->size()" |
|
2099 | 2 | * |
|
2100 | * @see Arrayy::size() |
||
2101 | * |
||
2102 | * @return int |
||
2103 | 13 | */ |
|
2104 | public function length() |
||
2108 | 13 | ||
2109 | 7 | /** |
|
2110 | * Apply the given function to the every element of the array, |
||
2111 | 9 | * collecting the results. |
|
2112 | * |
||
2113 | 7 | * @param callable $callable |
|
2114 | * |
||
2115 | * @return static <p>(Immutable) Arrayy object with modified elements.</p> |
||
2116 | */ |
||
2117 | public function map($callable) |
||
2123 | 14 | ||
2124 | /** |
||
2125 | 14 | * Check if all items in current array match a truth test. |
|
2126 | 2 | * |
|
2127 | * @param \Closure $closure |
||
2128 | * |
||
2129 | * @return bool |
||
2130 | 12 | */ |
|
2131 | View Code Duplication | public function matches(\Closure $closure) |
|
2150 | 10 | ||
2151 | 1 | /** |
|
2152 | * Check if any item in the current array matches a truth test. |
||
2153 | * |
||
2154 | 9 | * @param \Closure $closure |
|
2155 | * |
||
2156 | * @return bool |
||
2157 | */ |
||
2158 | View Code Duplication | public function matchesAny(\Closure $closure) |
|
2177 | |||
2178 | /** |
||
2179 | * Get the max value from an array. |
||
2180 | * |
||
2181 | * @return mixed |
||
2182 | */ |
||
2183 | public function max() |
||
2191 | 16 | ||
2192 | 4 | /** |
|
2193 | 4 | * Merge the new $array into the current array. |
|
2194 | 12 | * |
|
2195 | * - keep key,value from the current array, also if the index is in the new $array |
||
2196 | * |
||
2197 | 16 | * @param array $array |
|
2198 | * @param bool $recursive |
||
2199 | * |
||
2200 | * @return static <p>(Immutable)</p> |
||
2201 | */ |
||
2202 | View Code Duplication | public function mergeAppendKeepIndex(array $array = array(), $recursive = false) |
|
2212 | 16 | ||
2213 | 4 | /** |
|
2214 | 4 | * Merge the new $array into the current array. |
|
2215 | 12 | * |
|
2216 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
2217 | * - create new indexes |
||
2218 | 16 | * |
|
2219 | * @param array $array |
||
2220 | * @param bool $recursive |
||
2221 | * |
||
2222 | * @return static <p>(Immutable)</p> |
||
2223 | */ |
||
2224 | View Code Duplication | public function mergeAppendNewIndex(array $array = array(), $recursive = false) |
|
2234 | 17 | ||
2235 | 4 | /** |
|
2236 | 4 | * Merge the the current array into the $array. |
|
2237 | 13 | * |
|
2238 | * - use key,value from the new $array, also if the index is in the current array |
||
2239 | * |
||
2240 | 17 | * @param array $array |
|
2241 | * @param bool $recursive |
||
2242 | * |
||
2243 | * @return static <p>(Immutable)</p> |
||
2244 | */ |
||
2245 | View Code Duplication | public function mergePrependKeepIndex(array $array = array(), $recursive = false) |
|
2255 | |||
2256 | /** |
||
2257 | * Merge the current array into the new $array. |
||
2258 | * |
||
2259 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
2260 | * - create new indexes |
||
2261 | * |
||
2262 | * @param array $array |
||
2263 | * @param bool $recursive |
||
2264 | * |
||
2265 | * @return static <p>(Immutable)</p> |
||
2266 | */ |
||
2267 | 1 | View Code Duplication | public function mergePrependNewIndex(array $array = array(), $recursive = false) |
2277 | 1 | ||
2278 | 1 | /** |
|
2279 | 1 | * Get the min value from an array. |
|
2280 | 1 | * |
|
2281 | 1 | * @return mixed |
|
2282 | 1 | */ |
|
2283 | 1 | public function min() |
|
2291 | |||
2292 | 1 | /** |
|
2293 | * Move an array element to a new index. |
||
2294 | * |
||
2295 | * cherry-picked from: http://stackoverflow.com/questions/12624153/move-an-array-element-to-a-new-index-in-php |
||
2296 | * |
||
2297 | * @param int|string $from |
||
2298 | * @param int|string $to |
||
2299 | * |
||
2300 | * @return static <p>(Immutable)</p> |
||
2301 | */ |
||
2302 | public function moveElement($from, $to) |
||
2329 | 16 | ||
2330 | /** |
||
2331 | 16 | * Convert a object into an array. |
|
2332 | * |
||
2333 | * @param object $object |
||
2334 | * |
||
2335 | * @return mixed |
||
2336 | */ |
||
2337 | protected static function objectToArray($object) |
||
2349 | |||
2350 | /** |
||
2351 | 8 | * Get a subset of the items from the given array. |
|
2352 | * |
||
2353 | * @param mixed[] $keys |
||
2354 | * |
||
2355 | * @return static <p>(Immutable)</p> |
||
2356 | */ |
||
2357 | public function only(array $keys) |
||
2363 | 4 | ||
2364 | 4 | /** |
|
2365 | * Pad array to the specified size with a given value. |
||
2366 | 4 | * |
|
2367 | * @param int $size <p>Size of the result array.</p> |
||
2368 | * @param mixed $value <p>Empty value by default.</p> |
||
2369 | * |
||
2370 | * @return static <p>(Immutable) Arrayy object padded to $size with $value.</p> |
||
2371 | */ |
||
2372 | public function pad($size, $value) |
||
2378 | 17 | ||
2379 | /** |
||
2380 | * Pop a specified value off the end of the current array. |
||
2381 | * |
||
2382 | 17 | * @return mixed <p>(Mutable) The popped element from the current array.</p> |
|
2383 | 14 | */ |
|
2384 | public function pop() |
||
2388 | 5 | ||
2389 | 5 | /** |
|
2390 | * Prepend a (key) + value to the current array. |
||
2391 | 5 | * |
|
2392 | * @param mixed $value |
||
2393 | * @param mixed $key |
||
2394 | * |
||
2395 | * @return static <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
2396 | */ |
||
2397 | public function prepend($value, $key = null) |
||
2408 | |||
2409 | 4 | /** |
|
2410 | * Add a suffix to each key. |
||
2411 | * |
||
2412 | * @param string $suffix |
||
2413 | * |
||
2414 | * @return static <p>(Immutable) Return an Arrayy object, with the prepended keys.</p> |
||
2415 | */ |
||
2416 | View Code Duplication | public function prependToEachKey($suffix) |
|
2432 | 3 | ||
2433 | 3 | /** |
|
2434 | * Add a suffix to each value. |
||
2435 | * |
||
2436 | 11 | * @param mixed $suffix |
|
2437 | * |
||
2438 | 11 | * @return static <p>(Immutable) Return an Arrayy object, with the prepended values.</p> |
|
2439 | */ |
||
2440 | View Code Duplication | public function prependToEachValue($suffix) |
|
2457 | |||
2458 | 7 | /** |
|
2459 | * Push one or more values onto the end of array at once. |
||
2460 | * |
||
2461 | 11 | * @return static <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p> |
|
2462 | */ |
||
2463 | 11 | View Code Duplication | public function push(/* variadic arguments allowed */) |
2472 | |||
2473 | 4 | /** |
|
2474 | * Get a random value from the current array. |
||
2475 | 4 | * |
|
2476 | * @param null|int $number <p>How many values you will take?</p> |
||
2477 | * |
||
2478 | * @return static <p>(Immutable)</p> |
||
2479 | 4 | */ |
|
2480 | public function randomImmutable($number = null) |
||
2497 | |||
2498 | /** |
||
2499 | * Pick a random key/index from the keys of this array. |
||
2500 | * |
||
2501 | * @return mixed <p>Get a key/index or null if there wasn't a key/index.</p> |
||
2502 | * |
||
2503 | * @throws \RangeException If array is empty |
||
2504 | */ |
||
2505 | public function randomKey() |
||
2515 | 9 | ||
2516 | /** |
||
2517 | 9 | * Pick a given number of random keys/indexes out of this array. |
|
2518 | * |
||
2519 | * @param int $number <p>The number of keys/indexes (should be <= $this->count())</p> |
||
2520 | * |
||
2521 | * @return static <p>(Immutable)</p> |
||
2522 | * |
||
2523 | * @throws \RangeException If array is empty |
||
2524 | */ |
||
2525 | public function randomKeys($number) |
||
2544 | |||
2545 | /** |
||
2546 | 9 | * Get a random value from the current array. |
|
2547 | * |
||
2548 | 9 | * @param null|int $number <p>How many values you will take?</p> |
|
2549 | * |
||
2550 | 9 | * @return static <p>(Mutable)</p> |
|
2551 | */ |
||
2552 | public function randomMutable($number = null) |
||
2569 | |||
2570 | 1 | /** |
|
2571 | * Pick a random value from the values of this array. |
||
2572 | * |
||
2573 | * @return mixed <p>Get a random value or null if there wasn't a value.</p> |
||
2574 | */ |
||
2575 | public function randomValue() |
||
2585 | |||
2586 | /** |
||
2587 | * Pick a given number of random values out of this array. |
||
2588 | * |
||
2589 | * @param int $number |
||
2590 | * |
||
2591 | 18 | * @return static <p>(Mutable)</p> |
|
2592 | */ |
||
2593 | 18 | public function randomValues($number) |
|
2599 | |||
2600 | /** |
||
2601 | 7 | * Get a random value from an array, with the ability to skew the results. |
|
2602 | * |
||
2603 | 7 | * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
|
2604 | 7 | * |
|
2605 | * @param array $array |
||
2606 | 7 | * @param null|int $number <p>How many values you will take?</p> |
|
2607 | * |
||
2608 | * @return static <p>(Immutable)</p> |
||
2609 | */ |
||
2610 | public function randomWeighted(array $array, $number = null) |
||
2623 | |||
2624 | /** |
||
2625 | * Reduce the current array via callable e.g. anonymous-function. |
||
2626 | * |
||
2627 | * @param mixed $callable |
||
2628 | * @param array $init |
||
2629 | 7 | * |
|
2630 | * @return static <p>(Immutable)</p> |
||
2631 | 7 | */ |
|
2632 | 7 | public function reduce($callable, array $init = array()) |
|
2644 | |||
2645 | 7 | /** |
|
2646 | * Create a numerically re-indexed Arrayy object. |
||
2647 | * |
||
2648 | * @return static <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p> |
||
2649 | */ |
||
2650 | public function reindex() |
||
2656 | |||
2657 | 1 | /** |
|
2658 | 1 | * Return all items that fail the truth test. |
|
2659 | * |
||
2660 | * @param \Closure $closure |
||
2661 | 1 | * |
|
2662 | * @return static <p>(Immutable)</p> |
||
2663 | */ |
||
2664 | View Code Duplication | public function reject(\Closure $closure) |
|
2676 | |||
2677 | 2 | /** |
|
2678 | * Remove a value from the current array (optional using dot-notation). |
||
2679 | * |
||
2680 | * @param mixed $key |
||
2681 | * |
||
2682 | * @return static <p>(Immutable)</p> |
||
2683 | */ |
||
2684 | public function remove($key) |
||
2699 | |||
2700 | /** |
||
2701 | 2 | * Remove the first value from the current array. |
|
2702 | * |
||
2703 | 2 | * @return static <p>(Immutable)</p> |
|
2704 | */ |
||
2705 | 2 | public function removeFirst() |
|
2712 | |||
2713 | /** |
||
2714 | * Remove the last value from the current array. |
||
2715 | 1 | * |
|
2716 | * @return static <p>(Immutable)</p> |
||
2717 | 1 | */ |
|
2718 | 1 | public function removeLast() |
|
2725 | |||
2726 | /** |
||
2727 | * Removes a particular value from an array (numeric or associative). |
||
2728 | * |
||
2729 | * @param mixed $value |
||
2730 | * |
||
2731 | 3 | * @return static <p>(Immutable)</p> |
|
2732 | */ |
||
2733 | 3 | public function removeValue($value) |
|
2751 | 1 | ||
2752 | /** |
||
2753 | 1 | * Generate array of repeated arrays. |
|
2754 | * |
||
2755 | 1 | * @param int $times <p>How many times has to be repeated.</p> |
|
2756 | * |
||
2757 | 1 | * @return Arrayy |
|
2758 | */ |
||
2759 | 1 | public function repeat($times) |
|
2767 | |||
2768 | /** |
||
2769 | 15 | * Replace a key with a new key/value pair. |
|
2770 | * |
||
2771 | 15 | * @param $replace |
|
2772 | * @param $key |
||
2773 | 15 | * @param $value |
|
2774 | * |
||
2775 | * @return static <p>(Immutable)</p> |
||
2776 | */ |
||
2777 | public function replace($replace, $key, $value) |
||
2783 | 8 | ||
2784 | /** |
||
2785 | 8 | * Create an array using the current array as values and the other array as keys. |
|
2786 | * |
||
2787 | * @param array $keys <p>An array of keys.</p> |
||
2788 | * |
||
2789 | * @return static <p>(Immutable) Arrayy object with keys from the other array.</p> |
||
2790 | */ |
||
2791 | public function replaceAllKeys(array $keys) |
||
2797 | |||
2798 | /** |
||
2799 | 4 | * Create an array using the current array as keys and the other array as values. |
|
2800 | * |
||
2801 | 4 | * @param array $array <p>An array o values.</p> |
|
2802 | * |
||
2803 | 4 | * @return static <p>(Immutable) Arrayy object with values from the other array.</p> |
|
2804 | */ |
||
2805 | public function replaceAllValues(array $array) |
||
2811 | |||
2812 | /** |
||
2813 | 20 | * Replace the keys in an array with another set. |
|
2814 | * |
||
2815 | 20 | * @param array $keys <p>An array of keys matching the array's size</p> |
|
2816 | * |
||
2817 | * @return static <p>(Immutable)</p> |
||
2818 | */ |
||
2819 | public function replaceKeys(array $keys) |
||
2826 | |||
2827 | /** |
||
2828 | 9 | * Replace the first matched value in an array. |
|
2829 | * |
||
2830 | 9 | * @param mixed $search |
|
2831 | * @param mixed $replacement |
||
2832 | * |
||
2833 | * @return static <p>(Immutable)</p> |
||
2834 | */ |
||
2835 | 9 | public function replaceOneValue($search, $replacement = '') |
|
2846 | |||
2847 | /** |
||
2848 | * Replace values in the current array. |
||
2849 | * |
||
2850 | * @param string $search <p>The string to replace.</p> |
||
2851 | * @param string $replacement <p>What to replace it with.</p> |
||
2852 | * |
||
2853 | * @return static <p>(Immutable)</p> |
||
2854 | */ |
||
2855 | 17 | public function replaceValues($search, $replacement = '') |
|
2865 | |||
2866 | /** |
||
2867 | * Get the last elements from index $from until the end of this array. |
||
2868 | * |
||
2869 | * @param int $from |
||
2870 | * |
||
2871 | * @return static <p>(Immutable)</p> |
||
2872 | 11 | */ |
|
2873 | public function rest($from = 1) |
||
2879 | 11 | ||
2880 | /** |
||
2881 | * Return the array in the reverse order. |
||
2882 | * |
||
2883 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
2884 | */ |
||
2885 | public function reverse() |
||
2891 | |||
2892 | /** |
||
2893 | * Sort an array in reverse order. |
||
2894 | * |
||
2895 | * @param int $sort_flags [optional] <p> |
||
2896 | * You may modify the behavior of the sort using the optional |
||
2897 | 1 | * parameter sort_flags, for details |
|
2898 | * see sort. |
||
2899 | 1 | * </p> |
|
2900 | * |
||
2901 | 1 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
|
2902 | */ |
||
2903 | 1 | public function rsort($sort_flags = null) |
|
2909 | |||
2910 | /** |
||
2911 | 93 | * Search for the first index of the current array via $value. |
|
2912 | * |
||
2913 | 93 | * @param mixed $value |
|
2914 | * |
||
2915 | * @return int|float|string |
||
2916 | */ |
||
2917 | public function searchIndex($value) |
||
2921 | |||
2922 | /** |
||
2923 | * Search for the value of the current array via $index. |
||
2924 | * |
||
2925 | 4 | * @param mixed $index |
|
2926 | * |
||
2927 | 4 | * @return static <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p> |
|
2928 | */ |
||
2929 | 4 | public function searchValue($index) |
|
2950 | |||
2951 | /** |
||
2952 | * Set a value for the current array (optional using dot-notation). |
||
2953 | * |
||
2954 | * @param string $key <p>The key to set.</p> |
||
2955 | * @param mixed $value <p>Its value.</p> |
||
2956 | * |
||
2957 | * @return static <p>(Immutable)</p> |
||
2958 | */ |
||
2959 | public function set($key, $value) |
||
2965 | 18 | ||
2966 | /** |
||
2967 | * Get a value from a array and set it if it was not. |
||
2968 | * |
||
2969 | * WARNING: this method only set the value, if the $key is not already set |
||
2970 | * |
||
2971 | * @param string $key <p>The key</p> |
||
2972 | * @param mixed $fallback <p>The default value to set if it isn't.</p> |
||
2973 | * |
||
2974 | * @return mixed <p>(Mutable)</p> |
||
2975 | */ |
||
2976 | 1 | public function setAndGet($key, $fallback = null) |
|
2985 | |||
2986 | /** |
||
2987 | * Shifts a specified value off the beginning of array. |
||
2988 | * |
||
2989 | 1 | * @return mixed <p>(Mutable) A shifted element from the current array.</p> |
|
2990 | */ |
||
2991 | 1 | public function shift() |
|
2995 | |||
2996 | /** |
||
2997 | * Shuffle the current array. |
||
2998 | * |
||
2999 | * @return static <p>(Immutable)</p> |
||
3000 | */ |
||
3001 | public function shuffle() |
||
3009 | 1 | ||
3010 | 1 | /** |
|
3011 | * Get the size of an array. |
||
3012 | * |
||
3013 | 1 | * @return int |
|
3014 | 1 | */ |
|
3015 | public function size() |
||
3019 | 1 | ||
3020 | /** |
||
3021 | 1 | * Extract a slice of the array. |
|
3022 | * |
||
3023 | 1 | * @param int $offset <p>Slice begin index.</p> |
|
3024 | 1 | * @param int|null $length <p>Length of the slice.</p> |
|
3025 | 1 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
|
3026 | * |
||
3027 | * @return static <p>A slice of the original array with length $length.</p> |
||
3028 | */ |
||
3029 | 1 | public function slice($offset, $length = null, $preserveKeys = false) |
|
3035 | |||
3036 | /** |
||
3037 | * Sort the current array and optional you can keep the keys. |
||
3038 | * |
||
3039 | * @param integer $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
3040 | * @param integer $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
3041 | * <strong>SORT_NATURAL</strong></p> |
||
3042 | * @param bool $keepKeys |
||
3043 | 18 | * |
|
3044 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
3045 | 18 | */ |
|
3046 | public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
||
3052 | 13 | ||
3053 | 13 | /** |
|
3054 | 13 | * Sort the current array by key. |
|
3055 | 13 | * |
|
3056 | 13 | * @link http://php.net/manual/en/function.ksort.php |
|
3057 | * @link http://php.net/manual/en/function.krsort.php |
||
3058 | 18 | * |
|
3059 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
3060 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
3061 | * <strong>SORT_NATURAL</strong></p> |
||
3062 | * |
||
3063 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
3064 | */ |
||
3065 | public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR) |
||
3071 | |||
3072 | 19 | /** |
|
3073 | * Sort the current array by value. |
||
3074 | 19 | * |
|
3075 | 19 | * @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
|
3076 | 19 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
|
3077 | * |
||
3078 | * @return static <p>(Mutable)</p> |
||
3079 | 19 | */ |
|
3080 | 19 | public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
3084 | 4 | ||
3085 | /** |
||
3086 | 9 | * Sort the current array by value. |
|
3087 | 10 | * |
|
3088 | 10 | * @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
|
3089 | 10 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
|
3090 | 10 | * |
|
3091 | 4 | * @return static <p>(Mutable)</p> |
|
3092 | 4 | */ |
|
3093 | 6 | public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
3097 | 19 | ||
3098 | /** |
||
3099 | * Sort a array by value, by a closure or by a property. |
||
3100 | * |
||
3101 | * - If the sorter is null, the array is sorted naturally. |
||
3102 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
3103 | * |
||
3104 | * @param null $sorter |
||
3105 | * @param string|int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
3106 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
3107 | * <strong>SORT_NATURAL</strong></p> |
||
3108 | 1 | * |
|
3109 | * @return static <p>(Immutable)</p> |
||
3110 | 1 | */ |
|
3111 | public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
||
3137 | |||
3138 | 1 | /** |
|
3139 | * sorting keys |
||
3140 | * |
||
3141 | * @param array $elements |
||
3142 | * @param int $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
3143 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or <strong>SORT_NATURAL</strong></p> |
||
3144 | * |
||
3145 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
3146 | */ |
||
3147 | protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
||
3164 | |||
3165 | 146 | /** |
|
3166 | * @param array &$elements |
||
3167 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
3168 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
3169 | * <strong>SORT_NATURAL</strong></p> |
||
3170 | * @param bool $keepKeys |
||
3171 | * |
||
3172 | * @return static <p>(Mutable) Return this Arrayy object.</p> |
||
3173 | */ |
||
3174 | protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
||
3203 | 9 | ||
3204 | /** |
||
3205 | 8 | * Split an array in the given amount of pieces. |
|
3206 | 8 | * |
|
3207 | 8 | * @param int $numberOfPieces |
|
3208 | * @param bool $keepKeys |
||
3209 | 8 | * |
|
3210 | 9 | * @return static <p>(Immutable)</p> |
|
3211 | 9 | */ |
|
3212 | 9 | public function split($numberOfPieces = 2, $keepKeys = false) |
|
3226 | |||
3227 | /** |
||
3228 | 9 | * Stripe all empty items. |
|
3229 | * |
||
3230 | * @return static <p>(Immutable)</p> |
||
3231 | */ |
||
3232 | public function stripEmpty() |
||
3244 | 9 | ||
3245 | 9 | /** |
|
3246 | * Swap two values between positions by key. |
||
3247 | 9 | * |
|
3248 | * @param string|int $swapA <p>a key in the array</p> |
||
3249 | * @param string|int $swapB <p>a key in the array</p> |
||
3250 | 9 | * |
|
3251 | * @return static <p>(Immutable)</p> |
||
3252 | */ |
||
3253 | 9 | public function swap($swapA, $swapB) |
|
3261 | |||
3262 | /** |
||
3263 | 9 | * alias: for "Arrayy->getArray()" |
|
3264 | * |
||
3265 | 9 | * @see Arrayy::getArray() |
|
3266 | */ |
||
3267 | public function toArray() |
||
3271 | |||
3272 | /** |
||
3273 | 4 | * Convert the current array to JSON. |
|
3274 | * |
||
3275 | 4 | * @param null|int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p> |
|
3276 | 4 | * @param int $depth [optional] <p>Set the maximum depth. Must be greater than zero.</p> |
|
3277 | 4 | * |
|
3278 | 4 | * @return string |
|
3279 | */ |
||
3280 | 4 | public function toJson($options = null, $depth = 512) |
|
3284 | |||
3285 | /** |
||
3286 | * Implodes array to a string with specified separator. |
||
3287 | * |
||
3288 | 2 | * @param string $separator [optional] <p>The element's separator.</p> |
|
3289 | * |
||
3290 | 2 | * @return string <p>The string representation of array, separated by ",".</p> |
|
3291 | */ |
||
3292 | public function toString($separator = ',') |
||
3296 | |||
3297 | /** |
||
3298 | * Return a duplicate free copy of the current array. |
||
3299 | * |
||
3300 | * @return static <p>(Mutable)</p> |
||
3301 | 9 | */ |
|
3302 | public function unique() |
||
3326 | |||
3327 | /** |
||
3328 | * Return a duplicate free copy of the current array. (with the old keys) |
||
3329 | * |
||
3330 | * @return static <p>(Mutable)</p> |
||
3331 | */ |
||
3332 | public function uniqueKeepIndex() |
||
3359 | |||
3360 | /** |
||
3361 | * alias: for "Arrayy->unique()" |
||
3362 | * |
||
3363 | * @see Arrayy::unique() |
||
3364 | * |
||
3365 | * @return static <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
3366 | */ |
||
3367 | public function uniqueNewIndex() |
||
3371 | |||
3372 | /** |
||
3373 | * Prepends one or more values to the beginning of array at once. |
||
3374 | * |
||
3375 | * @return static <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p> |
||
3376 | */ |
||
3377 | View Code Duplication | public function unshift(/* variadic arguments allowed */) |
|
3386 | |||
3387 | /** |
||
3388 | * Get all values from a array. |
||
3389 | * |
||
3390 | * @return static <p>(Immutable)</p> |
||
3391 | */ |
||
3392 | public function values() |
||
3396 | |||
3397 | /** |
||
3398 | * Apply the given function to every element in the array, discarding the results. |
||
3399 | * |
||
3400 | * @param callable $callable |
||
3401 | * @param bool $recursive <p>Whether array will be walked recursively or no</p> |
||
3402 | * |
||
3403 | * @return static <p>(Mutable) Return this Arrayy object, with modified elements.</p> |
||
3404 | */ |
||
3405 | public function walk($callable, $recursive = false) |
||
3415 | } |
||
3416 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.