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. * @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. foreach ($type->getChildProperties() as $subProperty) {
  97. if (!isset($item[$subProperty->getName()])) {
  98. continue;
  99. }
  100. $subName = $subProperty->getName();
  101. $subValue = $item[$subName];
  102. if ($subValue instanceof PropertyValue) {
  103. $subValueProperty = new PropertyValue($subName, $subValue);
  104. $subProperty->setPropertyValue($subValueProperty);
  105. $item[$subName] = $subValueProperty;
  106. } else {
  107. $subProperty->setValue($subValue);
  108. }
  109. }
  110. $items[$i] = $item;
  111. }
  112. if ($value instanceof PropertyValue) {
  113. $value->setValue($items);
  114. }
  115. }
  116. /**
  117. * get value of sub properties.
  118. *
  119. * @return array|mixed
  120. */
  121. public function getValue()
  122. {
  123. // if size of children smaller than minimum
  124. if (\count($this->properties) < $this->getMinOccurs()) {
  125. for ($i = \count($this->properties); $i < $this->getMinOccurs(); ++$i) {
  126. $this->initProperties($i, $this->getDefaultTypeName());
  127. }
  128. }
  129. $data = [];
  130. foreach ($this->properties as $type) {
  131. $result = [
  132. 'type' => $type->getName(),
  133. 'settings' => $type->getSettings(),
  134. ];
  135. foreach ($type->getChildProperties() as $property) {
  136. $result[$property->getName()] = $property->getValue();
  137. }
  138. $data[] = $result;
  139. }
  140. return $data;
  141. }
  142. /**
  143. * returns TRUE if property is a block.
  144. *
  145. * @return bool
  146. */
  147. public function getIsBlock()
  148. {
  149. return true;
  150. }
  151. public function getIsMultiple()
  152. {
  153. if (\is_null($this->getMinOccurs()) || \is_null($this->getMaxOccurs())) {
  154. // in contrast to properties blocks are multiple by default
  155. return true;
  156. }
  157. return parent::getIsMultiple();
  158. }
  159. /**
  160. * returns child properties of given Type.
  161. *
  162. * @param string $typeName
  163. *
  164. * @return PropertyInterface[]
  165. */
  166. public function getChildProperties($typeName)
  167. {
  168. return $this->getTypeChildProperties($typeName);
  169. }
  170. }