From 735c66d90e7cf59f33be56bafd511aa5fc1700be Mon Sep 17 00:00:00 2001 From: freek Date: Fri, 2 Dec 2016 09:06:33 +0100 Subject: [PATCH 1/4] add partition collection method --- src/Illuminate/Support/Collection.php | 17 +++++++++++++++++ tests/Support/SupportCollectionTest.php | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 990729220663..5b7e3e0645ec 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -725,6 +725,23 @@ public function forPage($page, $perPage) return $this->slice(($page - 1) * $perPage, $perPage); } + /** + * Output a collection with two elements. Items in the first element did pass + * the given $callback, items in the second element did not. + * + * @param callable $callback + * @return static + */ + public function partition(callable $callback) { + $partitions = [new static(), new static()]; + + foreach ($this->items as $item) { + $partitions[! (int) $callback($item)][] = $item; + } + + return new static($partitions); + } + /** * Pass the collection to the given callback and return the result. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 05b7e4373643..59e91917546d 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1640,6 +1640,27 @@ public function testSplitEmptyCollection() })->toArray() ); } + + public function testPartition() + { + $collection = new Collection(range(1, 10)); + + $collection = $collection->partition(function ($i) { + return $i <= 5; + }); + + $this->assertEquals([1, 2, 3, 4, 5], $collection[0]->toArray()); + $this->assertEquals([6, 7, 8, 9, 10], $collection[1]->toArray()); + } + + public function testPartitionEmptyCollection() + { + $collection = new Collection(); + + $this->assertCount(2, $collection->partition(function () { + return true; + })); + } } class TestAccessorEloquentTestStub From 9ef387bb6b47ff335e3ae116b6d24fb2af8a61fb Mon Sep 17 00:00:00 2001 From: freek Date: Fri, 2 Dec 2016 09:13:03 +0100 Subject: [PATCH 2/4] fix cs --- src/Illuminate/Support/Collection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 5b7e3e0645ec..3f24cfcca32c 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -732,7 +732,8 @@ public function forPage($page, $perPage) * @param callable $callback * @return static */ - public function partition(callable $callback) { + public function partition(callable $callback) + { $partitions = [new static(), new static()]; foreach ($this->items as $item) { From 69379b4699619ed84b4f183e148e0e3083630d4d Mon Sep 17 00:00:00 2001 From: freek Date: Fri, 2 Dec 2016 16:15:22 +0100 Subject: [PATCH 3/4] return array --- src/Illuminate/Support/Collection.php | 6 +++--- tests/Support/SupportCollectionTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 3f24cfcca32c..a489818b7157 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -726,11 +726,11 @@ public function forPage($page, $perPage) } /** - * Output a collection with two elements. Items in the first element did pass + * Returns an array with two elements. Items in the first element did pass * the given $callback, items in the second element did not. * * @param callable $callback - * @return static + * @return array */ public function partition(callable $callback) { @@ -740,7 +740,7 @@ public function partition(callable $callback) $partitions[! (int) $callback($item)][] = $item; } - return new static($partitions); + return $partitions; } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 59e91917546d..87a11af240cf 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1645,12 +1645,12 @@ public function testPartition() { $collection = new Collection(range(1, 10)); - $collection = $collection->partition(function ($i) { + list($firstPartition, $secondPartition) = $collection->partition(function ($i) { return $i <= 5; }); - $this->assertEquals([1, 2, 3, 4, 5], $collection[0]->toArray()); - $this->assertEquals([6, 7, 8, 9, 10], $collection[1]->toArray()); + $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->toArray()); + $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->toArray()); } public function testPartitionEmptyCollection() From b901b7d12cc4ff45abd0b0d526df4a0dd7090dd6 Mon Sep 17 00:00:00 2001 From: freek Date: Fri, 2 Dec 2016 23:44:45 +0100 Subject: [PATCH 4/4] fix docblock --- src/Illuminate/Support/Collection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index a489818b7157..b652ca57a447 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -726,8 +726,7 @@ public function forPage($page, $perPage) } /** - * Returns an array with two elements. Items in the first element did pass - * the given $callback, items in the second element did not. + * Partition the collection into two array using the given callback. * * @param callable $callback * @return array