From 53efa7088f2c8f4465598abeeff5372f4280a6ec Mon Sep 17 00:00:00 2001 From: Lloople Date: Sun, 27 Aug 2017 11:01:04 +0200 Subject: [PATCH] Add the key to the callback on partition method --- src/Illuminate/Support/Collection.php | 2 +- tests/Support/SupportCollectionTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 8ebebf4678ca..3d507c61ecfe 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1046,7 +1046,7 @@ public function partition($callback) $callback = $this->valueRetriever($callback); foreach ($this->items as $key => $item) { - $partitions[(int) ! $callback($item)][$key] = $item; + $partitions[(int) ! $callback($item, $key)][$key] = $item; } return new static($partitions); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index e25add367bfa..e4df03bc41fd 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -2111,6 +2111,19 @@ public function testPartition() $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray()); } + public function testPartitionCallbackWithKey() + { + $collection = new Collection(['zero', 'one','two', 'three']); + + list($even, $odd) = $collection->partition(function ($item, $index) { + return $index % 2 === 0; + }); + + $this->assertEquals(['zero', 'two'], $even->values()->toArray()); + + $this->assertEquals(['one', 'three'], $odd->values()->toArray()); + } + public function testPartitionByKey() { $courses = new Collection([