Skip to content

Instantly share code, notes, and snippets.

@adamwathan
Last active December 27, 2015 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwathan/7312671 to your computer and use it in GitHub Desktop.
Save adamwathan/7312671 to your computer and use it in GitHub Desktop.
<?php namespace Acme\Presenters;
use Illuminate\Support\Collection;
abstract class Presenter {
protected $resource;
public function __construct($resource)
{
$this->resource = $resource;
}
public static function presentCollection($collection)
{
$presentedCollection = new Collection;
foreach($collection as $key => $value) {
$presentedCollection->put($key, new static($value));
}
return $presentedCollection;
}
public function __get($name)
{
if (method_exists($this, $name))
{
return $this->{$name}();
}
return $this->resource->{$name};
}
}
// Back in the controller...
$users = User::all();
return View::make('users')->with('users', UserPresenter::presentCollection($users));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment