Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Trait DecoratorTrait
- *
- * Have to implemented:
- * @method getDecoratedObject()
- * @static $decoratedClass
- */
- trait DecoratorTrait
- {
- /**
- * @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;
- }
- /**
- * @param $method
- * @param $args
- * @return mixed
- * @throws \Exception
- */
- public static function __callStatic($method, $args) {
- try {
- return forward_static_call_array([static::$decoratedClass, $method], $args);
- } catch (\Throwable $e) {
- throw new \Exception(
- sprintf("Call undefined static method: %s::%s", static::$decoratedClass, $method));
- }
- }
- }
Add Comment
Please, Sign In to add comment