vendor/shopware/core/Framework/DataAbstractionLayer/EntityCollection.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\Framework\Struct\Collection;
  5. /**
  6.  * @template TElement of Entity
  7.  *
  8.  * @extends Collection<TElement>
  9.  */
  10. #[Package('core')]
  11. class EntityCollection extends Collection
  12. {
  13.     public function __construct(iterable $elements = [])
  14.     {
  15.         parent::__construct([]);
  16.         foreach ($elements as $element) {
  17.             $this->validateType($element);
  18.             $this->set($element->getUniqueIdentifier(), $element);
  19.         }
  20.     }
  21.     public function fill(array $entities): void
  22.     {
  23.         array_map([$this'add'], $entities);
  24.     }
  25.     /**
  26.      * @param TElement $entity
  27.      */
  28.     public function add($entity): void
  29.     {
  30.         $this->set($entity->getUniqueIdentifier(), $entity);
  31.     }
  32.     /**
  33.      * @return list<string>
  34.      */
  35.     public function getIds(): array
  36.     {
  37.         return $this->fmap(static function (Entity $entity) {
  38.             return $entity->getUniqueIdentifier();
  39.         });
  40.     }
  41.     public function filterByProperty(string $property$value)
  42.     {
  43.         return $this->filter(
  44.             static function (Entity $struct) use ($property$value) {
  45.                 return $struct->get($property) === $value;
  46.             }
  47.         );
  48.     }
  49.     public function filterAndReduceByProperty(string $property$value)
  50.     {
  51.         $filtered = [];
  52.         foreach ($this->getIterator() as $key => $struct) {
  53.             if ($struct->get($property) !== $value) {
  54.                 continue;
  55.             }
  56.             $filtered[] = $struct;
  57.             $this->remove($key);
  58.         }
  59.         return $this->createNew($filtered);
  60.     }
  61.     /**
  62.      * @param EntityCollection<TElement> $collection
  63.      */
  64.     public function merge(self $collection): void
  65.     {
  66.         /** @var TElement $entity */
  67.         foreach ($collection as $entity) {
  68.             if ($this->has($entity->getUniqueIdentifier())) {
  69.                 continue;
  70.             }
  71.             $this->add($entity);
  72.         }
  73.     }
  74.     /**
  75.      * @param TElement $entity
  76.      */
  77.     public function insert(int $positionEntity $entity): void
  78.     {
  79.         $items array_values($this->elements);
  80.         $this->elements = [];
  81.         foreach ($items as $index => $item) {
  82.             if ($index === $position) {
  83.                 $this->add($entity);
  84.             }
  85.             $this->add($item);
  86.         }
  87.     }
  88.     public function getList(array $ids)
  89.     {
  90.         return $this->createNew(array_intersect_key($this->elementsarray_flip($ids)));
  91.     }
  92.     public function sortByIdArray(array $ids): void
  93.     {
  94.         $sorted = [];
  95.         foreach ($ids as $id) {
  96.             if (\is_array($id)) {
  97.                 $id implode('-'array_unique($id));
  98.             }
  99.             if (\array_key_exists($id$this->elements)) {
  100.                 $sorted[$id] = $this->elements[$id];
  101.             }
  102.         }
  103.         $this->elements $sorted;
  104.     }
  105.     protected function getExpectedClass(): string
  106.     {
  107.         return Entity::class;
  108.     }
  109. }