Skip to content

Commit

Permalink
Laravel 5.5.32 changes (tighten#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Jan 25, 2018
1 parent 07d58f7 commit 0db8c8a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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));
Expand Down
52 changes: 50 additions & 2 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]', 'missing']))->all());

$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except(['last'])->all());
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except('last')->all());
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except(collect(['last' => 'Otwell']))->all());
}

public function testPluckWithArrayAndObjectValues()
Expand Down Expand Up @@ -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']]);
Expand Down Expand Up @@ -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' => '[email protected]'], $data->only(['first', 'email'])->all());
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->only('first', 'email')->all());
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->only(collect(['first', 'email']))->all());
}

public function testGettingAvgItemsFromCollection()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0db8c8a

Please sign in to comment.