vendor/sulu/sulu/src/Sulu/Component/SmartContent/ArrayAccessItem.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\SmartContent;
  11. use JMS\Serializer\Annotation\ExclusionPolicy;
  12. use JMS\Serializer\Annotation\VirtualProperty;
  13. use Sulu\Component\SmartContent\Exception\NoSuchPropertyException;
  14. use Sulu\Component\SmartContent\Exception\NotSupportedException;
  15. /**
  16.  * Base class for DataProvider items.
  17.  *
  18.  * @ExclusionPolicy("all")
  19.  */
  20. class ArrayAccessItem implements ResourceItemInterface\ArrayAccess\JsonSerializable
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     private $id;
  26.     /**
  27.      * @var array
  28.      */
  29.     private $data = [];
  30.     /**
  31.      * @var object
  32.      */
  33.     private $resource;
  34.     /**
  35.      * @param object $resource
  36.      */
  37.     public function __construct($id, array $data$resource)
  38.     {
  39.         $this->id $id;
  40.         $this->data $data;
  41.         $this->resource $resource;
  42.     }
  43.     public function getResource()
  44.     {
  45.         return $this->resource;
  46.     }
  47.     /**
  48.      * @VirtualProperty()
  49.      */
  50.     public function getId()
  51.     {
  52.         return $this->id;
  53.     }
  54.     /**
  55.      * Returns TRUE if data array contains given key.
  56.      *
  57.      * @param string $key
  58.      *
  59.      * @return bool
  60.      */
  61.     protected function exists($key)
  62.     {
  63.         return \array_key_exists($key$this->data);
  64.     }
  65.     /**
  66.      * Returns value with given key.
  67.      *
  68.      * @param string $key
  69.      *
  70.      * @throws NoSuchPropertyException
  71.      */
  72.     protected function get($key)
  73.     {
  74.         if (!$this->exists($key)) {
  75.             throw new NoSuchPropertyException();
  76.         }
  77.         return $this->data[$key];
  78.     }
  79.     public function __get($name)
  80.     {
  81.         return $this->get($name);
  82.     }
  83.     #[\ReturnTypeWillChange]
  84.     public function offsetExists($offset)
  85.     {
  86.         return $this->exists($offset);
  87.     }
  88.     public function offsetGet($offset)
  89.     {
  90.         return $this->get($offset);
  91.     }
  92.     #[\ReturnTypeWillChange]
  93.     public function offsetSet($offset$value)
  94.     {
  95.         throw new NotSupportedException();
  96.     }
  97.     #[\ReturnTypeWillChange]
  98.     public function offsetUnset($offset)
  99.     {
  100.         throw new NotSupportedException();
  101.     }
  102.     #[\ReturnTypeWillChange]
  103.     public function jsonSerialize()
  104.     {
  105.         return $this->data;
  106.     }
  107. }