vendor/shopware/core/Content/Product/ProductCollection.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product;
  3. use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerCollection;
  4. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaCollection;
  5. use Shopware\Core\Content\Product\Aggregate\ProductPrice\ProductPriceCollection;
  6. use Shopware\Core\Content\Product\Aggregate\ProductPrice\ProductPriceEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  8. use Shopware\Core\System\Tax\TaxCollection;
  9. use Shopware\Core\System\Unit\UnitCollection;
  10. /**
  11.  * @extends EntityCollection<ProductEntity>
  12.  */
  13. class ProductCollection extends EntityCollection
  14. {
  15.     /**
  16.      * @return list<string>
  17.      */
  18.     public function getParentIds(): array
  19.     {
  20.         return $this->fmap(function (ProductEntity $product) {
  21.             return $product->getParentId();
  22.         });
  23.     }
  24.     public function filterByParentId(string $id): self
  25.     {
  26.         return $this->filter(function (ProductEntity $product) use ($id) {
  27.             return $product->getParentId() === $id;
  28.         });
  29.     }
  30.     /**
  31.      * @return list<string>
  32.      */
  33.     public function getTaxIds(): array
  34.     {
  35.         return $this->fmap(function (ProductEntity $product) {
  36.             return $product->getTaxId();
  37.         });
  38.     }
  39.     public function filterByTaxId(string $id): self
  40.     {
  41.         return $this->filter(function (ProductEntity $product) use ($id) {
  42.             return $product->getTaxId() === $id;
  43.         });
  44.     }
  45.     /**
  46.      * @return list<string>
  47.      */
  48.     public function getManufacturerIds(): array
  49.     {
  50.         return $this->fmap(function (ProductEntity $product) {
  51.             return $product->getManufacturerId();
  52.         });
  53.     }
  54.     public function filterByManufacturerId(string $id): self
  55.     {
  56.         return $this->filter(function (ProductEntity $product) use ($id) {
  57.             return $product->getManufacturerId() === $id;
  58.         });
  59.     }
  60.     /**
  61.      * @return list<string>
  62.      */
  63.     public function getUnitIds(): array
  64.     {
  65.         return $this->fmap(function (ProductEntity $product) {
  66.             return $product->getUnitId();
  67.         });
  68.     }
  69.     public function filterByUnitId(string $id): self
  70.     {
  71.         return $this->filter(function (ProductEntity $product) use ($id) {
  72.             return $product->getUnitId() === $id;
  73.         });
  74.     }
  75.     public function getTaxes(): TaxCollection
  76.     {
  77.         return new TaxCollection(
  78.             $this->fmap(function (ProductEntity $product) {
  79.                 return $product->getTax();
  80.             })
  81.         );
  82.     }
  83.     public function getManufacturers(): ProductManufacturerCollection
  84.     {
  85.         return new ProductManufacturerCollection(
  86.             $this->fmap(function (ProductEntity $product) {
  87.                 return $product->getManufacturer();
  88.             })
  89.         );
  90.     }
  91.     public function getUnits(): UnitCollection
  92.     {
  93.         return new UnitCollection(
  94.             $this->fmap(function (ProductEntity $product) {
  95.                 return $product->getUnit();
  96.             })
  97.         );
  98.     }
  99.     /**
  100.      * @return list<string>
  101.      */
  102.     public function getPriceIds(): array
  103.     {
  104.         $ids = [[]];
  105.         foreach ($this->getIterator() as $element) {
  106.             if ($element->getPrices() !== null) {
  107.                 $ids[] = $element->getPrices()->getIds();
  108.             }
  109.         }
  110.         return array_merge(...$ids);
  111.     }
  112.     public function getPrices(): ProductPriceCollection
  113.     {
  114.         $rules = [[]];
  115.         foreach ($this->getIterator() as $element) {
  116.             /** @var ProductPriceCollection $prices */
  117.             $prices = $element->getPrices();
  118.             $rules[] = (array) $prices;
  119.         }
  120.         /** @var array<ProductPriceEntity> $productPriceEntities */
  121.         $productPriceEntities = array_merge(...$rules);
  122.         return new ProductPriceCollection($productPriceEntities);
  123.     }
  124.     /**
  125.      * @param list<string> $optionIds
  126.      */
  127.     public function filterByOptionIds(array $optionIds): self
  128.     {
  129.         return $this->filter(function (ProductEntity $product) use ($optionIds) {
  130.             $ids = $product->getOptionIds() ?? [];
  131.             $same = array_intersect($ids, $optionIds);
  132.             return \count($same) === \count($optionIds);
  133.         });
  134.     }
  135.     public function getCovers(): ProductMediaCollection
  136.     {
  137.         return new ProductMediaCollection($this->fmap(function (ProductEntity $product) {
  138.             return $product->getCover();
  139.         }));
  140.     }
  141.     public function getApiAlias(): string
  142.     {
  143.         return 'product_collection';
  144.     }
  145.     protected function getExpectedClass(): string
  146.     {
  147.         return ProductEntity::class;
  148.     }
  149. }