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 |
||
14 | class Arrayy extends \ArrayObject implements \Countable, \IteratorAggregate, \ArrayAccess, \Serializable |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $array = array(); |
||
20 | |||
21 | /** |
||
22 | * Initializes |
||
23 | * |
||
24 | * @param array $array |
||
25 | */ |
||
26 | 413 | public function __construct($array = array()) |
|
32 | |||
33 | /** |
||
34 | * create a fallback for array |
||
35 | * |
||
36 | * 1. fallback to empty array, if there is nothing |
||
37 | * 2. cast a String or Object with "__toString" into an array |
||
38 | * 3. call "__toArray" on Object, if the method exists |
||
39 | * 4. throw a "InvalidArgumentException"-Exception |
||
40 | * |
||
41 | * @param $array |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 413 | protected function fallbackForArray(&$array) |
|
75 | |||
76 | /** |
||
77 | * Get the current array from the "Arrayy"-object |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 267 | public function getArray() |
|
85 | |||
86 | /** |
||
87 | * Create a new Arrayy object via string. |
||
88 | * |
||
89 | * @param string $str The input string. |
||
90 | * @param string|null $delimiter The boundary string. |
||
91 | * @param string|null $regEx Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be used. |
||
92 | * |
||
93 | * @return Arrayy |
||
94 | */ |
||
95 | 2 | public static function createFromString($str, $delimiter, $regEx = null) |
|
120 | |||
121 | /** |
||
122 | * Creates a Arrayy object. |
||
123 | * |
||
124 | * @param array $array |
||
125 | * |
||
126 | * @return Arrayy |
||
127 | */ |
||
128 | 325 | public static function create($array = array()) |
|
132 | |||
133 | /** |
||
134 | * create a new Arrayy object via JSON, |
||
135 | * |
||
136 | * @param string $json |
||
137 | * |
||
138 | * @return Arrayy |
||
139 | */ |
||
140 | 1 | public static function createFromJson($json) |
|
146 | |||
147 | /** |
||
148 | * find by ... |
||
149 | * |
||
150 | * @param $property |
||
151 | * @param $value |
||
152 | * @param string $comparisonOp |
||
153 | * |
||
154 | * @return Arrayy |
||
155 | */ |
||
156 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
162 | |||
163 | /** |
||
164 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
||
165 | * within that. |
||
166 | * |
||
167 | * @param $property |
||
168 | * @param $value |
||
169 | * @param string $comparisonOp |
||
170 | * |
||
171 | * @return Arrayy |
||
172 | */ |
||
173 | 1 | public function filterBy($property, $value, $comparisonOp = null) |
|
232 | |||
233 | /** |
||
234 | * Get a value from an array (optional using dot-notation). |
||
235 | * |
||
236 | * @param string $key The key to look for |
||
237 | * @param mixed $default Default value to fallback to |
||
238 | * @param array $array The array to get from, |
||
239 | * if it's set to "null" we use the current array from the class |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | 31 | public function get($key, $default = null, $array = null) |
|
270 | |||
271 | /** |
||
272 | * WARNING: Creates a Arrayy object by reference. |
||
273 | * |
||
274 | * @param array $array |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function createByReference(&$array = array()) |
||
286 | |||
287 | /** |
||
288 | * Get all keys from the current array. |
||
289 | * |
||
290 | * @return Arrayy |
||
291 | */ |
||
292 | 1 | public function keys() |
|
298 | |||
299 | /** |
||
300 | * Get all values from a array. |
||
301 | * |
||
302 | * @return Arrayy |
||
303 | */ |
||
304 | 1 | public function values() |
|
310 | |||
311 | /** |
||
312 | * Group values from a array according to the results of a closure. |
||
313 | * |
||
314 | * @param string $grouper a callable function name |
||
315 | * @param bool $saveKeys |
||
316 | * |
||
317 | * @return Arrayy |
||
318 | */ |
||
319 | 3 | public function group($grouper, $saveKeys = false) |
|
344 | |||
345 | /** |
||
346 | * Given a list and an iterate-function that returns |
||
347 | * a key for each element in the list (or a property name), |
||
348 | * returns an object with an index of each item. |
||
349 | * |
||
350 | * Just like groupBy, but for when you know your keys are unique. |
||
351 | * |
||
352 | * @param mixed $key |
||
353 | * |
||
354 | * @return Arrayy |
||
355 | */ |
||
356 | 3 | public function indexBy($key) |
|
368 | |||
369 | /** |
||
370 | * magic to string |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | 14 | public function __toString() |
|
378 | |||
379 | /** |
||
380 | * Implodes array to a string with specified separator. |
||
381 | * |
||
382 | * @param string $separator The element's separator |
||
383 | * |
||
384 | * @return string The string representation of array, separated by "," |
||
385 | */ |
||
386 | 14 | public function toString($separator = ',') |
|
390 | |||
391 | /** |
||
392 | * Implodes an array. |
||
393 | * |
||
394 | * @param string $with What to implode it with |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 22 | public function implode($with = '') |
|
402 | |||
403 | /** |
||
404 | * Push one or more values onto the end of array at once. |
||
405 | * |
||
406 | * @return $this An Arrayy object with pushed elements to the end of array |
||
407 | */ |
||
408 | View Code Duplication | public function push(/* variadic arguments allowed */) |
|
417 | |||
418 | /** |
||
419 | * Shifts a specified value off the beginning of array. |
||
420 | * |
||
421 | * @return mixed A shifted element from the current array. |
||
422 | */ |
||
423 | public function shift() |
||
427 | |||
428 | /** |
||
429 | * Prepends one or more values to the beginning of array at once. |
||
430 | * |
||
431 | * @return Arrayy Array object with prepended elements to the beginning of array |
||
432 | */ |
||
433 | View Code Duplication | public function unshift(/* variadic arguments allowed */) |
|
442 | |||
443 | /** |
||
444 | * @return mixed |
||
445 | */ |
||
446 | public function serialize() |
||
450 | |||
451 | /** |
||
452 | * @param string $array |
||
453 | */ |
||
454 | public function unserialize($array) |
||
458 | |||
459 | /** |
||
460 | * Assigns a value to the specified offset. |
||
461 | * |
||
462 | * @param mixed $offset |
||
463 | * @param mixed $value |
||
464 | */ |
||
465 | 2 | public function offsetSet($offset, $value) |
|
473 | |||
474 | /** |
||
475 | * Get a value by key. |
||
476 | * |
||
477 | * @param $key |
||
478 | * |
||
479 | * @return mixed |
||
480 | */ |
||
481 | public function &__get($key) |
||
485 | |||
486 | /** |
||
487 | * Assigns a value to the specified element. |
||
488 | * |
||
489 | * @param $key |
||
490 | * @param $value |
||
491 | */ |
||
492 | public function __set($key, $value) |
||
496 | |||
497 | /** |
||
498 | * Whether or not an element exists by key. |
||
499 | * |
||
500 | * @param $key |
||
501 | * |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function __isset($key) |
||
508 | |||
509 | /** |
||
510 | * Unset element by key |
||
511 | * |
||
512 | * @param mixed $key |
||
513 | */ |
||
514 | public function __unset($key) |
||
518 | |||
519 | /** |
||
520 | * Whether or not an offset exists. |
||
521 | * |
||
522 | * @param mixed $offset |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | 9 | public function offsetExists($offset) |
|
530 | |||
531 | /** |
||
532 | * Call object as function. |
||
533 | * |
||
534 | * @param mixed $key |
||
535 | * |
||
536 | * @return mixed |
||
537 | */ |
||
538 | public function __invoke($key = null) |
||
550 | |||
551 | /** |
||
552 | * Search for the value of the current array via $index. |
||
553 | * |
||
554 | * @param mixed $index |
||
555 | * |
||
556 | * @return Arrayy will return a empty Arrayy if the value wasn't found |
||
557 | */ |
||
558 | 7 | public function searchValue($index) |
|
573 | |||
574 | /** |
||
575 | * Unset an offset. |
||
576 | * |
||
577 | * @param mixed $offset |
||
578 | */ |
||
579 | 1 | public function offsetUnset($offset) |
|
585 | |||
586 | /** |
||
587 | * Check if all items in current array match a truth test. |
||
588 | * |
||
589 | * @param \Closure $closure |
||
590 | * |
||
591 | * @return bool |
||
592 | */ |
||
593 | 9 | View Code Duplication | public function matches(\Closure $closure) |
607 | |||
608 | /** |
||
609 | * Iterate over the current array and modify the array's value. |
||
610 | * |
||
611 | * @param \Closure $closure |
||
612 | * |
||
613 | * @return Arrayy |
||
614 | */ |
||
615 | 22 | View Code Duplication | public function each(\Closure $closure) |
625 | |||
626 | /** |
||
627 | * Returns the value at specified offset. |
||
628 | * |
||
629 | * @param mixed $offset |
||
630 | * |
||
631 | * @return mixed return null if the offset did not exists |
||
632 | */ |
||
633 | 8 | public function offsetGet($offset) |
|
637 | |||
638 | /** |
||
639 | * alias: for "Arrayy->getArray()" |
||
640 | */ |
||
641 | 14 | public function toArray() |
|
645 | |||
646 | /** |
||
647 | * Check if any item in the current array matches a truth test. |
||
648 | * |
||
649 | * @param \Closure $closure |
||
650 | * |
||
651 | * @return bool |
||
652 | */ |
||
653 | 9 | View Code Duplication | public function matchesAny(\Closure $closure) |
667 | |||
668 | /** |
||
669 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate interface. |
||
670 | * |
||
671 | * @return \ArrayIterator An iterator for the values in the array. |
||
672 | */ |
||
673 | 2 | public function getIterator() |
|
677 | |||
678 | /** |
||
679 | * Check if we have named keys in the current array. |
||
680 | * |
||
681 | * @return bool |
||
682 | */ |
||
683 | 12 | public function isAssoc() |
|
691 | |||
692 | /** |
||
693 | * Check if the current array is a multi-array. |
||
694 | * |
||
695 | * @return bool |
||
696 | */ |
||
697 | 13 | public function isMultiArray() |
|
701 | |||
702 | /** |
||
703 | * Check if an item is in the current array. |
||
704 | * |
||
705 | * @param mixed $value |
||
706 | * |
||
707 | * @return bool |
||
708 | */ |
||
709 | 9 | public function contains($value) |
|
713 | |||
714 | /** |
||
715 | * Returns the average value of the current array. |
||
716 | * |
||
717 | * @param int $decimals The number of decimals to return |
||
718 | * |
||
719 | * @return int|double The average value |
||
720 | */ |
||
721 | 10 | public function average($decimals = null) |
|
735 | |||
736 | /** |
||
737 | * Count the values from the current array. |
||
738 | * |
||
739 | * INFO: only a alias for "$arrayy->size()" |
||
740 | * |
||
741 | * @return int |
||
742 | */ |
||
743 | 77 | public function count() |
|
747 | |||
748 | /** |
||
749 | * Get the size of an array. |
||
750 | * |
||
751 | * @return int |
||
752 | */ |
||
753 | 77 | public function size() |
|
757 | |||
758 | /** |
||
759 | * Append a value to an array. |
||
760 | * |
||
761 | * @param mixed $value |
||
762 | * |
||
763 | * @return Arrayy |
||
764 | */ |
||
765 | 8 | public function append($value) |
|
771 | |||
772 | /** |
||
773 | * Count the values from the current array. |
||
774 | * |
||
775 | * INFO: only a alias for "$arrayy->size()" |
||
776 | * |
||
777 | * @return int |
||
778 | */ |
||
779 | 10 | public function length() |
|
783 | |||
784 | /** |
||
785 | * Get the max value from an array. |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | 10 | public function max() |
|
797 | |||
798 | /** |
||
799 | * Get the min value from an array. |
||
800 | * |
||
801 | * @return mixed |
||
802 | */ |
||
803 | 10 | public function min() |
|
811 | |||
812 | /** |
||
813 | * Find the first item in an array that passes the truth test, |
||
814 | * otherwise return false |
||
815 | * |
||
816 | * @param \Closure $closure |
||
817 | * |
||
818 | * @return mixed|false false if we did not find the value |
||
819 | */ |
||
820 | 7 | public function find(\Closure $closure) |
|
830 | |||
831 | /** |
||
832 | * WARNING!!! -> Clear the current array. |
||
833 | * |
||
834 | * @return Arrayy will always return an empty Arrayy object |
||
835 | */ |
||
836 | public function clear() |
||
842 | |||
843 | /** |
||
844 | * Clean all falsy values from an array. |
||
845 | * |
||
846 | * @return Arrayy |
||
847 | */ |
||
848 | 8 | public function clean() |
|
856 | |||
857 | /** |
||
858 | * Find all items in an array that pass the truth test. |
||
859 | * |
||
860 | * @param \Closure|null $closure |
||
861 | * |
||
862 | * @return Arrayy |
||
863 | */ |
||
864 | 8 | public function filter($closure = null) |
|
874 | |||
875 | /** |
||
876 | * Get a random value from an array, with the ability to skew the results. |
||
877 | * |
||
878 | * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
||
879 | * |
||
880 | * @param array $array |
||
881 | * @param null|int $take how many values you will take? |
||
882 | * |
||
883 | * @return Arrayy |
||
884 | */ |
||
885 | 9 | public function randomWeighted(array $array, $take = null) |
|
898 | |||
899 | /** |
||
900 | * Search for the first index of the current array via $value. |
||
901 | * |
||
902 | * @param mixed $value |
||
903 | * |
||
904 | * @return Arrayy will return a empty Arrayy if the index was not found |
||
905 | */ |
||
906 | 16 | public function searchIndex($value) |
|
918 | |||
919 | /** |
||
920 | * Get a random string from an array. |
||
921 | * |
||
922 | * @param null|int $take how many values you will take? |
||
923 | * |
||
924 | * @return Arrayy |
||
925 | */ |
||
926 | 18 | public function random($take = null) |
|
942 | |||
943 | /** |
||
944 | * Get the first value(s) from the current array. |
||
945 | * |
||
946 | * @param int|null $take how many values you will take? |
||
947 | * |
||
948 | * @return Arrayy |
||
949 | */ |
||
950 | 33 | public function first($take = null) |
|
960 | |||
961 | /** |
||
962 | * Merge the new $array into the current array. |
||
963 | * |
||
964 | * - keep key,value from the current array, also if the index is in the new $array |
||
965 | * |
||
966 | * @param array $array |
||
967 | * |
||
968 | * @return Arrayy |
||
969 | */ |
||
970 | 17 | public function mergeAppendKeepIndex(array $array = array()) |
|
976 | |||
977 | /** |
||
978 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
979 | * |
||
980 | * @param array $search |
||
981 | * |
||
982 | * @return bool |
||
983 | */ |
||
984 | 1 | public function intersects(array $search) |
|
988 | |||
989 | /** |
||
990 | * Return an array with all elements found in input array. |
||
991 | * |
||
992 | * @param array $search |
||
993 | * |
||
994 | * @return Arrayy |
||
995 | */ |
||
996 | 2 | public function intersection(array $search) |
|
1002 | |||
1003 | /** |
||
1004 | * Get the last value(s) from the current array. |
||
1005 | * |
||
1006 | * @param int|null $take |
||
1007 | * |
||
1008 | * @return Arrayy |
||
1009 | */ |
||
1010 | 11 | public function last($take = null) |
|
1021 | |||
1022 | /** |
||
1023 | * Pop a specified value off the end of the current array. |
||
1024 | * |
||
1025 | * @return mixed The popped element from the current array. |
||
1026 | */ |
||
1027 | 7 | public function pop() |
|
1031 | |||
1032 | /** |
||
1033 | * Get the last elements from index $from until the end of this array. |
||
1034 | * |
||
1035 | * @param int $from |
||
1036 | * |
||
1037 | * @return Arrayy |
||
1038 | */ |
||
1039 | 16 | public function rest($from = 1) |
|
1045 | |||
1046 | /** |
||
1047 | * Get everything but the last..$to items. |
||
1048 | * |
||
1049 | * @param int $to |
||
1050 | * |
||
1051 | * @return Arrayy |
||
1052 | */ |
||
1053 | 12 | public function initial($to = 1) |
|
1059 | |||
1060 | /** |
||
1061 | * Iterate over an array and execute a callback for each loop. |
||
1062 | * |
||
1063 | * @param \Closure $closure |
||
1064 | * |
||
1065 | * @return Arrayy |
||
1066 | */ |
||
1067 | 2 | View Code Duplication | public function at(\Closure $closure) |
1077 | |||
1078 | /** |
||
1079 | * Merge the new $array into the current array. |
||
1080 | * |
||
1081 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
1082 | * - create new indexes |
||
1083 | * |
||
1084 | * @param array $array |
||
1085 | * @param bool $recursive |
||
1086 | * |
||
1087 | * @return Arrayy |
||
1088 | */ |
||
1089 | 8 | View Code Duplication | public function mergeAppendNewIndex(array $array = array(), $recursive = false) |
1099 | |||
1100 | /** |
||
1101 | * Merge the current array into the new $array. |
||
1102 | * |
||
1103 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
1104 | * - create new indexes |
||
1105 | * |
||
1106 | * @param array $array |
||
1107 | * @param bool $recursive |
||
1108 | * |
||
1109 | * @return Arrayy |
||
1110 | */ |
||
1111 | 8 | View Code Duplication | public function mergePrependNewIndex(array $array = array(), $recursive = false) |
1121 | |||
1122 | /** |
||
1123 | * Merge the the current array into the $array. |
||
1124 | * |
||
1125 | * - use key,value from the new $array, also if the index is in the current array |
||
1126 | * |
||
1127 | * @param array $array |
||
1128 | * |
||
1129 | * @return Arrayy |
||
1130 | */ |
||
1131 | 8 | public function mergePrependKeepIndex(array $array = array()) |
|
1137 | |||
1138 | /** |
||
1139 | * Return values that are only in the current array. |
||
1140 | * |
||
1141 | * @param array $array |
||
1142 | * |
||
1143 | * @return Arrayy |
||
1144 | */ |
||
1145 | 8 | public function diff(array $array = array()) |
|
1151 | |||
1152 | /** |
||
1153 | * Return values that are only in the new $array. |
||
1154 | * |
||
1155 | * @param array $array |
||
1156 | * |
||
1157 | * @return Arrayy |
||
1158 | */ |
||
1159 | 8 | public function diffReverse(array $array = array()) |
|
1165 | |||
1166 | /** |
||
1167 | * Replace the first matched value in an array. |
||
1168 | * |
||
1169 | * @param mixed $search |
||
1170 | * @param mixed $replacement |
||
1171 | * |
||
1172 | * @return Arrayy |
||
1173 | */ |
||
1174 | 3 | public function replaceOneValue($search, $replacement = '') |
|
1185 | |||
1186 | /** |
||
1187 | * Replace values in the current array. |
||
1188 | * |
||
1189 | * @param string $search The string to replace. |
||
1190 | * @param string $replacement What to replace it with. |
||
1191 | * |
||
1192 | * @return Arrayy |
||
1193 | */ |
||
1194 | 1 | public function replaceValues($search, $replacement = '') |
|
1204 | |||
1205 | /** |
||
1206 | * Replace the keys in an array with another set. |
||
1207 | * |
||
1208 | * @param array $keys An array of keys matching the array's size |
||
1209 | * |
||
1210 | * @return Arrayy |
||
1211 | */ |
||
1212 | 1 | public function replaceKeys(array $keys) |
|
1219 | |||
1220 | /** |
||
1221 | * Create an array using the current array as keys and the other array as values. |
||
1222 | * |
||
1223 | * @param array $array Values array |
||
1224 | * |
||
1225 | * @return Arrayy Arrayy object with values from the other array. |
||
1226 | */ |
||
1227 | public function replaceAllValues(array $array) |
||
1233 | |||
1234 | /** |
||
1235 | * Shuffle the current array. |
||
1236 | * |
||
1237 | * @return Arrayy |
||
1238 | */ |
||
1239 | 1 | public function shuffle() |
|
1247 | |||
1248 | /** |
||
1249 | * Split an array in the given amount of pieces. |
||
1250 | * |
||
1251 | * @param int $numberOfPieces |
||
1252 | * @param bool $keepKeys |
||
1253 | * |
||
1254 | * @return array |
||
1255 | */ |
||
1256 | 1 | public function split($numberOfPieces = 2, $keepKeys = false) |
|
1267 | |||
1268 | /** |
||
1269 | * Returns the values from a single column of the input array, identified by |
||
1270 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
1271 | * |
||
1272 | * Info: Optionally, you may provide an $indexKey to index the values in the returned |
||
1273 | * array by the values from the $indexKey column in the input array. |
||
1274 | * |
||
1275 | * @param mixed $columnKey |
||
1276 | * @param mixed $indexKey |
||
1277 | * |
||
1278 | * @return Arrayy |
||
1279 | */ |
||
1280 | 1 | public function getColumn($columnKey = null, $indexKey = null) |
|
1286 | |||
1287 | /** |
||
1288 | * Invoke a function on all of an array's values. |
||
1289 | * |
||
1290 | * @param mixed $callable |
||
1291 | * @param array $arguments |
||
1292 | * |
||
1293 | * @return Arrayy |
||
1294 | */ |
||
1295 | 1 | public function invoke($callable, $arguments = array()) |
|
1311 | |||
1312 | /** |
||
1313 | * Apply the given function to the every element of the array, |
||
1314 | * collecting the results. |
||
1315 | * |
||
1316 | * @param callable $callable |
||
1317 | * |
||
1318 | * @return Arrayy Arrayy object with modified elements |
||
1319 | */ |
||
1320 | public function map($callable) |
||
1326 | |||
1327 | /** |
||
1328 | * Return all items that fail the truth test. |
||
1329 | * |
||
1330 | * @param \Closure $closure |
||
1331 | * |
||
1332 | * @return Arrayy |
||
1333 | */ |
||
1334 | 1 | View Code Duplication | public function reject(\Closure $closure) |
1346 | |||
1347 | /** |
||
1348 | * Replace a key with a new key/value pair. |
||
1349 | * |
||
1350 | * @param $replace |
||
1351 | * @param $key |
||
1352 | * @param $value |
||
1353 | * |
||
1354 | * @return Arrayy |
||
1355 | */ |
||
1356 | 1 | public function replace($replace, $key, $value) |
|
1362 | |||
1363 | /** |
||
1364 | * Remove a value from the current array (optional using dot-notation). |
||
1365 | * |
||
1366 | * @param mixed $key |
||
1367 | * |
||
1368 | * @return Arrayy |
||
1369 | */ |
||
1370 | 17 | public function remove($key) |
|
1385 | |||
1386 | /** |
||
1387 | * Internal mechanics of remove method. |
||
1388 | * |
||
1389 | * @param $key |
||
1390 | * |
||
1391 | * @return boolean |
||
1392 | */ |
||
1393 | 17 | protected function internalRemove($key) |
|
1415 | |||
1416 | /** |
||
1417 | * Check if an array has a given key. |
||
1418 | * |
||
1419 | * @param mixed $key |
||
1420 | * |
||
1421 | * @return bool |
||
1422 | */ |
||
1423 | 18 | public function has($key) |
|
1430 | |||
1431 | /** |
||
1432 | * Set a value for the current array (optional using dot-notation). |
||
1433 | * |
||
1434 | * @param string $key The key to set |
||
1435 | * @param mixed $value Its value |
||
1436 | * |
||
1437 | * @return Arrayy |
||
1438 | */ |
||
1439 | 14 | public function set($key, $value) |
|
1445 | |||
1446 | /** |
||
1447 | * Internal mechanic of set method. |
||
1448 | * |
||
1449 | * @param mixed $key |
||
1450 | * @param mixed $value |
||
1451 | * |
||
1452 | * @return bool |
||
1453 | */ |
||
1454 | 14 | protected function internalSet($key, $value) |
|
1478 | |||
1479 | /** |
||
1480 | * Get a value from a array and set it if it was not. |
||
1481 | * |
||
1482 | * WARNING: this method only set the value, if the $key is not already set |
||
1483 | * |
||
1484 | * @param string $key The key |
||
1485 | * @param mixed $default The default value to set if it isn't |
||
1486 | * |
||
1487 | * @return mixed |
||
1488 | */ |
||
1489 | 9 | public function setAndGet($key, $default = null) |
|
1498 | |||
1499 | /** |
||
1500 | * Remove the first value from the current array. |
||
1501 | * |
||
1502 | * @return Arrayy |
||
1503 | */ |
||
1504 | 7 | public function removeFirst() |
|
1510 | |||
1511 | /** |
||
1512 | * Remove the last value from the current array. |
||
1513 | * |
||
1514 | * @return Arrayy |
||
1515 | */ |
||
1516 | 7 | public function removeLast() |
|
1522 | |||
1523 | /** |
||
1524 | * Removes a particular value from an array (numeric or associative). |
||
1525 | * |
||
1526 | * @param mixed $value |
||
1527 | * |
||
1528 | * @return Arrayy |
||
1529 | */ |
||
1530 | 7 | public function removeValue($value) |
|
1548 | |||
1549 | /** |
||
1550 | * Pad array to the specified size with a given value. |
||
1551 | * |
||
1552 | * @param int $size Size of the result array |
||
1553 | * @param mixed $value Empty value by default |
||
1554 | * |
||
1555 | * @return Arrayy Arrayy object padded to $size with $value |
||
1556 | */ |
||
1557 | public function pad($size, $value) |
||
1563 | |||
1564 | /** |
||
1565 | * Prepend a value to an array. |
||
1566 | * |
||
1567 | * @param mixed $value |
||
1568 | * |
||
1569 | * @return Arrayy |
||
1570 | */ |
||
1571 | 7 | public function prepend($value) |
|
1577 | |||
1578 | /** |
||
1579 | * alias: for "Arrayy->append()" |
||
1580 | * |
||
1581 | * @param $value |
||
1582 | * |
||
1583 | * @return Arrayy |
||
1584 | */ |
||
1585 | public function add($value) |
||
1591 | |||
1592 | /** |
||
1593 | * Create a numerically re-indexed Arrayy object. |
||
1594 | * |
||
1595 | * @return Arrayy The new instance with re-indexed array-elements |
||
1596 | */ |
||
1597 | public function reindex() |
||
1603 | |||
1604 | /** |
||
1605 | * Return the array in the reverse order. |
||
1606 | * |
||
1607 | * @return Arrayy |
||
1608 | */ |
||
1609 | 7 | public function reverse() |
|
1615 | |||
1616 | /** |
||
1617 | * Custom sort by value via "usort" |
||
1618 | * |
||
1619 | * @link http://php.net/manual/en/function.usort.php |
||
1620 | * |
||
1621 | * @param callable $func |
||
1622 | * |
||
1623 | * @return Arrayy |
||
1624 | */ |
||
1625 | public function customSortValues(callable $func) |
||
1631 | |||
1632 | /** |
||
1633 | * Custom sort by index via "uksort" |
||
1634 | * |
||
1635 | * @link http://php.net/manual/en/function.uksort.php |
||
1636 | * |
||
1637 | * @param callable $func |
||
1638 | * |
||
1639 | * @return Arrayy |
||
1640 | */ |
||
1641 | public function customSortKeys(callable $func) |
||
1647 | |||
1648 | /** |
||
1649 | * Sort the current array by key. |
||
1650 | * |
||
1651 | * @link http://php.net/manual/en/function.ksort.php |
||
1652 | * @link http://php.net/manual/en/function.krsort.php |
||
1653 | * |
||
1654 | * @param int|string $direction use SORT_ASC or SORT_DESC |
||
1655 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
1656 | * |
||
1657 | * @return Arrayy |
||
1658 | */ |
||
1659 | 10 | public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
1665 | |||
1666 | /** |
||
1667 | * sorting keys |
||
1668 | * |
||
1669 | * @param array $elements |
||
1670 | * @param int $direction |
||
1671 | * @param int $strategy |
||
1672 | */ |
||
1673 | 10 | protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
1688 | |||
1689 | /** |
||
1690 | * Get correct PHP constant for direction. |
||
1691 | * |
||
1692 | * @param int|string $direction |
||
1693 | * |
||
1694 | * @return int |
||
1695 | */ |
||
1696 | 14 | protected function getDirection($direction) |
|
1718 | |||
1719 | /** |
||
1720 | * Sort the current array by value. |
||
1721 | * |
||
1722 | * @param int $direction use SORT_ASC or SORT_DESC |
||
1723 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
1724 | * |
||
1725 | * @return Arrayy |
||
1726 | */ |
||
1727 | 1 | public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
1731 | |||
1732 | /** |
||
1733 | * Sort the current array and optional you can keep the keys. |
||
1734 | * |
||
1735 | * @param string|int $direction use SORT_ASC or SORT_DESC |
||
1736 | * @param int|string $strategy |
||
1737 | * @param bool $keepKeys |
||
1738 | * |
||
1739 | * @return Arrayy |
||
1740 | */ |
||
1741 | 3 | public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
1747 | |||
1748 | /** |
||
1749 | * @param array &$elements |
||
1750 | * @param int|string $direction |
||
1751 | * @param int $strategy |
||
1752 | * @param bool $keepKeys |
||
1753 | */ |
||
1754 | 3 | protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
1781 | |||
1782 | /** |
||
1783 | * Sort the current array by value. |
||
1784 | * |
||
1785 | * @param int $direction use SORT_ASC or SORT_DESC |
||
1786 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
1787 | * |
||
1788 | * @return Arrayy |
||
1789 | */ |
||
1790 | 1 | public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
1794 | |||
1795 | /** |
||
1796 | * Sort a array by value, by a closure or by a property. |
||
1797 | * |
||
1798 | * - If the sorter is null, the array is sorted naturally. |
||
1799 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
1800 | * |
||
1801 | * @param null $sorter |
||
1802 | * @param string|int $direction |
||
1803 | * @param int $strategy |
||
1804 | * |
||
1805 | * @return Arrayy |
||
1806 | */ |
||
1807 | 1 | public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
1833 | |||
1834 | /** |
||
1835 | * Exchanges all keys with their associated values in an array. |
||
1836 | * |
||
1837 | * @return Arrayy |
||
1838 | */ |
||
1839 | 1 | public function flip() |
|
1845 | |||
1846 | /** |
||
1847 | * Apply the given function to every element in the array, |
||
1848 | * discarding the results. |
||
1849 | * |
||
1850 | * @param callable $callable |
||
1851 | * @param bool $recursive Whether array will be walked recursively or no |
||
1852 | * |
||
1853 | * @return Arrayy An Arrayy object with modified elements |
||
1854 | */ |
||
1855 | public function walk($callable, $recursive = false) |
||
1865 | |||
1866 | /** |
||
1867 | * Reduce the current array via callable e.g. anonymous-function. |
||
1868 | * |
||
1869 | * @param mixed $predicate |
||
1870 | * @param array $init |
||
1871 | * |
||
1872 | * @return Arrayy |
||
1873 | */ |
||
1874 | 2 | public function reduce($predicate, array $init = array()) |
|
1880 | |||
1881 | /** |
||
1882 | * Return a duplicate free copy of the current array. |
||
1883 | * |
||
1884 | * @return Arrayy |
||
1885 | */ |
||
1886 | 7 | public function unique() |
|
1902 | |||
1903 | /** |
||
1904 | * Convert the current array to JSON. |
||
1905 | * |
||
1906 | * @param null $options e.g. JSON_PRETTY_PRINT |
||
1907 | * |
||
1908 | * @return string |
||
1909 | */ |
||
1910 | 1 | public function toJson($options = null) |
|
1914 | } |
||
1915 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: