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 |
||
| 15 | class Arrayy extends \ArrayObject implements \Countable, \IteratorAggregate, \ArrayAccess, \Serializable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $array = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Initializes |
||
| 24 | * |
||
| 25 | * @param array $array |
||
| 26 | */ |
||
| 27 | 563 | public function __construct($array = array()) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * create a fallback for array |
||
| 36 | * |
||
| 37 | * 1. fallback to empty array, if there is nothing |
||
| 38 | * 2. cast a String or Object with "__toString" into an array |
||
| 39 | * 3. call "__toArray" on Object, if the method exists |
||
| 40 | * 4. throw a "InvalidArgumentException"-Exception |
||
| 41 | * |
||
| 42 | * @param $array |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | 563 | protected function fallbackForArray(&$array) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get the current array from the "Arrayy"-object |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | 417 | public function getArray() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Create a new Arrayy object via string. |
||
| 89 | * |
||
| 90 | * @param string $str The input string. |
||
| 91 | * @param string|null $delimiter The boundary string. |
||
| 92 | * @param string|null $regEx Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be used. |
||
| 93 | * |
||
| 94 | * @return Arrayy Returns created instance |
||
| 95 | */ |
||
| 96 | 8 | public static function createFromString($str, $delimiter, $regEx = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Creates a Arrayy object. |
||
| 124 | * |
||
| 125 | * @param array $array |
||
| 126 | * |
||
| 127 | * @return Arrayy Returns created instance |
||
| 128 | */ |
||
| 129 | 402 | public static function create($array = array()) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * create a new Arrayy object via JSON, |
||
| 136 | * |
||
| 137 | * @param string $json |
||
| 138 | * |
||
| 139 | * @return Arrayy Returns created instance |
||
| 140 | */ |
||
| 141 | 5 | public static function createFromJson($json) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Create a new instance filled with values from an object implementing ArrayAccess. |
||
| 150 | * |
||
| 151 | * @param ArrayAccess $elements Object that implements ArrayAccess |
||
| 152 | * |
||
| 153 | * @return Arrayy Returns created instance |
||
| 154 | */ |
||
| 155 | 4 | public static function createFromObject(ArrayAccess $elements) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Create a new instance containing a range of elements. |
||
| 168 | * |
||
| 169 | * @param mixed $low First value of the sequence |
||
| 170 | * @param mixed $high The sequence is ended upon reaching the end value |
||
| 171 | * @param int $step Used as the increment between elements in the sequence |
||
| 172 | * |
||
| 173 | * @return Arrayy The created array |
||
| 174 | */ |
||
| 175 | 1 | public static function createWithRange($low, $high, $step = 1) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * alias: for "Arrayy->random()" |
||
| 182 | * |
||
| 183 | * @return Arrayy |
||
| 184 | */ |
||
| 185 | public function getRandom() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get a random value from the current array. |
||
| 192 | * |
||
| 193 | * @param null|int $number how many values you will take? |
||
| 194 | * |
||
| 195 | * @return Arrayy |
||
| 196 | */ |
||
| 197 | 18 | public function random($number = null) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Count the values from the current array. |
||
| 216 | * |
||
| 217 | * INFO: only a alias for "$arrayy->size()" |
||
| 218 | * |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | 77 | public function count() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get the size of an array. |
||
| 228 | * |
||
| 229 | * @return int |
||
| 230 | */ |
||
| 231 | 77 | public function size() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Get the first value(s) from the current array. |
||
| 238 | * |
||
| 239 | * @param int|null $number how many values you will take? |
||
| 240 | * |
||
| 241 | * @return Arrayy |
||
| 242 | */ |
||
| 243 | 33 | public function first($number = null) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Append a value to an array. |
||
| 257 | * |
||
| 258 | * @param mixed $value |
||
| 259 | * |
||
| 260 | * @return Arrayy |
||
| 261 | */ |
||
| 262 | 8 | public function append($value) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | public function serialize() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $array |
||
| 279 | */ |
||
| 280 | public function unserialize($array) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Assigns a value to the specified offset. |
||
| 287 | * |
||
| 288 | * @param mixed $offset |
||
| 289 | * @param mixed $value |
||
| 290 | */ |
||
| 291 | 13 | public function offsetSet($offset, $value) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * alias: for "Arrayy->randomValue()" |
||
| 302 | * |
||
| 303 | * @return Arrayy |
||
| 304 | */ |
||
| 305 | public function getRandomValue() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Pick a random value from the values of this array. |
||
| 312 | * |
||
| 313 | * @return Arrayy |
||
| 314 | */ |
||
| 315 | public function randomValue() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * alias: for "Arrayy->randomValues()" |
||
| 322 | * |
||
| 323 | * @param int $number |
||
| 324 | * |
||
| 325 | * @return Arrayy |
||
| 326 | */ |
||
| 327 | public function getRandomValues($number) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Pick a given number of random values out of this array. |
||
| 334 | * |
||
| 335 | * @param int $number |
||
| 336 | * |
||
| 337 | * @return Arrayy |
||
| 338 | */ |
||
| 339 | public function randomValues($number) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * alias: for "Arrayy->randomKey()" |
||
| 348 | * |
||
| 349 | * @return Arrayy |
||
| 350 | */ |
||
| 351 | public function getRandomKey() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Pick a random key/index from the keys of this array. |
||
| 358 | * |
||
| 359 | * @return Arrayy |
||
| 360 | * |
||
| 361 | * @throws \RangeException If array is empty |
||
| 362 | */ |
||
| 363 | public function randomKey() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Pick a given number of random keys/indexes out of this array. |
||
| 370 | * |
||
| 371 | * @param int $number The number of keys/indexes (should be <= $this->count()) |
||
| 372 | * |
||
| 373 | * @return Arrayy |
||
| 374 | * |
||
| 375 | * @throws \RangeException If array is empty |
||
| 376 | */ |
||
| 377 | public function randomKeys($number) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * alias: for "Arrayy->randomKeys()" |
||
| 399 | * |
||
| 400 | * @param int $number |
||
| 401 | * |
||
| 402 | * @return Arrayy |
||
| 403 | */ |
||
| 404 | public function getRandomKeys($number) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * find by ... |
||
| 411 | * |
||
| 412 | * @param $property |
||
| 413 | * @param $value |
||
| 414 | * @param string $comparisonOp |
||
| 415 | * |
||
| 416 | * @return Arrayy |
||
| 417 | */ |
||
| 418 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
||
| 427 | * within that. |
||
| 428 | * |
||
| 429 | * @param $property |
||
| 430 | * @param $value |
||
| 431 | * @param string $comparisonOp |
||
| 432 | * |
||
| 433 | * @return Arrayy |
||
| 434 | */ |
||
| 435 | 1 | public function filterBy($property, $value, $comparisonOp = null) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Get a value from an array (optional using dot-notation). |
||
| 497 | * |
||
| 498 | * @param string $key The key to look for |
||
| 499 | * @param mixed $default Default value to fallback to |
||
| 500 | * @param array $array The array to get from, |
||
| 501 | * if it's set to "null" we use the current array from the class |
||
| 502 | * |
||
| 503 | * @return mixed |
||
| 504 | */ |
||
| 505 | 31 | public function get($key, $default = null, $array = null) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * WARNING: Creates a Arrayy object by reference. |
||
| 535 | * |
||
| 536 | * @param array $array |
||
| 537 | * |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function createByReference(&$array = array()) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get all values from a array. |
||
| 551 | * |
||
| 552 | * @return Arrayy |
||
| 553 | */ |
||
| 554 | 1 | public function values() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Group values from a array according to the results of a closure. |
||
| 563 | * |
||
| 564 | * @param string $grouper a callable function name |
||
| 565 | * @param bool $saveKeys |
||
| 566 | * |
||
| 567 | * @return Arrayy |
||
| 568 | */ |
||
| 569 | 3 | public function group($grouper, $saveKeys = false) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Given a list and an iterate-function that returns |
||
| 597 | * a key for each element in the list (or a property name), |
||
| 598 | * returns an object with an index of each item. |
||
| 599 | * |
||
| 600 | * Just like groupBy, but for when you know your keys are unique. |
||
| 601 | * |
||
| 602 | * @param mixed $key |
||
| 603 | * |
||
| 604 | * @return Arrayy |
||
| 605 | */ |
||
| 606 | 3 | public function indexBy($key) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * magic to string |
||
| 621 | * |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | 14 | public function __toString() |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Implodes array to a string with specified separator. |
||
| 631 | * |
||
| 632 | * @param string $separator The element's separator |
||
| 633 | * |
||
| 634 | * @return string The string representation of array, separated by "," |
||
| 635 | */ |
||
| 636 | 14 | public function toString($separator = ',') |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Implodes an array. |
||
| 643 | * |
||
| 644 | * @param string $with What to implode it with |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | 22 | public function implode($with = '') |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Push one or more values onto the end of array at once. |
||
| 655 | * |
||
| 656 | * @return $this An Arrayy object with pushed elements to the end of array |
||
| 657 | */ |
||
| 658 | 4 | View Code Duplication | public function push(/* variadic arguments allowed */) |
| 667 | |||
| 668 | /** |
||
| 669 | * Shifts a specified value off the beginning of array. |
||
| 670 | * |
||
| 671 | * @return mixed A shifted element from the current array. |
||
| 672 | */ |
||
| 673 | 4 | public function shift() |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Prepends one or more values to the beginning of array at once. |
||
| 680 | * |
||
| 681 | * @return Arrayy Array object with prepended elements to the beginning of array |
||
| 682 | */ |
||
| 683 | 4 | View Code Duplication | public function unshift(/* variadic arguments allowed */) |
| 692 | |||
| 693 | /** |
||
| 694 | * Get a value by key. |
||
| 695 | * |
||
| 696 | * @param $key |
||
| 697 | * |
||
| 698 | * @return mixed |
||
| 699 | */ |
||
| 700 | public function &__get($key) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Whether or not an offset exists. |
||
| 707 | * |
||
| 708 | * @param mixed $offset |
||
| 709 | * |
||
| 710 | * @return bool |
||
| 711 | */ |
||
| 712 | 13 | public function offsetExists($offset) |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Assigns a value to the specified element. |
||
| 719 | * |
||
| 720 | * @param $key |
||
| 721 | * @param $value |
||
| 722 | */ |
||
| 723 | public function __set($key, $value) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Whether or not an element exists by key. |
||
| 730 | * |
||
| 731 | * @param $key |
||
| 732 | * |
||
| 733 | * @return bool |
||
| 734 | */ |
||
| 735 | public function __isset($key) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Unset element by key |
||
| 742 | * |
||
| 743 | * @param mixed $key |
||
| 744 | */ |
||
| 745 | public function __unset($key) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Call object as function. |
||
| 752 | * |
||
| 753 | * @param mixed $key |
||
| 754 | * |
||
| 755 | * @return mixed |
||
| 756 | */ |
||
| 757 | public function __invoke($key = null) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Search for the value of the current array via $index. |
||
| 772 | * |
||
| 773 | * @param mixed $index |
||
| 774 | * |
||
| 775 | * @return Arrayy will return a empty Arrayy if the value wasn't found |
||
| 776 | */ |
||
| 777 | 7 | public function searchValue($index) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Check if all items in current array match a truth test. |
||
| 795 | * |
||
| 796 | * @param \Closure $closure |
||
| 797 | * |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | 9 | View Code Duplication | public function matches(\Closure $closure) |
| 814 | |||
| 815 | /** |
||
| 816 | * Unset an offset. |
||
| 817 | * |
||
| 818 | * @param mixed $offset |
||
| 819 | */ |
||
| 820 | 5 | public function offsetUnset($offset) |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Iterate over the current array and modify the array's value. |
||
| 829 | * |
||
| 830 | * @param \Closure $closure |
||
| 831 | * |
||
| 832 | * @return Arrayy |
||
| 833 | */ |
||
| 834 | 22 | View Code Duplication | public function each(\Closure $closure) |
| 844 | |||
| 845 | /** |
||
| 846 | * alias: for "Arrayy->getArray()" |
||
| 847 | */ |
||
| 848 | 143 | public function toArray() |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Check if any item in the current array matches a truth test. |
||
| 855 | * |
||
| 856 | * @param \Closure $closure |
||
| 857 | * |
||
| 858 | * @return bool |
||
| 859 | */ |
||
| 860 | 9 | View Code Duplication | public function matchesAny(\Closure $closure) |
| 874 | |||
| 875 | /** |
||
| 876 | * Check if we have named keys in the current array. |
||
| 877 | * |
||
| 878 | * @return bool |
||
| 879 | */ |
||
| 880 | 12 | public function isAssoc() |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Check whether array is numeric or not. |
||
| 891 | * |
||
| 892 | * @return bool Returns true if numeric, false otherwise |
||
| 893 | */ |
||
| 894 | public function isNumeric() |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Check whether the array is empty or not. |
||
| 914 | * |
||
| 915 | * @return bool Returns true if empty, false otherwise |
||
| 916 | */ |
||
| 917 | public function isEmpty() |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Returns the value at specified offset. |
||
| 924 | * |
||
| 925 | * @param mixed $offset |
||
| 926 | * |
||
| 927 | * @return mixed return null if the offset did not exists |
||
| 928 | */ |
||
| 929 | 8 | public function offsetGet($offset) |
|
| 933 | |||
| 934 | /** |
||
| 935 | * alias: for "Arrayy->keys()" |
||
| 936 | * |
||
| 937 | * @return Arrayy |
||
| 938 | */ |
||
| 939 | public function getKeys() |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Get all keys from the current array. |
||
| 946 | * |
||
| 947 | * @return Arrayy |
||
| 948 | */ |
||
| 949 | 1 | public function keys() |
|
| 955 | |||
| 956 | /** |
||
| 957 | * Check if the current array is a multi-array. |
||
| 958 | * |
||
| 959 | * @return bool |
||
| 960 | */ |
||
| 961 | 13 | public function isMultiArray() |
|
| 965 | |||
| 966 | /** |
||
| 967 | * Check if an item is in the current array. |
||
| 968 | * |
||
| 969 | * @param mixed $value |
||
| 970 | * |
||
| 971 | * @return bool |
||
| 972 | */ |
||
| 973 | 9 | public function contains($value) |
|
| 977 | |||
| 978 | /** |
||
| 979 | * Check if the given key/index exists in the array. |
||
| 980 | * |
||
| 981 | * @param mixed $key Key/index to search for |
||
| 982 | * |
||
| 983 | * @return bool Returns true if the given key/index exists in the array, false otherwise |
||
| 984 | */ |
||
| 985 | public function containsKey($key) |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Returns the average value of the current array. |
||
| 992 | * |
||
| 993 | * @param int $decimals The number of decimals to return |
||
| 994 | * |
||
| 995 | * @return int|double The average value |
||
| 996 | */ |
||
| 997 | 10 | public function average($decimals = null) |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate interface. |
||
| 1014 | * |
||
| 1015 | * @return \ArrayIterator An iterator for the values in the array. |
||
| 1016 | */ |
||
| 1017 | 6 | public function getIterator() |
|
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Count the values from the current array. |
||
| 1024 | * |
||
| 1025 | * INFO: only a alias for "$arrayy->size()" |
||
| 1026 | * |
||
| 1027 | * @return int |
||
| 1028 | */ |
||
| 1029 | 10 | public function length() |
|
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Get the max value from an array. |
||
| 1036 | * |
||
| 1037 | * @return mixed |
||
| 1038 | */ |
||
| 1039 | 10 | public function max() |
|
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Get the min value from an array. |
||
| 1050 | * |
||
| 1051 | * @return mixed |
||
| 1052 | */ |
||
| 1053 | 10 | public function min() |
|
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Find the first item in an array that passes the truth test, |
||
| 1064 | * otherwise return false |
||
| 1065 | * |
||
| 1066 | * @param \Closure $closure |
||
| 1067 | * |
||
| 1068 | * @return mixed|false false if we did not find the value |
||
| 1069 | */ |
||
| 1070 | 7 | public function find(\Closure $closure) |
|
| 1080 | |||
| 1081 | /** |
||
| 1082 | * WARNING!!! -> Clear the current array. |
||
| 1083 | * |
||
| 1084 | * @return $this will always return an empty Arrayy object |
||
| 1085 | */ |
||
| 1086 | 4 | public function clear() |
|
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Clean all falsy values from an array. |
||
| 1095 | * |
||
| 1096 | * @return Arrayy |
||
| 1097 | */ |
||
| 1098 | 8 | public function clean() |
|
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Find all items in an array that pass the truth test. |
||
| 1109 | * |
||
| 1110 | * @param \Closure|null $closure |
||
| 1111 | * |
||
| 1112 | * @return Arrayy |
||
| 1113 | */ |
||
| 1114 | 8 | public function filter($closure = null) |
|
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Get a random value from an array, with the ability to skew the results. |
||
| 1127 | * |
||
| 1128 | * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
||
| 1129 | * |
||
| 1130 | * @param array $array |
||
| 1131 | * @param null|int $number how many values you will take? |
||
| 1132 | * |
||
| 1133 | * @return Arrayy |
||
| 1134 | */ |
||
| 1135 | 9 | public function randomWeighted(array $array, $number = null) |
|
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Search for the first index of the current array via $value. |
||
| 1151 | * |
||
| 1152 | * @param mixed $value |
||
| 1153 | * |
||
| 1154 | * @return Arrayy will return a empty Arrayy if the index was not found |
||
| 1155 | */ |
||
| 1156 | 16 | public function searchIndex($value) |
|
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Merge the new $array into the current array. |
||
| 1171 | * |
||
| 1172 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 1173 | * |
||
| 1174 | * @param array $array |
||
| 1175 | * @param bool $recursive |
||
| 1176 | * |
||
| 1177 | * @return Arrayy |
||
| 1178 | */ |
||
| 1179 | 25 | View Code Duplication | public function mergeAppendKeepIndex(array $array = array(), $recursive = false) |
| 1189 | |||
| 1190 | /** |
||
| 1191 | * alias: for "Arrayy->searchIndex()" |
||
| 1192 | * |
||
| 1193 | * @param mixed $value Value to search for |
||
| 1194 | * |
||
| 1195 | * @return Arrayy will return a empty Arrayy if the index was not found |
||
| 1196 | */ |
||
| 1197 | public function indexOf($value) |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 1204 | * |
||
| 1205 | * @param array $search |
||
| 1206 | * |
||
| 1207 | * @return bool |
||
| 1208 | */ |
||
| 1209 | 1 | public function intersects(array $search) |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Return an array with all elements found in input array. |
||
| 1216 | * |
||
| 1217 | * @param array $search |
||
| 1218 | * |
||
| 1219 | * @return Arrayy |
||
| 1220 | */ |
||
| 1221 | 2 | public function intersection(array $search) |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Get the last value(s) from the current array. |
||
| 1230 | * |
||
| 1231 | * @param int|null $number |
||
| 1232 | * |
||
| 1233 | * @return Arrayy |
||
| 1234 | */ |
||
| 1235 | 11 | public function last($number = null) |
|
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Pop a specified value off the end of the current array. |
||
| 1250 | * |
||
| 1251 | * @return mixed The popped element from the current array. |
||
| 1252 | */ |
||
| 1253 | 11 | public function pop() |
|
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Get the last elements from index $from until the end of this array. |
||
| 1260 | * |
||
| 1261 | * @param int $from |
||
| 1262 | * |
||
| 1263 | * @return Arrayy |
||
| 1264 | */ |
||
| 1265 | 16 | public function rest($from = 1) |
|
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Get everything but the last..$to items. |
||
| 1274 | * |
||
| 1275 | * @param int $to |
||
| 1276 | * |
||
| 1277 | * @return Arrayy |
||
| 1278 | */ |
||
| 1279 | 12 | public function initial($to = 1) |
|
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Extract a slice of the array. |
||
| 1288 | * |
||
| 1289 | * @param int $offset Slice begin index |
||
| 1290 | * @param int|null $length Length of the slice |
||
| 1291 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
| 1292 | * |
||
| 1293 | * @return static A slice of the original array with length $length |
||
| 1294 | */ |
||
| 1295 | 4 | public function slice($offset, $length = null, $preserveKeys = false) |
|
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Iterate over an array and execute a callback for each loop. |
||
| 1304 | * |
||
| 1305 | * @param \Closure $closure |
||
| 1306 | * |
||
| 1307 | * @return Arrayy |
||
| 1308 | */ |
||
| 1309 | 2 | View Code Duplication | public function at(\Closure $closure) |
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Merge the new $array into the current array. |
||
| 1322 | * |
||
| 1323 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
| 1324 | * - create new indexes |
||
| 1325 | * |
||
| 1326 | * @param array $array |
||
| 1327 | * @param bool $recursive |
||
| 1328 | * |
||
| 1329 | * @return Arrayy |
||
| 1330 | */ |
||
| 1331 | 16 | View Code Duplication | public function mergeAppendNewIndex(array $array = array(), $recursive = false) |
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Merge the current array into the new $array. |
||
| 1344 | * |
||
| 1345 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
| 1346 | * - create new indexes |
||
| 1347 | * |
||
| 1348 | * @param array $array |
||
| 1349 | * @param bool $recursive |
||
| 1350 | * |
||
| 1351 | * @return Arrayy |
||
| 1352 | */ |
||
| 1353 | 16 | View Code Duplication | public function mergePrependNewIndex(array $array = array(), $recursive = false) |
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Merge the the current array into the $array. |
||
| 1366 | * |
||
| 1367 | * - use key,value from the new $array, also if the index is in the current array |
||
| 1368 | * |
||
| 1369 | * @param array $array |
||
| 1370 | * @param bool $recursive |
||
| 1371 | * |
||
| 1372 | * @return Arrayy |
||
| 1373 | */ |
||
| 1374 | 16 | View Code Duplication | public function mergePrependKeepIndex(array $array = array(), $recursive = false) |
| 1384 | |||
| 1385 | /** |
||
| 1386 | * Return values that are only in the current array. |
||
| 1387 | * |
||
| 1388 | * @param array $array |
||
| 1389 | * |
||
| 1390 | * @return Arrayy |
||
| 1391 | */ |
||
| 1392 | 12 | public function diff(array $array = array()) |
|
| 1398 | |||
| 1399 | /** |
||
| 1400 | * Return values that are only in the new $array. |
||
| 1401 | * |
||
| 1402 | * @param array $array |
||
| 1403 | * |
||
| 1404 | * @return Arrayy |
||
| 1405 | */ |
||
| 1406 | 8 | public function diffReverse(array $array = array()) |
|
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Replace the first matched value in an array. |
||
| 1415 | * |
||
| 1416 | * @param mixed $search |
||
| 1417 | * @param mixed $replacement |
||
| 1418 | * |
||
| 1419 | * @return Arrayy |
||
| 1420 | */ |
||
| 1421 | 3 | public function replaceOneValue($search, $replacement = '') |
|
| 1432 | |||
| 1433 | /** |
||
| 1434 | * Replace values in the current array. |
||
| 1435 | * |
||
| 1436 | * @param string $search The string to replace. |
||
| 1437 | * @param string $replacement What to replace it with. |
||
| 1438 | * |
||
| 1439 | * @return Arrayy |
||
| 1440 | */ |
||
| 1441 | 1 | public function replaceValues($search, $replacement = '') |
|
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Replace the keys in an array with another set. |
||
| 1454 | * |
||
| 1455 | * @param array $keys An array of keys matching the array's size |
||
| 1456 | * |
||
| 1457 | * @return Arrayy |
||
| 1458 | */ |
||
| 1459 | 1 | public function replaceKeys(array $keys) |
|
| 1466 | |||
| 1467 | /** |
||
| 1468 | * Create an array using the current array as keys and the other array as values. |
||
| 1469 | * |
||
| 1470 | * @param array $array Values array |
||
| 1471 | * |
||
| 1472 | * @return Arrayy Arrayy object with values from the other array. |
||
| 1473 | */ |
||
| 1474 | 1 | public function replaceAllValues(array $array) |
|
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Create an array using the current array as values and the other array as keys. |
||
| 1483 | * |
||
| 1484 | * @param array $keys Keys array |
||
| 1485 | * |
||
| 1486 | * @return Arrayy Arrayy object with keys from the other array. |
||
| 1487 | */ |
||
| 1488 | 1 | public function replaceAllKeys(array $keys) |
|
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Shuffle the current array. |
||
| 1497 | * |
||
| 1498 | * @return Arrayy |
||
| 1499 | */ |
||
| 1500 | 1 | public function shuffle() |
|
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Split an array in the given amount of pieces. |
||
| 1511 | * |
||
| 1512 | * @param int $numberOfPieces |
||
| 1513 | * @param bool $keepKeys |
||
| 1514 | * |
||
| 1515 | * @return array |
||
| 1516 | */ |
||
| 1517 | 1 | public function split($numberOfPieces = 2, $keepKeys = false) |
|
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Create a chunked version of this array. |
||
| 1532 | * |
||
| 1533 | * @param int $size Size of each chunk |
||
| 1534 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
| 1535 | * |
||
| 1536 | * @return static A new array of chunks from the original array |
||
| 1537 | */ |
||
| 1538 | 4 | public function chunk($size, $preserveKeys = false) |
|
| 1544 | |||
| 1545 | /** |
||
| 1546 | * Returns the values from a single column of the input array, identified by |
||
| 1547 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 1548 | * |
||
| 1549 | * Info: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 1550 | * array by the values from the $indexKey column in the input array. |
||
| 1551 | * |
||
| 1552 | * @param mixed $columnKey |
||
| 1553 | * @param mixed $indexKey |
||
| 1554 | * |
||
| 1555 | * @return Arrayy |
||
| 1556 | */ |
||
| 1557 | 1 | public function getColumn($columnKey = null, $indexKey = null) |
|
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Invoke a function on all of an array's values. |
||
| 1566 | * |
||
| 1567 | * @param mixed $callable |
||
| 1568 | * @param array $arguments |
||
| 1569 | * |
||
| 1570 | * @return Arrayy |
||
| 1571 | */ |
||
| 1572 | 1 | public function invoke($callable, $arguments = array()) |
|
| 1588 | |||
| 1589 | /** |
||
| 1590 | * Apply the given function to the every element of the array, |
||
| 1591 | * collecting the results. |
||
| 1592 | * |
||
| 1593 | * @param callable $callable |
||
| 1594 | * |
||
| 1595 | * @return Arrayy Arrayy object with modified elements |
||
| 1596 | */ |
||
| 1597 | 4 | public function map($callable) |
|
| 1603 | |||
| 1604 | /** |
||
| 1605 | * Check if a value is in the current array using a closure. |
||
| 1606 | * |
||
| 1607 | * @param \Closure $closure |
||
| 1608 | * |
||
| 1609 | * @return bool Returns true if the given value is found, false otherwise |
||
| 1610 | */ |
||
| 1611 | public function exists(\Closure $closure) |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * Return all items that fail the truth test. |
||
| 1626 | * |
||
| 1627 | * @param \Closure $closure |
||
| 1628 | * |
||
| 1629 | * @return Arrayy |
||
| 1630 | */ |
||
| 1631 | 1 | View Code Duplication | public function reject(\Closure $closure) |
| 1643 | |||
| 1644 | /** |
||
| 1645 | * Replace a key with a new key/value pair. |
||
| 1646 | * |
||
| 1647 | * @param $replace |
||
| 1648 | * @param $key |
||
| 1649 | * @param $value |
||
| 1650 | * |
||
| 1651 | * @return Arrayy |
||
| 1652 | */ |
||
| 1653 | 1 | public function replace($replace, $key, $value) |
|
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Remove a value from the current array (optional using dot-notation). |
||
| 1662 | * |
||
| 1663 | * @param mixed $key |
||
| 1664 | * |
||
| 1665 | * @return Arrayy |
||
| 1666 | */ |
||
| 1667 | 17 | public function remove($key) |
|
| 1682 | |||
| 1683 | /** |
||
| 1684 | * Internal mechanics of remove method. |
||
| 1685 | * |
||
| 1686 | * @param $key |
||
| 1687 | * |
||
| 1688 | * @return boolean |
||
| 1689 | */ |
||
| 1690 | 17 | protected function internalRemove($key) |
|
| 1712 | |||
| 1713 | /** |
||
| 1714 | * Check if an array has a given key. |
||
| 1715 | * |
||
| 1716 | * @param mixed $key |
||
| 1717 | * |
||
| 1718 | * @return bool |
||
| 1719 | */ |
||
| 1720 | 18 | public function has($key) |
|
| 1727 | |||
| 1728 | /** |
||
| 1729 | * Set a value for the current array (optional using dot-notation). |
||
| 1730 | * |
||
| 1731 | * @param string $key The key to set |
||
| 1732 | * @param mixed $value Its value |
||
| 1733 | * |
||
| 1734 | * @return Arrayy |
||
| 1735 | */ |
||
| 1736 | 14 | public function set($key, $value) |
|
| 1742 | |||
| 1743 | /** |
||
| 1744 | * Internal mechanic of set method. |
||
| 1745 | * |
||
| 1746 | * @param mixed $key |
||
| 1747 | * @param mixed $value |
||
| 1748 | * |
||
| 1749 | * @return bool |
||
| 1750 | */ |
||
| 1751 | 14 | protected function internalSet($key, $value) |
|
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Get a value from a array and set it if it was not. |
||
| 1778 | * |
||
| 1779 | * WARNING: this method only set the value, if the $key is not already set |
||
| 1780 | * |
||
| 1781 | * @param string $key The key |
||
| 1782 | * @param mixed $default The default value to set if it isn't |
||
| 1783 | * |
||
| 1784 | * @return mixed |
||
| 1785 | */ |
||
| 1786 | 9 | public function setAndGet($key, $default = null) |
|
| 1795 | |||
| 1796 | /** |
||
| 1797 | * Remove the first value from the current array. |
||
| 1798 | * |
||
| 1799 | * @return Arrayy |
||
| 1800 | */ |
||
| 1801 | 7 | public function removeFirst() |
|
| 1807 | |||
| 1808 | /** |
||
| 1809 | * Remove the last value from the current array. |
||
| 1810 | * |
||
| 1811 | * @return Arrayy |
||
| 1812 | */ |
||
| 1813 | 7 | public function removeLast() |
|
| 1819 | |||
| 1820 | /** |
||
| 1821 | * Removes a particular value from an array (numeric or associative). |
||
| 1822 | * |
||
| 1823 | * @param mixed $value |
||
| 1824 | * |
||
| 1825 | * @return Arrayy |
||
| 1826 | */ |
||
| 1827 | 7 | public function removeValue($value) |
|
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Pad array to the specified size with a given value. |
||
| 1848 | * |
||
| 1849 | * @param int $size Size of the result array |
||
| 1850 | * @param mixed $value Empty value by default |
||
| 1851 | * |
||
| 1852 | * @return Arrayy Arrayy object padded to $size with $value |
||
| 1853 | */ |
||
| 1854 | 4 | public function pad($size, $value) |
|
| 1860 | |||
| 1861 | /** |
||
| 1862 | * Prepend a value to an array. |
||
| 1863 | * |
||
| 1864 | * @param mixed $value |
||
| 1865 | * |
||
| 1866 | * @return Arrayy |
||
| 1867 | */ |
||
| 1868 | 7 | public function prepend($value) |
|
| 1874 | |||
| 1875 | /** |
||
| 1876 | * alias: for "Arrayy->append()" |
||
| 1877 | * |
||
| 1878 | * @param $value |
||
| 1879 | * |
||
| 1880 | * @return $this |
||
| 1881 | */ |
||
| 1882 | 1 | public function add($value) |
|
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Create a numerically re-indexed Arrayy object. |
||
| 1891 | * |
||
| 1892 | * @return Arrayy The new instance with re-indexed array-elements |
||
| 1893 | */ |
||
| 1894 | 4 | public function reindex() |
|
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Return the array in the reverse order. |
||
| 1903 | * |
||
| 1904 | * @return Arrayy |
||
| 1905 | */ |
||
| 1906 | 7 | public function reverse() |
|
| 1912 | |||
| 1913 | /** |
||
| 1914 | * Custom sort by value via "usort" |
||
| 1915 | * |
||
| 1916 | * @link http://php.net/manual/en/function.usort.php |
||
| 1917 | * |
||
| 1918 | * @param callable $func |
||
| 1919 | * |
||
| 1920 | * @return $this |
||
| 1921 | */ |
||
| 1922 | 4 | public function customSortValues(callable $func) |
|
| 1928 | |||
| 1929 | /** |
||
| 1930 | * Custom sort by index via "uksort" |
||
| 1931 | * |
||
| 1932 | * @link http://php.net/manual/en/function.uksort.php |
||
| 1933 | * |
||
| 1934 | * @param callable $func |
||
| 1935 | * |
||
| 1936 | * @return $this |
||
| 1937 | */ |
||
| 1938 | 4 | public function customSortKeys(callable $func) |
|
| 1944 | |||
| 1945 | /** |
||
| 1946 | * Sort the current array by key. |
||
| 1947 | * |
||
| 1948 | * @link http://php.net/manual/en/function.ksort.php |
||
| 1949 | * @link http://php.net/manual/en/function.krsort.php |
||
| 1950 | * |
||
| 1951 | * @param int|string $direction use SORT_ASC or SORT_DESC |
||
| 1952 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 1953 | * |
||
| 1954 | * @return $this |
||
| 1955 | */ |
||
| 1956 | 18 | public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1962 | |||
| 1963 | /** |
||
| 1964 | * sorting keys |
||
| 1965 | * |
||
| 1966 | * @param array $elements |
||
| 1967 | * @param int $direction |
||
| 1968 | * @param int $strategy |
||
| 1969 | */ |
||
| 1970 | 18 | protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1985 | |||
| 1986 | /** |
||
| 1987 | * Get correct PHP constant for direction. |
||
| 1988 | * |
||
| 1989 | * @param int|string $direction |
||
| 1990 | * |
||
| 1991 | * @return int |
||
| 1992 | */ |
||
| 1993 | 38 | protected function getDirection($direction) |
|
| 2015 | |||
| 2016 | /** |
||
| 2017 | * Sort the current array by value. |
||
| 2018 | * |
||
| 2019 | * @param int $direction use SORT_ASC or SORT_DESC |
||
| 2020 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 2021 | * |
||
| 2022 | * @return Arrayy |
||
| 2023 | */ |
||
| 2024 | 1 | public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 2028 | |||
| 2029 | /** |
||
| 2030 | * Sort the current array and optional you can keep the keys. |
||
| 2031 | * |
||
| 2032 | * @param string|int $direction use SORT_ASC or SORT_DESC |
||
| 2033 | * @param int|string $strategy |
||
| 2034 | * @param bool $keepKeys |
||
| 2035 | * |
||
| 2036 | * @return Arrayy |
||
| 2037 | */ |
||
| 2038 | 19 | public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
| 2044 | |||
| 2045 | /** |
||
| 2046 | * @param array &$elements |
||
| 2047 | * @param int|string $direction |
||
| 2048 | * @param int $strategy |
||
| 2049 | * @param bool $keepKeys |
||
| 2050 | */ |
||
| 2051 | 19 | protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
| 2078 | |||
| 2079 | /** |
||
| 2080 | * Sort the current array by value. |
||
| 2081 | * |
||
| 2082 | * @param int $direction use SORT_ASC or SORT_DESC |
||
| 2083 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 2084 | * |
||
| 2085 | * @return Arrayy |
||
| 2086 | */ |
||
| 2087 | 1 | public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 2091 | |||
| 2092 | /** |
||
| 2093 | * Sort a array by value, by a closure or by a property. |
||
| 2094 | * |
||
| 2095 | * - If the sorter is null, the array is sorted naturally. |
||
| 2096 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
| 2097 | * |
||
| 2098 | * @param null $sorter |
||
| 2099 | * @param string|int $direction |
||
| 2100 | * @param int $strategy |
||
| 2101 | * |
||
| 2102 | * @return Arrayy |
||
| 2103 | */ |
||
| 2104 | 1 | public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 2130 | |||
| 2131 | /** |
||
| 2132 | * Exchanges all keys with their associated values in an array. |
||
| 2133 | * |
||
| 2134 | * @return Arrayy |
||
| 2135 | */ |
||
| 2136 | 1 | public function flip() |
|
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Apply the given function to every element in the array, |
||
| 2145 | * discarding the results. |
||
| 2146 | * |
||
| 2147 | * @param callable $callable |
||
| 2148 | * @param bool $recursive Whether array will be walked recursively or no |
||
| 2149 | * |
||
| 2150 | * @return Arrayy An Arrayy object with modified elements |
||
| 2151 | */ |
||
| 2152 | 8 | public function walk($callable, $recursive = false) |
|
| 2162 | |||
| 2163 | /** |
||
| 2164 | * Reduce the current array via callable e.g. anonymous-function. |
||
| 2165 | * |
||
| 2166 | * @param mixed $predicate |
||
| 2167 | * @param array $init |
||
| 2168 | * |
||
| 2169 | * @return Arrayy |
||
| 2170 | */ |
||
| 2171 | 2 | public function reduce($predicate, array $init = array()) |
|
| 2177 | |||
| 2178 | /** |
||
| 2179 | * Return a duplicate free copy of the current array. |
||
| 2180 | * |
||
| 2181 | * @return Arrayy |
||
| 2182 | */ |
||
| 2183 | 7 | public function unique() |
|
| 2199 | |||
| 2200 | /** |
||
| 2201 | * Convert the current array to JSON. |
||
| 2202 | * |
||
| 2203 | * @param null $options e.g. JSON_PRETTY_PRINT |
||
| 2204 | * |
||
| 2205 | * @return string |
||
| 2206 | */ |
||
| 2207 | 1 | public function toJson($options = null) |
|
| 2211 | } |
||
| 2212 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: