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