TheCelavi

Abstract Builder

Nov 16th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class AbstractBuilder
  4. {
  5.     protected $data;
  6.  
  7.     public function __construct()
  8.     {
  9.         $this->data = $this->configureParameters();
  10.     }
  11.  
  12.     public function build()
  13.     {
  14.         $reflector = new \ReflectionClass($this->getObjectFqcn());
  15.         return $reflector->newInstanceArgs(array_values($this->data));
  16.     }
  17.  
  18.     public function setValues(array $values)
  19.     {
  20.         foreach ($values as $key => $value) {
  21.             $this->{$key} = $value;
  22.         }
  23.  
  24.         return $this;
  25.     }
  26.  
  27.     public function __set($name, $value)
  28.     {
  29.         if (!array_key_exists($name, $this->data)) {
  30.             throw new \InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
  31.         }
  32.  
  33.         $this->data[$name] = $value;
  34.     }
  35.  
  36.     public function __get($name)
  37.     {
  38.         if (!array_key_exists($name, $this->data)) {
  39.             throw new \InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
  40.         }
  41.  
  42.         return $this->data[$name];
  43.     }
  44.  
  45.     public function __isset($name)
  46.     {
  47.         return array_key_exists($name, $this->data);
  48.     }
  49.  
  50.     public function __call($name, array $arguments)
  51.     {
  52.         $property = lcfirst(substr($name, 3));
  53.  
  54.         if (strpos($name, 'set') !== 0 || !array_key_exists($property, $this->data)) {
  55.             throw new \BadMethodCallException(sprintf('Unknown method "%s" in "%s".', $name, get_class($this)));
  56.         }
  57.  
  58.         if (count($arguments) !== 1) {
  59.             throw new \BadMethodCallException(sprintf('Method "%s" in "%s" expects exactly one parameter.', $name, get_class($this)));
  60.         }
  61.  
  62.         $this->data[$property] = $arguments[0];
  63.  
  64.         return $this;
  65.     }
  66.  
  67.     public static function createBuilder()
  68.     {
  69.         return new static();
  70.     }
  71.  
  72.     /**
  73.      * Configure builder parameters that will be passed to building class constructor.
  74.      *
  75.      * @return array
  76.      */
  77.     protected abstract function configureParameters();
  78.  
  79.     /**
  80.      * Get full qualified class name of class that instance ought to be constructed.
  81.      *
  82.      * @return string
  83.      */
  84.     protected abstract function getObjectFqcn();
  85. }
  86.  
  87.  
  88. // EXAMPLE - creating a builder that builds instance of "Response" class
  89.  
  90.  
  91. /**
  92.  * Class ResponseBuilder
  93.  *
  94.  * @method setId($id)
  95.  * @method setToken($token)
  96.  * @method setResponseCode($responseCode)
  97.  * @method setResponseAction($responseAction)
  98.  * @method setErrorReason($errorReason)
  99.  * @method setTimestamp($timestamp)
  100.  *
  101.  * @package Syntech\Swarm\Handler\Apns\Builder
  102.  */
  103. final class ResponseBuilder extends AbstractBuilder
  104. {
  105.     /**
  106.      * Get builded response.
  107.      *
  108.      * @return Response
  109.      */
  110.     public function getResponse()
  111.     {
  112.         return $this->build();
  113.     }
  114.  
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     protected function configureParameters()
  119.     {
  120.         return array(
  121.             'id' => null,
  122.             'token' => null,
  123.             'responseCode' => null,
  124.             'responseAction' => null,
  125.             'errorReason' => null,
  126.             'timestamp' => null
  127.         );
  128.     }
  129.  
  130.     /**
  131.      * {@inheritdoc}
  132.      */
  133.     protected function getObjectFqcn()
  134.     {
  135.         return Response::class;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment