From 85756380e7bc2353ed038c15c40cc79aaafe6d08 Mon Sep 17 00:00:00 2001 From: Ed Grosvenor Date: Wed, 21 Jul 2021 05:26:47 -0400 Subject: [PATCH 1/4] Allow shift() and pop() to take multiple items from a collection --- src/Illuminate/Collections/Collection.php | 30 ++++++++++++++++++++--- tests/Support/SupportCollectionTest.php | 16 ++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index d1e94b2cd322..73d0d439b08b 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -790,9 +790,20 @@ public function only($keys) * * @return mixed */ - public function pop() + public function pop($number = 1) { - return array_pop($this->items); + if ($number === 1) { + return array_pop($this->items); + } + + $results = []; + + foreach (range(1, $number) as $item) { + array_push($results, array_pop($this->items)); + } + + return collect($results); + } /** @@ -943,9 +954,20 @@ public function search($value, $strict = false) * * @return mixed */ - public function shift() + public function shift($number = 1) { - return array_shift($this->items); + if ($number === 1) { + return array_shift($this->items); + } + + $results = []; + + foreach (range(1, $number) as $item) { + array_push($results, array_shift($this->items)); + } + + return collect($results); + } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index c77addd9ffc0..e33cc13b8cd8 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -225,6 +225,14 @@ public function testPopReturnsAndRemovesLastItemInCollection() $this->assertSame('foo', $c->first()); } + public function testPopReturnsAndRemovesLastXItemsInCollection() + { + $c = new Collection(['foo', 'bar', 'baz']); + + $this->assertEquals(new Collection(['baz', 'bar']), $c->pop(2)); + $this->assertSame('foo', $c->first()); + } + public function testShiftReturnsAndRemovesFirstItemInCollection() { $data = new Collection(['Taylor', 'Otwell']); @@ -235,6 +243,14 @@ public function testShiftReturnsAndRemovesFirstItemInCollection() $this->assertNull($data->first()); } + public function testShiftReturnsAndRemovesFirstXItemsInCollection() + { + $data = new Collection(['foo', 'bar', 'baz']); + + $this->assertEquals(new Collection(['foo', 'bar']), $data->shift(2)); + $this->assertSame('baz', $data->first()); + } + /** * @dataProvider collectionClassProvider */ From 33f81e7655d70d3966c8526d53c598ea7cb4edaa Mon Sep 17 00:00:00 2001 From: Ed Grosvenor Date: Wed, 21 Jul 2021 05:40:29 -0400 Subject: [PATCH 2/4] Make styleci happy --- src/Illuminate/Collections/Collection.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 73d0d439b08b..2390ab4c8a71 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -803,7 +803,6 @@ public function pop($number = 1) } return collect($results); - } /** @@ -967,7 +966,6 @@ public function shift($number = 1) } return collect($results); - } /** From 81ae758e11917034896c6f64829fcdd0eb79ee73 Mon Sep 17 00:00:00 2001 From: Ed Grosvenor Date: Wed, 21 Jul 2021 08:11:05 -0400 Subject: [PATCH 3/4] Add param with typehint to docblock --- src/Illuminate/Collections/Collection.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 2390ab4c8a71..517961d826ae 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -788,6 +788,7 @@ public function only($keys) /** * Get and remove the last item from the collection. * + * @param int $number * @return mixed */ public function pop($number = 1) @@ -951,6 +952,7 @@ public function search($value, $strict = false) /** * Get and remove the first item from the collection. * + * @param int $number * @return mixed */ public function shift($number = 1) From 220dd071860ca0151aeacc8f623b468131a3273c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Jul 2021 07:13:27 -0500 Subject: [PATCH 4/4] formatting --- src/Illuminate/Collections/Collection.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 2390ab4c8a71..39c611198d48 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -786,23 +786,24 @@ public function only($keys) } /** - * Get and remove the last item from the collection. + * Get and remove the last N items from the collection. * + * @param int $count * @return mixed */ - public function pop($number = 1) + public function pop($count = 1) { - if ($number === 1) { + if ($count === 1) { return array_pop($this->items); } $results = []; - foreach (range(1, $number) as $item) { + foreach (range(1, $count) as $item) { array_push($results, array_pop($this->items)); } - return collect($results); + return new static($results); } /** @@ -949,23 +950,24 @@ public function search($value, $strict = false) } /** - * Get and remove the first item from the collection. + * Get and remove the first N items from the collection. * + * @param int $count * @return mixed */ - public function shift($number = 1) + public function shift($count = 1) { - if ($number === 1) { + if ($count === 1) { return array_shift($this->items); } $results = []; - foreach (range(1, $number) as $item) { + foreach (range(1, $count) as $item) { array_push($results, array_shift($this->items)); } - return collect($results); + return new static($results); } /**