From c929ecb7c19df0ba42f2cda4508dd069a638f6d3 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Sat, 3 Dec 2016 22:04:30 +0000 Subject: [PATCH 1/3] Allow passing a key to the partition method in the collection class. --- src/Illuminate/Support/Collection.php | 10 ++++++---- tests/Support/SupportCollectionTest.php | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 429d46af3788..adb4402ddc8b 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -726,17 +726,19 @@ public function forPage($page, $perPage) } /** - * Partition the collection into two array using the given callback. + * Partition the collection into two arrays using the given callback or key. * - * @param callable $callback + * @param callable|string $callback * @return array */ - public function partition(callable $callback) + public function partition($callback) { $partitions = [new static(), new static()]; + $callback = $this->valueRetriever($callback); + foreach ($this->items as $item) { - $partitions[! (int) $callback($item)][] = $item; + $partitions[(int) ! $callback($item)][] = $item; } return $partitions; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index bdef348375d3..8c478cc7f79e 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1640,7 +1640,7 @@ public function testSplitEmptyCollection() ); } - public function testPartition() + public function testPartitionWithClosure() { $collection = new Collection(range(1, 10)); @@ -1652,6 +1652,19 @@ public function testPartition() $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->toArray()); } + public function testPartitionByKey() + { + $courses = new Collection([ + ['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'], + ]); + + list($free, $premium) = $courses->partition('free'); + + $this->assertSame([['free' => true, 'title' => 'Basic']], $free->toArray()); + + $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->toArray()); + } + public function testPartitionEmptyCollection() { $collection = new Collection(); From 6cc3068768ce8eff105569657c21e66a540d9c8b Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Mon, 5 Dec 2016 10:21:18 +0000 Subject: [PATCH 2/3] Preserve keys in the partition method --- src/Illuminate/Support/Collection.php | 4 ++-- tests/Support/SupportCollectionTest.php | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index adb4402ddc8b..ce23af8ea2a8 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -737,8 +737,8 @@ public function partition($callback) $callback = $this->valueRetriever($callback); - foreach ($this->items as $item) { - $partitions[(int) ! $callback($item)][] = $item; + foreach ($this->items as $key => $item) { + $partitions[(int) ! $callback($item)][$key] = $item; } return $partitions; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 8c478cc7f79e..ab256a6b93d8 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1648,8 +1648,8 @@ public function testPartitionWithClosure() return $i <= 5; }); - $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->toArray()); - $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->toArray()); + $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray()); + $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray()); } public function testPartitionByKey() @@ -1660,9 +1660,22 @@ public function testPartitionByKey() list($free, $premium) = $courses->partition('free'); - $this->assertSame([['free' => true, 'title' => 'Basic']], $free->toArray()); + $this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray()); - $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->toArray()); + $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray()); + } + + public function testPartitionPreservesKeys() + { + $courses = new Collection([ + 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true], + ]); + + list($free, $premium) = $courses->partition('free'); + + $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray()); + + $this->assertSame(['b' => ['free' => false]], $premium->toArray()); } public function testPartitionEmptyCollection() From 6fb879614ae1f818cbc7c55669a8b2678393aee8 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Mon, 5 Dec 2016 11:08:23 +0000 Subject: [PATCH 3/3] Return collection instead of array in the partition method. --- src/Illuminate/Support/Collection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index ce23af8ea2a8..a6ebdbf1511c 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -729,11 +729,11 @@ public function forPage($page, $perPage) * Partition the collection into two arrays using the given callback or key. * * @param callable|string $callback - * @return array + * @return static */ public function partition($callback) { - $partitions = [new static(), new static()]; + $partitions = [new static, new static]; $callback = $this->valueRetriever($callback); @@ -741,7 +741,7 @@ public function partition($callback) $partitions[(int) ! $callback($item)][$key] = $item; } - return $partitions; + return new static($partitions); } /**