Skip to content

Commit

Permalink
code cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 4, 2016
1 parent 8dd57f9 commit e276b3d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
59 changes: 14 additions & 45 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support;

use Countable;
use Exception;
use ArrayAccess;
use Traversable;
use ArrayIterator;
Expand Down Expand Up @@ -1266,25 +1267,6 @@ public function toBase()
return new self($this);
}

public function __get($name)
{
$nameToMethod = [
'where' => 'filter',
'unless' => 'reject',
'do' => 'each',
'extract' => 'map',
'sum' => 'sum',
'inOrderOf' => 'sortBy',
'inReverseOrderOf' => 'sortByDesc',
];

if (isset($nameToMethod[$name])) {
throw new \Exception('Not accessible.');
}

return new HigherOrderProxy($this, $nameToMethod[$name]);
}

/**
* Determine if an item exists at an offset.
*
Expand Down Expand Up @@ -1368,37 +1350,24 @@ protected function getArrayableItems($items)

return (array) $items;
}
}

class HigherOrderProxy
{
/**
* @var Collection
*/
protected $collection;

/**
* @var string
* Dynamically access collection proxies.
*
* @param string $key
* @return mixed
*/
protected $method;

public function __construct(Collection $collection, $method)
public function __get($key)
{
$this->collection = $collection;
$this->method = $method;
}
$proxies = [
'each', 'map', 'first', 'sortBy',
'sortByDesc', 'sum', 'reject', 'filter',
];

public function __call($name, $arguments)
{
return $this->collection->{$this->method}(function($value) use ($name, $arguments) {
return call_user_func_array([$value, $name], $arguments);
});
}
if (! in_array($key, $proxies)) {
throw new Exception("Property [{$key}] does not exist on this collection instance.");
}

public function __get($name)
{
return $this->collection->{$this->method}(function($value) use ($name) {
return $value->$name;
});
return new HigherOrderCollectionProxy($this, $key);
}
}
23 changes: 23 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,29 @@ public function testSplitEmptyCollection()
})->toArray()
);
}

public function testHigherOrderCollectionMap()
{
$person1 = (object) ['name' => 'Taylor'];
$person2 = (object) ['name' => 'Yaz'];

$collection = collect([$person1, $person2]);

$this->assertEquals(['Taylor', 'Yaz'], $collection->map->name->toArray());

$collection = collect([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);

$this->assertEquals(['TAYLOR', 'TAYLOR'], $collection->each->uppercase()->map->name->toArray());
}
}

class TestSupportCollectionHigherOrderItem
{
public $name = 'taylor';
public function uppercase()
{
$this->name = strtoupper($this->name);
}
}

class TestAccessorEloquentTestStub
Expand Down

0 comments on commit e276b3d

Please sign in to comment.