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

  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.      * @param array|PropertyValue|null $value
  70.      */
  71.     public function doSetValue($value)
  72.     {
  73.         $items $value;
  74.         if ($value instanceof PropertyValue) {
  75.             $items $value->getValue();
  76.         }
  77.         if (null == $items) {
  78.             return;
  79.         }
  80.         if (!\is_array($items)) {
  81.             throw new \InvalidArgumentException(
  82.                 \sprintf('Expected block configuration but got "%s" at property: "%s"'\get_debug_type($items), $this->getName())
  83.             );
  84.         }
  85.         // check value for single value
  86.         if (\array_keys($items) !== \range(0\count($items) - 1)) {
  87.             $items = [$items];
  88.         }
  89.         $this->properties = [];
  90.         for ($i 0$i \count($items); ++$i) {
  91.             $item $items[$i];
  92.             $type $this->initProperties($i$item['type']);
  93.             if (isset($item['settings'])) {
  94.                 $type->setSettings($item['settings']);
  95.             }
  96.             /** @var PropertyInterface $subProperty */
  97.             foreach ($type->getChildProperties() as $subProperty) {
  98.                 if (!isset($item[$subProperty->getName()])) {
  99.                     continue;
  100.                 }
  101.                 $subName $subProperty->getName();
  102.                 $subValue $item[$subName];
  103.                 if ($subValue instanceof PropertyValue) {
  104.                     $subValueProperty = new PropertyValue($subName$subValue);
  105.                     $subProperty->setPropertyValue($subValueProperty);
  106.                     $item[$subName] = $subValueProperty;
  107.                 } else {
  108.                     $subProperty->setValue($subValue);
  109.                 }
  110.             }
  111.             $items[$i] = $item;
  112.         }
  113.         if ($value instanceof PropertyValue) {
  114.             $value->setValue($items);
  115.         }
  116.     }
  117.     /**
  118.      * get value of sub properties.
  119.      *
  120.      * @return array|mixed
  121.      */
  122.     public function getValue()
  123.     {
  124.         // if size of children smaller than minimum
  125.         if (\count($this->properties) < $this->getMinOccurs()) {
  126.             for ($i \count($this->properties); $i $this->getMinOccurs(); ++$i) {
  127.                 $this->initProperties($i$this->getDefaultTypeName());
  128.             }
  129.         }
  130.         $data = [];
  131.         foreach ($this->properties as $type) {
  132.             $result = [
  133.                 'type' => $type->getName(),
  134.                 'settings' => $type->getSettings(),
  135.             ];
  136.             foreach ($type->getChildProperties() as $property) {
  137.                 $result[$property->getName()] = $property->getValue();
  138.             }
  139.             $data[] = $result;
  140.         }
  141.         return $data;
  142.     }
  143.     /**
  144.      * returns TRUE if property is a block.
  145.      *
  146.      * @return bool
  147.      */
  148.     public function getIsBlock()
  149.     {
  150.         return true;
  151.     }
  152.     public function getIsMultiple()
  153.     {
  154.         if (\is_null($this->getMinOccurs()) || \is_null($this->getMaxOccurs())) {
  155.             // in contrast to properties blocks are multiple by default
  156.             return true;
  157.         }
  158.         return parent::getIsMultiple();
  159.     }
  160.     /**
  161.      * returns child properties of given Type.
  162.      *
  163.      * @param string $typeName
  164.      *
  165.      * @return PropertyInterface[]
  166.      */
  167.     public function getChildProperties($typeName)
  168.     {
  169.         return $this->getTypeChildProperties($typeName);
  170.     }
  171. }