vendor/shopware/core/Content/Cms/Aggregate/CmsSlot/CmsSlotCollection.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Cms\Aggregate\CmsSlot;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  4. /**
  5.  * @method CmsSlotEntity[]    getIterator()
  6.  * @method CmsSlotEntity[]    getElements()
  7.  * @method CmsSlotEntity|null get(string $key)
  8.  * @method CmsSlotEntity|null first()
  9.  * @method CmsSlotEntity|null last()
  10.  */
  11. class CmsSlotCollection extends EntityCollection
  12. {
  13.     /**
  14.      * @var CmsSlotEntity[]|null indexed by slot name
  15.      */
  16.     private $slotCache;
  17.     /**
  18.      * @param string        $key
  19.      * @param CmsSlotEntity $entity
  20.      */
  21.     public function set($key$entity): void
  22.     {
  23.         parent::set($key$entity);
  24.         $this->slotCache[$entity->getSlot()] = $entity;
  25.     }
  26.     /**
  27.      * @param CmsSlotEntity $entity
  28.      */
  29.     public function add($entity): void
  30.     {
  31.         parent::add($entity);
  32.         $this->slotCache[$entity->getSlot()] = $entity;
  33.     }
  34.     public function getSlot(string $slot): ?CmsSlotEntity
  35.     {
  36.         $this->createSlotHashMap();
  37.         return $this->slotCache[$slot] ?? null;
  38.     }
  39.     public function getApiAlias(): string
  40.     {
  41.         return 'cms_page_slot_collection';
  42.     }
  43.     protected function getExpectedClass(): string
  44.     {
  45.         return CmsSlotEntity::class;
  46.     }
  47.     private function createSlotHashMap(): void
  48.     {
  49.         if ($this->slotCache !== null) {
  50.             return;
  51.         }
  52.         foreach ($this->getIterator() as $element) {
  53.             $this->slotCache[$element->getSlot()] = $element;
  54.         }
  55.     }
  56. }