Skip to content

Instantly share code, notes, and snippets.

@webinfinita
Last active August 29, 2015 14:17
Show Gist options
  • Save webinfinita/f6cc55f4ee8e175274c6 to your computer and use it in GitHub Desktop.
Save webinfinita/f6cc55f4ee8e175274c6 to your computer and use it in GitHub Desktop.
View Presenters for Laravel 5
<?php namespace App\Presenters;
trait PresentableTrait {
protected $presenterInstance;
public function present()
{
if (! $this->presenter)
{
throw new \Exception('Falta la propiedad presenter en la Entidad/Modelo');
}
if (! class_exists($this->presenter))
{
throw new \Exception("La clase '{$this->presenter}' no existe");
}
if ( ! $this->presenterInstance)
{
$this->presenterInstance = new $this->presenter($this);
}
return $this->presenterInstance;
}
}
<?php namespace App\Presenters;
abstract class Presenter {
protected $entity;
public function __construct($entity)
{
$this->entity = $entity;
}
public function __get($property)
{
if (method_exists($this, $property))
{
return $this->{$property}();
}
return $this->entity->{$property};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment