vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Teaser/TeaserManager.php line 40

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\Bundle\PageBundle\Teaser;
  11. use Sulu\Bundle\PageBundle\Teaser\Provider\TeaserProviderPoolInterface;
  12. /**
  13.  * Provides functionality for interacting with teasers.
  14.  */
  15. class TeaserManager implements TeaserManagerInterface
  16. {
  17.     /**
  18.      * @var TeaserProviderPoolInterface
  19.      */
  20.     private $providerPool;
  21.     public function __construct(TeaserProviderPoolInterface $providerPool)
  22.     {
  23.         $this->providerPool $providerPool;
  24.     }
  25.     public function find(array $items$locale)
  26.     {
  27.         if (=== \count($items)) {
  28.             return [];
  29.         }
  30.         $result = [];
  31.         list($sortedIds$positions) = $this->sortItems($items);
  32.         foreach ($sortedIds as $type => $typeIds) {
  33.             $teasers $this->providerPool->getProvider($type)->find($typeIds$locale);
  34.             $result $this->sortTeasers($teasers$result$positions$items);
  35.         }
  36.         \ksort($result);
  37.         return \array_values($result);
  38.     }
  39.     /**
  40.      * Returns sorted teaser by given position array.
  41.      *
  42.      * @param Teaser[] $teasers
  43.      *
  44.      * @return array
  45.      */
  46.     private function sortTeasers(array $teasers, array $result, array $positions, array $items)
  47.     {
  48.         foreach ($teasers as $teaser) {
  49.             $index $positions[\sprintf('%s;%s'$teaser->getType(), $teaser->getId())];
  50.             $result[$index] = $teaser;
  51.             $item $items[$index];
  52.             if (['type''id'] !== \array_keys($item)) {
  53.                 $result[$index] = $result[$index]->merge($item);
  54.             }
  55.         }
  56.         return $result;
  57.     }
  58.     /**
  59.      * Returns items sorted by type.
  60.      *
  61.      * @param array $items
  62.      *
  63.      * @return array
  64.      */
  65.     private function sortItems($items)
  66.     {
  67.         $ids = [];
  68.         $positions = [];
  69.         $index 0;
  70.         foreach ($items as $item) {
  71.             if (!\array_key_exists($item['type'], $ids)) {
  72.                 $ids[$item['type']] = [];
  73.             }
  74.             $ids[$item['type']][] = $item['id'];
  75.             $positions[\sprintf('%s;%s'$item['type'], $item['id'])] = $index++;
  76.         }
  77.         return [$ids$positions];
  78.     }
  79. }