Skip to content

Commit

Permalink
Allow higher order groupBy (#23608)
Browse files Browse the repository at this point in the history
collect()->groupBy->computed() instead of
relying on a Closure value retriever.
  • Loading branch information
derekmd authored and taylorotwell committed Mar 19, 2018
1 parent 350ea24 commit 87838b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate
* @var array
*/
protected static $proxies = [
'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', 'keyBy',
'map', 'max', 'min', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique',
'average', 'avg', 'contains', 'each', 'every', 'filter', 'first',
'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition',
'reject', 'sortBy', 'sortByDesc', 'sum', 'unique',
];

/**
Expand Down
29 changes: 27 additions & 2 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,26 @@ public function testSplitEmptyCollection()
);
}

public function testHigherOrderCollectionGroupBy()
{
$collection = collect([
new TestSupportCollectionHigherOrderItem,
new TestSupportCollectionHigherOrderItem('TAYLOR'),
new TestSupportCollectionHigherOrderItem('foo'),
]);

$this->assertEquals([
'taylor' => [$collection[0]],
'TAYLOR' => [$collection[1]],
'foo' => [$collection[2]],
], $collection->groupBy->name->toArray());

$this->assertEquals([
'TAYLOR' => [$collection[0], $collection[1]],
'FOO' => [$collection[2]],
], $collection->groupBy->uppercase()->toArray());
}

public function testHigherOrderCollectionMap()
{
$person1 = (object) ['name' => 'Taylor'];
Expand Down Expand Up @@ -2647,11 +2667,16 @@ public function testGetWithNullReturnsNull()

class TestSupportCollectionHigherOrderItem
{
public $name = 'taylor';
public $name;

public function __construct($name = 'taylor')
{
$this->name = $name;
}

public function uppercase()
{
$this->name = strtoupper($this->name);
return $this->name = strtoupper($this->name);
}
}

Expand Down

0 comments on commit 87838b5

Please sign in to comment.