vendor/sulu/sulu/src/Sulu/Component/Content/Document/Structure/Structure.php line 73

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\Content\Document\Structure;
  11. /**
  12.  * Lazy loading container for content properties.
  13.  */
  14. class Structure implements StructureInterface
  15. {
  16.     /**
  17.      * @var array
  18.      */
  19.     protected $properties = [];
  20.     /**
  21.      * @var array
  22.      */
  23.     protected $stagedData = [];
  24.     public function getStagedData()
  25.     {
  26.         return $this->stagedData;
  27.     }
  28.     public function setStagedData(array $stagedData)
  29.     {
  30.         $this->stagedData $stagedData;
  31.     }
  32.     public function commitStagedData($clearMissingContent)
  33.     {
  34.         $this->bind($this->stagedData$clearMissingContent);
  35.         $this->stagedData = [];
  36.     }
  37.     public function getProperty($name)
  38.     {
  39.         if (isset($this->properties[$name])) {
  40.             return $this->properties[$name];
  41.         }
  42.         $property = new PropertyValue($name);
  43.         $this->properties[$name] = $property;
  44.         return $property;
  45.     }
  46.     public function getContentViewProperty($name)
  47.     {
  48.         throw new \Exception('Cannot retrieve content view property for non-managed property');
  49.     }
  50.     public function hasProperty($name)
  51.     {
  52.         return $this->offsetExists($name);
  53.     }
  54.     #[\ReturnTypeWillChange]
  55.     public function offsetExists($offset)
  56.     {
  57.         return isset($this->properties[$offset]);
  58.     }
  59.     public function offsetGet($offset)
  60.     {
  61.         return $this->getProperty($offset);
  62.     }
  63.     #[\ReturnTypeWillChange]
  64.     public function offsetSet($offset$value)
  65.     {
  66.         $this->getProperty($offset)->setValue($value);
  67.     }
  68.     #[\ReturnTypeWillChange]
  69.     public function offsetUnset($offset)
  70.     {
  71.         unset($this->properties[$offset]);
  72.     }
  73.     public function toArray()
  74.     {
  75.         $values = [];
  76.         foreach ($this->properties as $name => $property) {
  77.             $values[$name] = $this->normalize($property->getValue());
  78.         }
  79.         return $values;
  80.     }
  81.     public function bind($data$clearMissing false)
  82.     {
  83.         foreach ($data as $key => $value) {
  84.             $property $this->getProperty($key);
  85.             $property->setValue($value);
  86.         }
  87.     }
  88.     public function __get($name)
  89.     {
  90.         return $this->offsetGet($name);
  91.     }
  92.     public function __set($name$value)
  93.     {
  94.         $this->offsetSet($name$value);
  95.     }
  96.     protected function normalize($value)
  97.     {
  98.         if ($value instanceof PropertyValue) {
  99.             $value $value->getValue();
  100.         }
  101.         if (!\is_array($value)) {
  102.             return $value;
  103.         }
  104.         $ret = [];
  105.         foreach ($value as $key => $value) {
  106.             $ret[$key] = $this->normalize($value);
  107.         }
  108.         return $ret;
  109.     }
  110. }