From b4f148060842eeebb08edb005978360767af3630 Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Mon, 16 Oct 2017 09:11:46 -0700 Subject: [PATCH] Laravel 5.5.16 changes (#63) --- src/Illuminate/Support/Arr.php | 2 +- src/Illuminate/Support/Collection.php | 6 ++++-- tests/Support/SupportCollectionTest.php | 11 +++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 075ad68..a9e09ce 100644 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -122,7 +122,7 @@ public static function dot($array, $prepend = '') } /** - * Get all of the given array except for a specified array of items. + * Get all of the given array except for a specified array of keys. * * @param array $array * @param array|string $keys diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 1937a0f..1389879 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -34,7 +34,7 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate */ protected static $proxies = [ 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', - 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', + 'keyBy', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', ]; /** @@ -1055,7 +1055,9 @@ public function only($keys) */ public function forPage($page, $perPage) { - return $this->slice(($page - 1) * $perPage, $perPage); + $offset = max(0, ($page - 1) * $perPage); + + return $this->slice($offset, $perPage); } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 1de6fd2..2365217 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -307,6 +307,16 @@ public function testFilter() })->all()); } + public function testHigherOrderKeyBy() + { + $c = new Collection([ + ['id' => 'id1', 'name' => 'first'], + ['id' => 'id2', 'name' => 'second'], + ]); + + $this->assertEquals(['id1' => 'first', 'id2' => 'second'], $c->keyBy->id->map->name->all()); + } + public function testHigherOrderFilter() { $c = new Collection([ @@ -1758,6 +1768,7 @@ public function testKeys() public function testPaginate() { $c = new Collection(['one', 'two', 'three', 'four']); + $this->assertEquals(['one', 'two'], $c->forPage(0, 2)->all()); $this->assertEquals(['one', 'two'], $c->forPage(1, 2)->all()); $this->assertEquals([2 => 'three', 3 => 'four'], $c->forPage(2, 2)->all()); $this->assertEquals([], $c->forPage(3, 2)->all());