src/Controller/AppBundle/Helpers/Optional.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Controller\AppBundle\Helpers;
  3. use ArrayAccess;
  4. use ArrayObject;
  5. class Optional implements ArrayAccess
  6. {
  7.     /**
  8.      * The underlying object.
  9.      *
  10.      * @var mixed
  11.      */
  12.     protected $value;
  13.     /**
  14.      * Create a new optional instance.
  15.      *
  16.      * @param mixed $value
  17.      * @return void
  18.      */
  19.     public function __construct($value)
  20.     {
  21.         $this->value $value;
  22.     }
  23.     /**
  24.      * Dynamically access a property on the underlying object.
  25.      *
  26.      * @param string $key
  27.      * @return mixed
  28.      */
  29.     public function __get($key)
  30.     {
  31.         if (is_object($this->value)) {
  32.             return $this->value->{$key} ?? null;
  33.         }
  34.     }
  35.     /**
  36.      * Dynamically check a property exists on the underlying object.
  37.      *
  38.      * @param mixed $name
  39.      * @return bool
  40.      */
  41.     public function __isset($name)
  42.     {
  43.         if (is_object($this->value)) {
  44.             return isset($this->value->{$name});
  45.         }
  46.         if (is_array($this->value) || $this->value instanceof ArrayObject) {
  47.             return isset($this->value[$name]);
  48.         }
  49.         return false;
  50.     }
  51.     /**
  52.      * Determine if an item exists at an offset.
  53.      *
  54.      * @param mixed $key
  55.      * @return bool
  56.      */
  57.     public function offsetExists($key)
  58.     {
  59.         return $this->arrayAccessible() && array_key_exists($key$this->value);
  60.     }
  61.     /**
  62.      * Get an item at a given offset.
  63.      *
  64.      * @param mixed $key
  65.      * @return mixed
  66.      */
  67.     public function offsetGet($key)
  68.     {
  69.         return $this->offsetExists($key)
  70.             ? $this->value[$key]
  71.             : null;
  72.     }
  73.     /**
  74.      * Set the item at a given offset.
  75.      *
  76.      * @param mixed $key
  77.      * @param mixed $value
  78.      * @return void
  79.      */
  80.     public function offsetSet($key$value)
  81.     {
  82.         if ($this->arrayAccessible()) {
  83.             $this->value[$key] = $value;
  84.         }
  85.     }
  86.     /**
  87.      * Unset the item at a given offset.
  88.      *
  89.      * @param string $key
  90.      * @return void
  91.      */
  92.     public function offsetUnset($key)
  93.     {
  94.         if ($this->arrayAccessible()) {
  95.             unset($this->value[$key]);
  96.         }
  97.     }
  98.     /**
  99.      * Dynamically pass a method to the underlying object.
  100.      *
  101.      * @param string $method
  102.      * @param array $parameters
  103.      * @return mixed
  104.      */
  105.     public function __call($method$parameters)
  106.     {
  107.         if (is_object($this->value)) {
  108.             return $this->value->{$method}(...$parameters);
  109.         }
  110.     }
  111.     protected function arrayAccessible()
  112.     {
  113.         return (is_array($this->value) || $this->value instanceof ArrayAccess);
  114.     }
  115. }