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

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\Section\SectionProperty;
  18. use Sulu\Component\Content\Compat\StructureInterface;
  19. use Sulu\Component\Content\Mapper\Translation\TranslatedProperty;
  20. use Sulu\Component\Content\Metadata\BlockMetadata;
  21. use Sulu\Component\Content\Metadata\ComponentMetadata;
  22. use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
  23. use Sulu\Component\Content\Metadata\ItemMetadata;
  24. use Sulu\Component\Content\Metadata\PropertyMetadata;
  25. use Sulu\Component\Content\Metadata\SectionMetadata;
  26. use Sulu\Component\Content\Metadata\StructureMetadata;
  27. use Sulu\Component\DocumentManager\NamespaceRegistry;
  28. /**
  29. * Creates legacy properties from "new" properties.
  30. *
  31. * @deprecated
  32. */
  33. class LegacyPropertyFactory
  34. {
  35. public function __construct(
  36. private NamespaceRegistry $namespaceRegistry,
  37. private ?StructureMetadataFactoryInterface $structureFactory = null
  38. ) {
  39. if (!$this->structureFactory) {
  40. @trigger_deprecation(
  41. 'sulu/sulu',
  42. '2.6',
  43. \sprintf(
  44. 'The usage of the "%s" without the service the "sulu_page.structure.factory" is deprecated and will not longer work in Sulu 3.0.',
  45. self::class
  46. )
  47. );
  48. }
  49. }
  50. /**
  51. * Create a new "translated" property.
  52. *
  53. * @param object $property
  54. * @param string $locale
  55. *
  56. * @return PropertyInterface
  57. */
  58. public function createTranslatedProperty($property, $locale, ?StructureInterface $structure = null)
  59. {
  60. if ($property instanceof ItemMetadata) {
  61. $property = $this->createProperty($property, $structure);
  62. }
  63. return new TranslatedProperty(
  64. $property,
  65. $locale,
  66. $this->namespaceRegistry->getPrefix('content_localized')
  67. );
  68. }
  69. /**
  70. * Create a new property.
  71. *
  72. * @return PropertyInterface $property
  73. */
  74. public function createProperty(ItemMetadata $property, ?StructureInterface $structure = null)
  75. {
  76. if ($property instanceof SectionMetadata) {
  77. return $this->createSectionProperty($property, $structure);
  78. }
  79. if ($property instanceof BlockMetadata) {
  80. return $this->createBlockProperty($property, $structure);
  81. }
  82. if (!$property instanceof PropertyMetadata) {
  83. throw new \RuntimeException(\sprintf(
  84. 'Property needs to be of type [%s].',
  85. \implode(', ', [
  86. PropertyMetadata::class,
  87. BlockMetadata::class,
  88. SectionMetadata::class,
  89. ])
  90. ));
  91. }
  92. if (null === $property->getType()) {
  93. throw new \RuntimeException(\sprintf(
  94. 'Property name "%s" has no type.',
  95. $property->getName()
  96. ));
  97. }
  98. $parameters = $this->convertArrayToParameters($property->getParameters());
  99. $propertyBridge = new LegacyProperty(
  100. $property->getName(),
  101. [
  102. 'title' => $property->getTitles(),
  103. 'info_text' => $property->getDescriptions(),
  104. 'placeholder' => $property->getPlaceholders(),
  105. ],
  106. $property->getType(),
  107. $property->isRequired(),
  108. $property->isLocalized(),
  109. $property->getMaxOccurs(),
  110. $property->getMinOccurs(),
  111. $parameters,
  112. [],
  113. $property->getColSpan(),
  114. $property->getDefaultComponentName()
  115. );
  116. foreach ($property->getTags() as $tag) {
  117. $propertyBridge->addTag(new PropertyTag($tag['name'], $tag['priority'], $tag['attributes']));
  118. }
  119. foreach ($property->getComponents() as $component) {
  120. if ($component->hasTag('sulu.global_block')) {
  121. $propertyBridge->addType($this->createReferenceType($component, $structure));
  122. continue;
  123. }
  124. $propertyBridge->addType($this->createType($component, $structure));
  125. }
  126. $propertyBridge->setStructure($structure);
  127. return $propertyBridge;
  128. }
  129. private function convertArrayToParameters($arrayParams)
  130. {
  131. $parameters = [];
  132. foreach ($arrayParams as $arrayParam) {
  133. $value = $arrayParam['value'];
  134. if (\is_array($value)) {
  135. $value = $this->convertArrayToParameters($value);
  136. }
  137. $parameters[$arrayParam['name']] = new PropertyParameter($arrayParam['name'], $value, $arrayParam['type'], $arrayParam['meta']);
  138. }
  139. return $parameters;
  140. }
  141. private function createSectionProperty(SectionMetadata $property, ?StructureInterface $structure = null)
  142. {
  143. $sectionProperty = new SectionProperty(
  144. $property->getName(),
  145. [
  146. 'title' => $property->getTitles(),
  147. 'info_text' => $property->getDescriptions(),
  148. ],
  149. $property->getColSpan()
  150. );
  151. foreach ($property->getChildren() as $child) {
  152. $sectionProperty->addChild($this->createProperty($child, $structure));
  153. }
  154. return $sectionProperty;
  155. }
  156. private function createBlockProperty(BlockMetadata $property, ?StructureInterface $structure = null)
  157. {
  158. $blockProperty = new BlockProperty(
  159. $property->getName(),
  160. [
  161. 'title' => $property->getTitles(),
  162. 'info_text' => $property->getDescriptions(),
  163. ],
  164. $property->getDefaultComponentName(),
  165. $property->isRequired(),
  166. $property->isLocalized(),
  167. $property->getMaxOccurs(),
  168. $property->getMinOccurs(),
  169. $property->getParameters(),
  170. [],
  171. $property->getColSpan()
  172. );
  173. $blockProperty->setStructure($structure);
  174. foreach ($property->getComponents() as $component) {
  175. if ($component->hasTag('sulu.global_block')) {
  176. $blockProperty->addType($this->createReferenceType($component, $structure));
  177. continue;
  178. }
  179. $blockProperty->addType($this->createType($component, $structure));
  180. }
  181. return $blockProperty;
  182. }
  183. private function createReferenceType(ComponentMetadata $component, ?StructureInterface $structure = null): BlockPropertyType
  184. {
  185. if (!$this->structureFactory) {
  186. throw new \RuntimeException('The required service "sulu_page.structure.factory" was not injected.');
  187. }
  188. /** @var StructureMetadata $structureMetadata */
  189. $structureMetadata = $this->structureFactory->getStructureMetadata('block', $component->getName());
  190. if (!$structureMetadata) {
  191. throw new \InvalidArgumentException(
  192. \sprintf('Global block with name "%s" was not found!', $component->getName())
  193. );
  194. }
  195. if ($structure) {
  196. $propertyType = new BlockPropertyType(
  197. $component->getName(),
  198. [
  199. 'title' => $structureMetadata->getTitle($structure->getLanguageCode()),
  200. 'info_text' => $structureMetadata->getDescription($structure->getLanguageCode()),
  201. ]
  202. );
  203. } else {
  204. $propertyType = new BlockPropertyType($component->getName(), []);
  205. }
  206. foreach ($structureMetadata->getProperties() as $property) {
  207. $propertyType->addChild($this->createProperty($property, $structure));
  208. }
  209. return $propertyType;
  210. }
  211. public function createType(ComponentMetadata $component, ?StructureInterface $structure = null): BlockPropertyType
  212. {
  213. $propertyType = new BlockPropertyType(
  214. $component->getName(),
  215. [
  216. 'title' => $component->getTitles(),
  217. 'info_text' => $component->getDescriptions(),
  218. ]
  219. );
  220. foreach ($component->getChildren() as $property) {
  221. $propertyType->addChild($this->createProperty($property, $structure));
  222. }
  223. return $propertyType;
  224. }
  225. }