Last active
August 29, 2015 14:17
-
-
Save webinfinita/f6cc55f4ee8e175274c6 to your computer and use it in GitHub Desktop.
View Presenters for Laravel 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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