vendor/sulu/sulu/src/Sulu/Component/Content/Compat/Structure/LegacyPropertyFactory.php line 71

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\Structure;
  11. use Sulu\Component\Content\Compat\Block\BlockProperty;
  12. use Sulu\Component\Content\Compat\Block\BlockPropertyType;
  13. use Sulu\Component\Content\Compat\Property as LegacyProperty;
  14. use Sulu\Component\Content\Compat\PropertyInterface;
  15. use Sulu\Component\Content\Compat\PropertyParameter;
  16. use Sulu\Component\Content\Compat\PropertyTag;
  17. use Sulu\Component\Content\Compat\PropertyType;
  18. use Sulu\Component\Content\Compat\Section\SectionProperty;
  19. use Sulu\Component\Content\Compat\StructureInterface;
  20. use Sulu\Component\Content\Mapper\Translation\TranslatedProperty;
  21. use Sulu\Component\Content\Metadata\BlockMetadata;
  22. use Sulu\Component\Content\Metadata\ItemMetadata;
  23. use Sulu\Component\Content\Metadata\PropertyMetadata;
  24. use Sulu\Component\Content\Metadata\SectionMetadata;
  25. use Sulu\Component\DocumentManager\NamespaceRegistry;
  26. /**
  27. * Creates legacy properties from "new" properties.
  28. *
  29. * @deprecated
  30. */
  31. class LegacyPropertyFactory
  32. {
  33. private $namespaceRegistry;
  34. public function __construct(NamespaceRegistry $namespaceRegistry)
  35. {
  36. $this->namespaceRegistry = $namespaceRegistry;
  37. }
  38. /**
  39. * Create a new "translated" property.
  40. *
  41. * @param object $property
  42. * @param string $locale
  43. *
  44. * @return PropertyInterface
  45. */
  46. public function createTranslatedProperty($property, $locale, ?StructureInterface $structure = null)
  47. {
  48. if ($property instanceof ItemMetadata) {
  49. $property = $this->createProperty($property, $structure);
  50. }
  51. $property = new TranslatedProperty(
  52. $property,
  53. $locale,
  54. $this->namespaceRegistry->getPrefix('content_localized')
  55. );
  56. return $property;
  57. }
  58. /**
  59. * Create a new property.
  60. *
  61. * @return PropertyInterface $property
  62. */
  63. public function createProperty(ItemMetadata $property, ?StructureInterface $structure = null)
  64. {
  65. if ($property instanceof SectionMetadata) {
  66. return $this->createSectionProperty($property, $structure);
  67. }
  68. if ($property instanceof BlockMetadata) {
  69. return $this->createBlockProperty($property, $structure);
  70. }
  71. if (!$property instanceof PropertyMetadata) {
  72. throw new \RuntimeException(\sprintf(
  73. 'Property needs to be of type [%s].',
  74. \implode(', ', [
  75. PropertyMetadata::class,
  76. BlockMetadata::class,
  77. SectionMetadata::class,
  78. ])
  79. ));
  80. }
  81. if (null === $property->getType()) {
  82. throw new \RuntimeException(\sprintf(
  83. 'Property name "%s" has no type.',
  84. $property->getName()
  85. ));
  86. }
  87. $parameters = $this->convertArrayToParameters($property->getParameters());
  88. $propertyBridge = new LegacyProperty(
  89. $property->getName(),
  90. [
  91. 'title' => $property->getTitles(),
  92. 'info_text' => $property->getDescriptions(),
  93. 'placeholder' => $property->getPlaceholders(),
  94. ],
  95. $property->getType(),
  96. $property->isRequired(),
  97. $property->isLocalized(),
  98. $property->getMaxOccurs(),
  99. $property->getMinOccurs(),
  100. $parameters,
  101. [],
  102. $property->getColSpan(),
  103. $property->getDefaultComponentName()
  104. );
  105. foreach ($property->getTags() as $tag) {
  106. $propertyBridge->addTag(new PropertyTag($tag['name'], $tag['priority'], $tag['attributes']));
  107. }
  108. foreach ($property->getComponents() as $component) {
  109. $propertyType = new PropertyType(
  110. $component->getName(),
  111. [
  112. 'title' => $component->getTitles(),
  113. 'info_text' => $component->getDescriptions(),
  114. ]
  115. );
  116. foreach ($component->getChildren() as $property) {
  117. $propertyType->addChild($this->createProperty($property, $structure));
  118. }
  119. $propertyBridge->addType($propertyType);
  120. }
  121. $propertyBridge->setStructure($structure);
  122. return $propertyBridge;
  123. }
  124. private function convertArrayToParameters($arrayParams)
  125. {
  126. $parameters = [];
  127. foreach ($arrayParams as $arrayParam) {
  128. $value = $arrayParam['value'];
  129. if (\is_array($value)) {
  130. $value = $this->convertArrayToParameters($value);
  131. }
  132. $parameters[$arrayParam['name']] = new PropertyParameter($arrayParam['name'], $value, $arrayParam['type'], $arrayParam['meta']);
  133. }
  134. return $parameters;
  135. }
  136. private function createSectionProperty(SectionMetadata $property, ?StructureInterface $structure = null)
  137. {
  138. $sectionProperty = new SectionProperty(
  139. $property->getName(),
  140. [
  141. 'title' => $property->getTitles(),
  142. 'info_text' => $property->getDescriptions(),
  143. ],
  144. $property->getColSpan()
  145. );
  146. foreach ($property->getChildren() as $child) {
  147. $sectionProperty->addChild($this->createProperty($child, $structure));
  148. }
  149. return $sectionProperty;
  150. }
  151. private function createBlockProperty(BlockMetadata $property, ?StructureInterface $structure = null)
  152. {
  153. $blockProperty = new BlockProperty(
  154. $property->getName(),
  155. [
  156. 'title' => $property->getTitles(),
  157. 'info_text' => $property->getDescriptions(),
  158. ],
  159. $property->getDefaultComponentName(),
  160. $property->isRequired(),
  161. $property->isLocalized(),
  162. $property->getMaxOccurs(),
  163. $property->getMinOccurs(),
  164. $property->getParameters(),
  165. [],
  166. $property->getColSpan()
  167. );
  168. $blockProperty->setStructure($structure);
  169. foreach ($property->getComponents() as $component) {
  170. $blockPropertyType = new BlockPropertyType(
  171. $component->getName(),
  172. [
  173. 'title' => $component->getTitles(),
  174. 'info_text' => $component->getDescriptions(),
  175. ]
  176. );
  177. foreach ($component->getChildren() as $property) {
  178. $blockPropertyType->addChild($this->createProperty($property, $structure));
  179. }
  180. $blockProperty->addType($blockPropertyType);
  181. }
  182. return $blockProperty;
  183. }
  184. }