From baf4de3b3c06568feedd676554838585ec41992e Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 8 Feb 2024 16:50:32 +0000 Subject: [PATCH 1/2] Support/Arr: Introduce new Arr::limit() helper --- src/Illuminate/Collections/Arr.php | 16 ++++++++++++++++ tests/Support/SupportArrTest.php | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 482bbadf0fb0..5d90d683edb5 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -225,6 +225,22 @@ public static function last($array, callable $callback = null, $default = null) return static::first(array_reverse($array, true), $callback, $default); } + /** + * Take the first or last {$limit} items from an array. + * + * @param array $array + * @param int $limit + * @return array + */ + public static function limit($array, $limit) + { + if ($limit < 0) { + return array_slice($array, $limit, abs($limit)); + } + + return array_slice($array, 0, $limit); + } + /** * Flatten a multi-dimensional array into a single level. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 083ca5bbb306..335642bff990 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -1260,4 +1260,17 @@ public function testPrependKeysWith() ], ], Arr::prependKeysWith($array, 'test.')); } + + public function testLimit() + { + $array = [1, 2, 3, 4, 5, 6]; + + $this->assertEquals([ + 1, 2, 3, + ], Arr::limit($array, 3)); + + $this->assertEquals([ + 4, 5, 6, + ], Arr::limit($array, -3)); + } } From c1a66c2c48f6520a8655f3b6410d3baf2edd2f54 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 9 Feb 2024 15:49:18 +0000 Subject: [PATCH 2/2] Arr: Rename limit() to take() for consistency --- src/Illuminate/Collections/Arr.php | 2 +- tests/Support/SupportArrTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 5d90d683edb5..f51accf1e408 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -232,7 +232,7 @@ public static function last($array, callable $callback = null, $default = null) * @param int $limit * @return array */ - public static function limit($array, $limit) + public static function take($array, $limit) { if ($limit < 0) { return array_slice($array, $limit, abs($limit)); diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 335642bff990..05f6b743f3e1 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -1261,16 +1261,16 @@ public function testPrependKeysWith() ], Arr::prependKeysWith($array, 'test.')); } - public function testLimit() + public function testTake() { $array = [1, 2, 3, 4, 5, 6]; $this->assertEquals([ 1, 2, 3, - ], Arr::limit($array, 3)); + ], Arr::take($array, 3)); $this->assertEquals([ 4, 5, 6, - ], Arr::limit($array, -3)); + ], Arr::take($array, -3)); } }