From 31dbc46a75d18bdccc6183994e68f1cbd90953ca Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:57:45 +0330 Subject: [PATCH 1/4] add reduce with keys to collections --- src/Illuminate/Collections/Collection.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index f4c7b4007a91..fd51e5de5ed8 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -884,6 +884,24 @@ public function reduce(callable $callback, $initial = null) return array_reduce($this->items, $callback, $initial); } + /** + * Reduce an associative collection to a single value. + * + * @param callable $callback + * @param mixed $initial + * @return mixed + */ + public function reduceWithKeys(callable $callback, $initial = null) + { + $result = $initial; + + foreach($this->items as $key => $value) { + $result = $callback($result, $value, $key); + } + + return $result; + } + /** * Replace the collection items with the given items. * From 4777a5415758f05049ee12765b66f71980bc0681 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:58:03 +0330 Subject: [PATCH 2/4] add reduce with keys to lazy collections --- src/Illuminate/Collections/LazyCollection.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 2384948f951a..4474ab863db5 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -845,6 +845,24 @@ public function reduce(callable $callback, $initial = null) return $result; } + /** + * Reduce an associative collection to a single value. + * + * @param callable $callback + * @param mixed $initial + * @return mixed + */ + public function reduceWithKeys(callable $callback, $initial = null) + { + $result = $initial; + + foreach($this as $key => $value) { + $result = $callback($result, $value, $key); + } + + return $result; + } + /** * Replace the collection items with the given items. * From 1b788e795621cba583803d36b2ef9d92674ba330 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:58:21 +0330 Subject: [PATCH 3/4] add test for reduce with keys --- tests/Support/SupportCollectionTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6faf65a1a3c2..a75d555c0749 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3592,6 +3592,20 @@ public function testReduce($collection) })); } + /** + * @dataProvider collectionClassProvider + */ + public function testReduceWithKeys($collection) + { + $data = new $collection([ + 'foo' => 'bar', + 'baz' => 'qux', + ]); + $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { + return $carry .= $key . $element; + })); + } + /** * @dataProvider collectionClassProvider */ From b84aaecb413b35ad1990c4210e2498df494a3895 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 13:15:19 +0330 Subject: [PATCH 4/4] fix style --- src/Illuminate/Collections/Collection.php | 2 +- src/Illuminate/Collections/LazyCollection.php | 2 +- tests/Support/SupportCollectionTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index fd51e5de5ed8..6a8ab88818aa 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -895,7 +895,7 @@ public function reduceWithKeys(callable $callback, $initial = null) { $result = $initial; - foreach($this->items as $key => $value) { + foreach ($this->items as $key => $value) { $result = $callback($result, $value, $key); } diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 4474ab863db5..ca51626b071e 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -856,7 +856,7 @@ public function reduceWithKeys(callable $callback, $initial = null) { $result = $initial; - foreach($this as $key => $value) { + foreach ($this as $key => $value) { $result = $callback($result, $value, $key); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index a75d555c0749..b622884fe6c1 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3602,7 +3602,7 @@ public function testReduceWithKeys($collection) 'baz' => 'qux', ]); $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { - return $carry .= $key . $element; + return $carry .= $key.$element; })); }