From e276b3d4bf2a124c4eb5975a8a2724b8c806139a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 4 Nov 2016 08:45:49 -0500 Subject: [PATCH] code cleanup and formatting --- src/Illuminate/Support/Collection.php | 59 ++++++------------------- tests/Support/SupportCollectionTest.php | 23 ++++++++++ 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 5b325f944461..161f3cf296eb 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -3,6 +3,7 @@ namespace Illuminate\Support; use Countable; +use Exception; use ArrayAccess; use Traversable; use ArrayIterator; @@ -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. * @@ -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); } } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 05b7e4373643..4034ee9b4be8 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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