vendor/sulu/headless-bundle/Content/ContentTypeResolver/BlockResolver.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Sulu.
  5.  *
  6.  * (c) Sulu GmbH
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver;
  12. use Sulu\Bundle\HeadlessBundle\Content\ContentResolverInterface;
  13. use Sulu\Bundle\HeadlessBundle\Content\ContentView;
  14. use Sulu\Component\Content\Compat\Block\BlockPropertyInterface;
  15. use Sulu\Component\Content\Compat\PropertyInterface;
  16. use Sulu\Component\Content\Types\Block\BlockVisitorInterface;
  17. class BlockResolver implements ContentTypeResolverInterface
  18. {
  19.     public static function getContentType(): string
  20.     {
  21.         return 'block';
  22.     }
  23.     /**
  24.      * @var ContentResolverInterface
  25.      */
  26.     private $contentResolver;
  27.     /**
  28.      * @var \Traversable<BlockVisitorInterface>
  29.      */
  30.     private $blockVisitors;
  31.     /**
  32.      * @param \Traversable<BlockVisitorInterface> $blockVisitors
  33.      */
  34.     public function __construct(ContentResolverInterface $contentResolver\Traversable $blockVisitors)
  35.     {
  36.         $this->contentResolver $contentResolver;
  37.         $this->blockVisitors $blockVisitors;
  38.     }
  39.     /**
  40.      * @param BlockPropertyInterface $property
  41.      */
  42.     public function resolve($dataPropertyInterface $propertystring $locale, array $attributes = []): ContentView
  43.     {
  44.         $blockPropertyTypes = [];
  45.         for ($i 0$i $property->getLength(); ++$i) {
  46.             $blockPropertyType $property->getProperties($i);
  47.             foreach ($this->blockVisitors as $blockVisitor) {
  48.                 $blockPropertyType $blockVisitor->visit($blockPropertyType);
  49.                 if (!$blockPropertyType) {
  50.                     break;
  51.                 }
  52.             }
  53.             if ($blockPropertyType) {
  54.                 $blockPropertyTypes[] = $blockPropertyType;
  55.             }
  56.         }
  57.         $content = [];
  58.         $view = [];
  59.         foreach ($blockPropertyTypes as $i => $blockPropertyType) {
  60.             $content[$i] = ['type' => $blockPropertyType->getName(), 'settings' => $blockPropertyType->getSettings()];
  61.             $view[$i] = [];
  62.             foreach ($blockPropertyType->getChildProperties() as $childProperty) {
  63.                 $contentView $this->contentResolver->resolve($childProperty->getValue(), $childProperty$locale$attributes);
  64.                 $content[$i][$childProperty->getName()] = $contentView->getContent();
  65.                 $view[$i][$childProperty->getName()] = $contentView->getView();
  66.             }
  67.         }
  68.         return new ContentView($content$view);
  69.     }
  70. }