Skip to content

Commit

Permalink
Basic implementation of higher-order messages
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Nov 4, 2016
1 parent 722bf59 commit 8dd57f9
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,25 @@ 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 @@ -1350,3 +1369,36 @@ protected function getArrayableItems($items)
return (array) $items;
}
}

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

/**
* @var string
*/
protected $method;

public function __construct(Collection $collection, $method)
{
$this->collection = $collection;
$this->method = $method;
}

public function __call($name, $arguments)
{
return $this->collection->{$this->method}(function($value) use ($name, $arguments) {
return call_user_func_array([$value, $name], $arguments);
});
}

public function __get($name)
{
return $this->collection->{$this->method}(function($value) use ($name) {
return $value->$name;
});
}
}

0 comments on commit 8dd57f9

Please sign in to comment.