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

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