@@ -184,6 +184,10 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /* Wraps a callable with the purpose of fixing bad PHP sort implementations */ |
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @param Closure $callable |
|
| 190 | + */ |
|
| 187 | 191 | static private function wrapCallable($callable) |
| 188 | 192 | { |
| 189 | 193 | $direction = Arrgh::getSortDirection(); |
@@ -195,6 +199,10 @@ discard block |
||
| 195 | 199 | } |
| 196 | 200 | |
| 197 | 201 | /* Transforms the incoming calls to native calls */ |
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @param Arrgh $object |
|
| 205 | + */ |
|
| 198 | 206 | static private function invoke($method, $args, $object = null) |
| 199 | 207 | { |
| 200 | 208 | self::getSortDirection(); |
@@ -628,6 +636,7 @@ discard block |
||
| 628 | 636 | * |
| 629 | 637 | * @param array A collection |
| 630 | 638 | * @param callable A callable returning true or false depending on which way to partion the element—left or right. |
| 639 | + * @param Closure $callable |
|
| 631 | 640 | * @return [left, right] An array with two arrays—left and right. |
| 632 | 641 | */ |
| 633 | 642 | static private function arrgh_partition($array, $callable) |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | /* Returns an array */ |
| 42 | 42 | public function toArray() |
| 43 | 43 | { |
| 44 | - $array = array_map(function ($item) { |
|
| 44 | + $array = array_map(function($item) { |
|
| 45 | 45 | if ($item instanceof Arrgh) { |
| 46 | 46 | return $item->toArray(); |
| 47 | 47 | } |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | static private function wrapCallable($callable) |
| 188 | 188 | { |
| 189 | 189 | $direction = Arrgh::getSortDirection(); |
| 190 | - return function ($a, $b) use ($direction, $callable) { |
|
| 190 | + return function($a, $b) use ($direction, $callable) { |
|
| 191 | 191 | $result = $callable($a, $b); |
| 192 | 192 | if ($result === 0) return $direction; |
| 193 | 193 | return $result; |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | $function_name = $snake; |
| 204 | 204 | $function_name_prefixed = stripos($method, "array_") === 0 ? $snake : "array_" . $snake; |
| 205 | 205 | |
| 206 | - $all_function_names = [ $function_name, $function_name_prefixed ]; |
|
| 206 | + $all_function_names = [$function_name, $function_name_prefixed]; |
|
| 207 | 207 | $all_functions = self::allFunctions(); |
| 208 | 208 | |
| 209 | 209 | $matching_handler = null; |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | // asort in PHP5 reverses equals ("arsort" doen't mess up for some reason) |
| 227 | 227 | if ($matching_function === "asort") { |
| 228 | 228 | $matching_function = "uasort"; |
| 229 | - array_push($args, function ($a, $b) { return strcasecmp($a, $b); }); |
|
| 229 | + array_push($args, function($a, $b) { return strcasecmp($a, $b); }); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // Native array_column filters away null values. That means you cannot use array_column |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | $column_key = $args[1]; |
| 241 | 241 | if (count($args) === 3) { |
| 242 | 242 | $column_id = $args[2]; |
| 243 | - $column_ids_new = array_map(function ($item) use ($column_id) { return isset($item[$column_id]) ? $item[$column_id] : null; }, $column_array); |
|
| 244 | - $post_handler = function ($result) use ($column_ids_new) { |
|
| 243 | + $column_ids_new = array_map(function($item) use ($column_id) { return isset($item[$column_id]) ? $item[$column_id] : null; }, $column_array); |
|
| 244 | + $post_handler = function($result) use ($column_ids_new) { |
|
| 245 | 245 | return array_combine($column_ids_new, $result); |
| 246 | 246 | }; |
| 247 | 247 | } |
| 248 | 248 | $args = [$column_array]; |
| 249 | - array_push($args, function ($item) use ($column_key) { return isset($item[$column_key]) ? $item[$column_key] : null; }); |
|
| 249 | + array_push($args, function($item) use ($column_key) { return isset($item[$column_key]) ? $item[$column_key] : null; }); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | // If chain unshift array onto argument stack |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | |
| 257 | 257 | // If some arrays are Arrghs map to array or if callable, wrap it in new callable with |
| 258 | 258 | // info about sort direction. |
| 259 | - $args = array_map(function ($arg) use ($matching_function) { |
|
| 259 | + $args = array_map(function($arg) use ($matching_function) { |
|
| 260 | 260 | if ($arg instanceof Arrgh) { |
| 261 | 261 | return $arg->array; |
| 262 | 262 | } else if ($arg instanceof Closure) { |
@@ -383,14 +383,14 @@ discard block |
||
| 383 | 383 | return $array; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | - $column = array_map(function ($item) use ($key) { return isset($item[$key]) ? $item[$key] : null; }, $array); |
|
| 386 | + $column = array_map(function($item) use ($key) { return isset($item[$key]) ? $item[$key] : null; }, $array); |
|
| 387 | 387 | array_multisort($column, ($direction_int === 1 ? SORT_ASC : SORT_DESC), $array); |
| 388 | 388 | return $array; |
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | static private function arrgh_collapse($array) |
| 392 | 392 | { |
| 393 | - return array_reduce($array, function ($merged, $item) { |
|
| 393 | + return array_reduce($array, function($merged, $item) { |
|
| 394 | 394 | if (is_array($item)) { |
| 395 | 395 | return $merged = array_merge($merged, $item); |
| 396 | 396 | } |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | if ($key) { |
| 406 | 406 | $haystack = array_column($array, $key); |
| 407 | 407 | } else { |
| 408 | - $haystack = array_reduce($array, function ($merged, $item) { |
|
| 408 | + $haystack = array_reduce($array, function($merged, $item) { |
|
| 409 | 409 | return $merged = array_merge($merged, array_values($item)); |
| 410 | 410 | }, []); |
| 411 | 411 | } |
@@ -415,13 +415,13 @@ discard block |
||
| 415 | 415 | static private function arrgh_except($array, $except) |
| 416 | 416 | { |
| 417 | 417 | if (is_string($except)) { |
| 418 | - $except = [ $except ]; |
|
| 418 | + $except = [$except]; |
|
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | $is_collection = Arrgh::arrgh_is_collection($array); |
| 422 | - $array = $is_collection ? $array : [ $array ]; |
|
| 422 | + $array = $is_collection ? $array : [$array]; |
|
| 423 | 423 | |
| 424 | - $result = array_map(function ($item) use ($except) { |
|
| 424 | + $result = array_map(function($item) use ($except) { |
|
| 425 | 425 | foreach ($except as $key) { |
| 426 | 426 | unset($item[$key]); |
| 427 | 427 | } |
@@ -437,13 +437,13 @@ discard block |
||
| 437 | 437 | static private function arrgh_only($array, $only) |
| 438 | 438 | { |
| 439 | 439 | if (is_string($only)) { |
| 440 | - $only = [ $only ]; |
|
| 440 | + $only = [$only]; |
|
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | $is_collection = Arrgh::arrgh_is_collection($array); |
| 444 | - $array = $is_collection ? $array : [ $array ]; |
|
| 444 | + $array = $is_collection ? $array : [$array]; |
|
| 445 | 445 | |
| 446 | - $result = array_map(function ($item) use ($only) { |
|
| 446 | + $result = array_map(function($item) use ($only) { |
|
| 447 | 447 | foreach ($item as $key => $value) { |
| 448 | 448 | if (!in_array($key, $only)) { |
| 449 | 449 | unset($item[$key]); |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | $next_node = $data; |
| 517 | 517 | } else { |
| 518 | 518 | if ($is_collection) { |
| 519 | - $next_node = array_map(function ($item) use ($next_key) { |
|
| 519 | + $next_node = array_map(function($item) use ($next_key) { |
|
| 520 | 520 | if ($item !== null && array_key_exists($next_key, $item)) { |
| 521 | 521 | return $item[$next_key]; |
| 522 | 522 | } |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | if ($collapse) { |
| 574 | 574 | $result[] = $partial; |
| 575 | 575 | } else { |
| 576 | - $result[] = [ $partial ]; |
|
| 576 | + $result[] = [$partial]; |
|
| 577 | 577 | } |
| 578 | 578 | } |
| 579 | 579 | } |
@@ -616,7 +616,7 @@ discard block |
||
| 616 | 616 | |
| 617 | 617 | $depth = 0; |
| 618 | 618 | $child = array_shift($array); |
| 619 | - while(Arrgh::arrgh_is_collection($child)) { |
|
| 619 | + while (Arrgh::arrgh_is_collection($child)) { |
|
| 620 | 620 | $depth += 1; |
| 621 | 621 | $child = array_shift($child); |
| 622 | 622 | } |
@@ -634,24 +634,24 @@ discard block |
||
| 634 | 634 | { |
| 635 | 635 | $left = []; |
| 636 | 636 | $right = []; |
| 637 | - array_walk($array, function ($item, $key) use (&$left, &$right, $callable) { |
|
| 637 | + array_walk($array, function($item, $key) use (&$left, &$right, $callable) { |
|
| 638 | 638 | if ($callable($item, $key)) { |
| 639 | 639 | $left[] = $item; |
| 640 | 640 | } else { |
| 641 | 641 | $right[] = $item; |
| 642 | 642 | } |
| 643 | 643 | }); |
| 644 | - return [ $left, $right ]; |
|
| 644 | + return [$left, $right]; |
|
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | static private function arrgh_even($array) |
| 648 | 648 | { |
| 649 | - return Arrgh::arrgh_partition($array, function ($item, $key) { return $key % 2 === 0; })[0]; |
|
| 649 | + return Arrgh::arrgh_partition($array, function($item, $key) { return $key % 2 === 0; })[0]; |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | static private function arrgh_odd($array) |
| 653 | 653 | { |
| 654 | - return Arrgh::arrgh_partition($array, function ($item, $key) { return $key % 2 === 1; })[0]; |
|
| 654 | + return Arrgh::arrgh_partition($array, function($item, $key) { return $key % 2 === 1; })[0]; |
|
| 655 | 655 | } |
| 656 | 656 | |
| 657 | 657 | /* Synonym of shift */ |
@@ -189,7 +189,9 @@ discard block |
||
| 189 | 189 | $direction = Arrgh::getSortDirection(); |
| 190 | 190 | return function ($a, $b) use ($direction, $callable) { |
| 191 | 191 | $result = $callable($a, $b); |
| 192 | - if ($result === 0) return $direction; |
|
| 192 | + if ($result === 0) { |
|
| 193 | + return $direction; |
|
| 194 | + } |
|
| 193 | 195 | return $result; |
| 194 | 196 | }; |
| 195 | 197 | } |
@@ -611,8 +613,12 @@ discard block |
||
| 611 | 613 | */ |
| 612 | 614 | static private function arrgh_depth($array) |
| 613 | 615 | { |
| 614 | - if (empty($array) && is_array($array)) return 0; |
|
| 615 | - if (!Arrgh::arrgh_is_collection($array)) return null; |
|
| 616 | + if (empty($array) && is_array($array)) { |
|
| 617 | + return 0; |
|
| 618 | + } |
|
| 619 | + if (!Arrgh::arrgh_is_collection($array)) { |
|
| 620 | + return null; |
|
| 621 | + } |
|
| 616 | 622 | |
| 617 | 623 | $depth = 0; |
| 618 | 624 | $child = array_shift($array); |
@@ -29,6 +29,8 @@ |
||
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | // Define so it will not be defiend again |
| 32 | - if (!defined("ARRGH_IS_DEFINED")) define("ARRGH_IS_DEFINED", true); |
|
| 33 | -} |
|
| 32 | + if (!defined("ARRGH_IS_DEFINED")) { |
|
| 33 | + define("ARRGH_IS_DEFINED", true); |
|
| 34 | + } |
|
| 35 | + } |
|
| 34 | 36 | |