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

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