vendor/sulu/sulu/src/Sulu/Component/Content/Compat/Block/BlockProperty.php line 27

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\Compat\Block;
  11. use Sulu\Component\Content\Compat\Property;
  12. use Sulu\Component\Content\Compat\PropertyInterface;
  13. use Sulu\Component\Content\Document\Structure\PropertyValue;
  14. /**
  15.  * Representation of a block node in template xml.
  16.  *
  17.  * @method BlockPropertyType[] getTypes()
  18.  * @method addType(BlockPropertyType $type)
  19.  * @method BlockPropertyType getType(string $name)
  20.  * @method BlockPropertyType getProperties(int $index)
  21.  * @method BlockPropertyType initProperties(int $index, string $typeName)
  22.  */
  23. class BlockProperty extends Property implements BlockPropertyInterface
  24. {
  25.     public function __construct(
  26.         $name,
  27.         $metadata,
  28.         $defaultTypeName,
  29.         $mandatory false,
  30.         $multilingual false,
  31.         $maxOccurs 1,
  32.         $minOccurs 1,
  33.         $params = [],
  34.         $tags = [],
  35.         $col null
  36.     ) {
  37.         parent::__construct(
  38.             $name,
  39.             $metadata,
  40.             'block',
  41.             $mandatory,
  42.             $multilingual,
  43.             $maxOccurs,
  44.             $minOccurs,
  45.             $params,
  46.             $tags,
  47.             $col,
  48.             $defaultTypeName
  49.         );
  50.     }
  51.     public function setValue($value)
  52.     {
  53.         $this->doSetValue($value);
  54.         if ($this->propertyValue) {
  55.             $this->propertyValue->setValue($value);
  56.         }
  57.     }
  58.     public function setPropertyValue(PropertyValue $value)
  59.     {
  60.         parent::setPropertyValue($value);
  61.         $this->doSetValue($value);
  62.     }
  63.     /**
  64.      * Sub properties need to be referenced to the PropertyValue so
  65.      * that the "real" property is updated.
  66.      *
  67.      * TODO: This is very tedious code. It is important to factor this out.
  68.      */
  69.     public function doSetValue($value)
  70.     {
  71.         $items $value;
  72.         if ($value instanceof PropertyValue) {
  73.             $items $value->getValue();
  74.         }
  75.         if (null == $items) {
  76.             return;
  77.         }
  78.         // check value for single value
  79.         if (\array_keys($items) !== \range(0, \count($items) - 1)) {
  80.             $items = [$items];
  81.         }
  82.         $this->properties = [];
  83.         for ($i 0$i < \count($items); ++$i) {
  84.             $item $items[$i];
  85.             $type $this->initProperties($i$item['type']);
  86.             if (isset($item['settings'])) {
  87.                 $type->setSettings($item['settings']);
  88.             }
  89.             /** @var PropertyInterface $subProperty */
  90.             foreach ($type->getChildProperties() as $subProperty) {
  91.                 if (!isset($item[$subProperty->getName()])) {
  92.                     continue;
  93.                 }
  94.                 $subName $subProperty->getName();
  95.                 $subValue $item[$subName];
  96.                 if ($subValue instanceof PropertyValue) {
  97.                     $subValueProperty = new PropertyValue($subName$subValue);
  98.                     $subProperty->setPropertyValue($subValueProperty);
  99.                     $item[$subName] = $subValueProperty;
  100.                 } else {
  101.                     $subProperty->setValue($subValue);
  102.                 }
  103.             }
  104.             $items[$i] = $item;
  105.         }
  106.         if ($value instanceof PropertyValue) {
  107.             $value->setValue($items);
  108.         }
  109.     }
  110.     /**
  111.      * get value of sub properties.
  112.      *
  113.      * @return array|mixed
  114.      */
  115.     public function getValue()
  116.     {
  117.         // if size of children smaller than minimum
  118.         if (\count($this->properties) < $this->getMinOccurs()) {
  119.             for ($i = \count($this->properties); $i $this->getMinOccurs(); ++$i) {
  120.                 $this->initProperties($i$this->getDefaultTypeName());
  121.             }
  122.         }
  123.         $data = [];
  124.         foreach ($this->properties as $type) {
  125.             $result = [
  126.                 'type' => $type->getName(),
  127.                 'settings' => $type->getSettings(),
  128.             ];
  129.             foreach ($type->getChildProperties() as $property) {
  130.                 $result[$property->getName()] = $property->getValue();
  131.             }
  132.             $data[] = $result;
  133.         }
  134.         return $data;
  135.     }
  136.     /**
  137.      * returns TRUE if property is a block.
  138.      *
  139.      * @return bool
  140.      */
  141.     public function getIsBlock()
  142.     {
  143.         return true;
  144.     }
  145.     public function getIsMultiple()
  146.     {
  147.         if (\is_null($this->getMinOccurs()) || \is_null($this->getMaxOccurs())) {
  148.             // in contrast to properties blocks are multiple by default
  149.             return true;
  150.         }
  151.         return parent::getIsMultiple();
  152.     }
  153.     /**
  154.      * returns child properties of given Type.
  155.      *
  156.      * @param string $typeName
  157.      *
  158.      * @return PropertyInterface[]
  159.      */
  160.     public function getChildProperties($typeName)
  161.     {
  162.         return $this->getTypeChildProperties($typeName);
  163.     }
  164. }