From a37bcfc615daa8682efffb59fbe685aab93d19aa Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 22 Jul 2014 14:53:58 +0300 Subject: [PATCH] more groupBy methods --- src/collection.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/collection.php b/src/collection.php index 87332ee..d9b39ae 100644 --- a/src/collection.php +++ b/src/collection.php @@ -59,6 +59,14 @@ final class Collection return $result; } + public static function groupBy($collection, $key) + { + $groups = array(); + foreach ($collection as $item) + Arr::pushToKey($groups, call_user_func($key, $item), $item); + return $groups; + } + public static function groupByProperty($collection, $propertyName) { $groups = array(); @@ -68,5 +76,20 @@ final class Collection } return $groups; } + + public static function groupByMethod(/*$collection, $methodName, ...$args*/) + { + $args = func_get_args(); + $collection = $args[0]; + $methodName = $args[1]; + $methodArgs = array_slice($args, 2); + + $groups = array(); + foreach ($collection as $item) { + $key = call_user_func_array(array($item, $methodName), $methodArgs); + Arr::pushToKey($groups, $key, $item); + } + return $groups; + } } -- 2.49.0