Last active
December 7, 2017 09:19
-
-
Save shov/85919ad65a742a1e45f62b553e15c046 to your computer and use it in GitHub Desktop.
PHP Implementation of the decorator as the Trait
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 | |
/** | |
* Trait Decorator | |
* | |
* Required on host | |
* @method getDecoratedObject() | |
*/ | |
trait Decorator | |
{ | |
/** | |
* @param $method | |
* @param $args | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function __call($method, $args) | |
{ | |
if (is_callable([$this->getDecoratedObject(), $method])) { | |
return call_user_func_array([$this->getDecoratedObject(), $method], $args); | |
} | |
throw new \Exception( | |
sprintf("Call undefined method: %s::%s", get_class($this->getDecoratedObject()), $method)); | |
} | |
/** | |
* @param $property | |
* @return null | |
*/ | |
public function __get($property) | |
{ | |
$publicVars = get_object_vars($this->getDecoratedObject()); | |
if (array_key_exists($property, $publicVars)) { | |
return $this->getDecoratedObject()->$property; | |
} | |
return null; | |
} | |
/** | |
* @param $property | |
* @param $value | |
* @return $this | |
*/ | |
public function __set($property, $value) | |
{ | |
$this->getDecoratedObject()->$property = $value; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment