From 0db8c8aa2aae44af756437ba6e98d9b0bd1ecec3 Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Thu, 25 Jan 2018 11:26:27 -0800 Subject: [PATCH] Laravel 5.5.32 changes (#72) --- src/Illuminate/Support/Collection.php | 22 ++++++++++- tests/Support/SupportCollectionTest.php | 52 ++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index c7285c4..ea584fc 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -271,6 +271,8 @@ public function crossJoin(...$lists) */ public function dd(...$args) { + http_response_code(500); + call_user_func_array([$this, 'dump'], $args); die(1); @@ -391,7 +393,7 @@ public function every($key, $operator = null, $value = null) public function except($keys) { if ($keys instanceof self) { - $keys = $keys->keys()->all(); + $keys = $keys->all(); } elseif (! is_array($keys)) { $keys = func_get_args(); } @@ -658,6 +660,12 @@ public function get($key, $default = null) */ public function groupBy($groupBy, $preserveKeys = false) { + if (is_array($groupBy)) { + $nextGroups = $groupBy; + + $groupBy = array_shift($nextGroups); + } + $groupBy = $this->valueRetriever($groupBy); $results = []; @@ -680,7 +688,13 @@ public function groupBy($groupBy, $preserveKeys = false) } } - return new static($results); + $result = new static($results); + + if (! empty($nextGroups)) { + return $result->map->groupBy($nextGroups, $preserveKeys); + } + + return $result; } /** @@ -1052,6 +1066,10 @@ public function only($keys) return new static($this->items); } + if ($keys instanceof self) { + $keys = $keys->all(); + } + $keys = is_array($keys) ? $keys : func_get_args(); return new static(Arr::only($this->items, $keys)); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index c49675d..0bb8c63 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -973,11 +973,9 @@ public function testExcept() $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all()); $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all()); - $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last' => 'Otwell', 'email' => 'taylorotwell@gmail.com', 'missing']))->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all()); - $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(collect(['last' => 'Otwell']))->all()); } public function testPluckWithArrayAndObjectValues() @@ -1601,6 +1599,48 @@ public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys() $this->assertEquals($expected_result, $result->toArray()); } + public function testGroupByMultiLevelAndClosurePreservingKeys() + { + $data = new Collection([ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']], + 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']], + ]); + + $result = $data->groupBy([ + 'skilllevel', + function ($item) { + return $item['roles']; + }, + ], true); + + $expected_result = [ + 1 => [ + 'Role_1' => [ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + ], + 'Role_3' => [ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + ], + 'Role_2' => [ + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + ], + ], + 2 => [ + 'Role_1' => [ + 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']], + ], + 'Role_2' => [ + 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']], + ], + ], + ]; + + $this->assertEquals($expected_result, $result->toArray()); + } + public function testKeyByAttribute() { $data = new Collection([['rating' => 1, 'name' => '1'], ['rating' => 2, 'name' => '2'], ['rating' => 3, 'name' => '3']]); @@ -1928,9 +1968,11 @@ public function testOnly() $this->assertEquals($data->all(), $data->only(null)->all()); $this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all()); $this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all()); + $this->assertEquals(['first' => 'Taylor'], $data->only(collect(['first', 'missing']))->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(['first', 'email'])->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only('first', 'email')->all()); + $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(collect(['first', 'email']))->all()); } public function testGettingAvgItemsFromCollection() @@ -2422,6 +2464,12 @@ public function testUnlessDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + + public function testGetWithNullReturnsNull() + { + $collection = new Collection([1, 2, 3]); + $this->assertNull($collection->get(null)); + } } class TestSupportCollectionHigherOrderItem