vendor/shopware/core/Checkout/Cart/LineItem/LineItemCollection.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\LineItem;
  3. use Shopware\Core\Checkout\Cart\CartException;
  4. use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
  5. use Shopware\Core\Checkout\Cart\Exception\LineItemNotStackableException;
  6. use Shopware\Core\Checkout\Cart\Exception\MixedLineItemTypeException;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  8. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  9. use Shopware\Core\Framework\Feature;
  10. use Shopware\Core\Framework\Struct\Collection;
  11. /**
  12.  * @extends Collection<LineItem>
  13.  */
  14. class LineItemCollection extends Collection
  15. {
  16.     public function __construct(iterable $elements = [])
  17.     {
  18.         parent::__construct();
  19.         foreach ($elements as $lineItem) {
  20.             $this->add($lineItem);
  21.         }
  22.     }
  23.     /**
  24.      * @param LineItem $lineItem
  25.      *
  26.      * @throws MixedLineItemTypeException
  27.      * @throws InvalidQuantityException
  28.      * @throws LineItemNotStackableException
  29.      */
  30.     public function add($lineItem): void
  31.     {
  32.         $this->validateType($lineItem);
  33.         $exists $this->get($lineItem->getId());
  34.         if ($exists && $exists->getType() !== $lineItem->getType()) {
  35.             if (Feature::isActive('v6.5.0.0')) {
  36.                 throw CartException::mixedLineItemType($lineItem->getId(), $lineItem->getType());
  37.             }
  38.             throw new MixedLineItemTypeException($lineItem->getId(), $exists->getType());
  39.         }
  40.         if ($exists) {
  41.             $exists->setQuantity($lineItem->getQuantity() + $exists->getQuantity());
  42.             return;
  43.         }
  44.         $this->elements[$this->getKey($lineItem)] = $lineItem;
  45.     }
  46.     /**
  47.      * @param int|string $key
  48.      * @param LineItem   $lineItem
  49.      */
  50.     public function set($key$lineItem): void
  51.     {
  52.         $this->validateType($lineItem);
  53.         $this->elements[$this->getKey($lineItem)] = $lineItem;
  54.     }
  55.     public function removeElement(LineItem $lineItem): void
  56.     {
  57.         $this->remove($this->getKey($lineItem));
  58.     }
  59.     public function exists(LineItem $lineItem): bool
  60.     {
  61.         return $this->has($this->getKey($lineItem));
  62.     }
  63.     public function get($identifier): ?LineItem
  64.     {
  65.         if ($this->has($identifier)) {
  66.             return $this->elements[$identifier];
  67.         }
  68.         return null;
  69.     }
  70.     /**
  71.      * @return LineItem[]
  72.      */
  73.     public function filterFlatByType(string $type): array
  74.     {
  75.         $lineItems $this->getFlat();
  76.         $filtered = [];
  77.         foreach ($lineItems as $lineItem) {
  78.             if ($lineItem->getType() === $type) {
  79.                 $filtered[] = $lineItem;
  80.             }
  81.         }
  82.         return $filtered;
  83.     }
  84.     public function filterType(string $type): LineItemCollection
  85.     {
  86.         return $this->filter(
  87.             function (LineItem $lineItem) use ($type) {
  88.                 return $lineItem->getType() === $type;
  89.             }
  90.         );
  91.     }
  92.     public function getPayload(): array
  93.     {
  94.         return $this->map(function (LineItem $lineItem) {
  95.             return $lineItem->getPayload();
  96.         });
  97.     }
  98.     public function getPrices(): PriceCollection
  99.     {
  100.         return new PriceCollection(
  101.             array_filter(array_map(static function (LineItem $lineItem) {
  102.                 return $lineItem->getPrice();
  103.             }, array_values($this->getElements())))
  104.         );
  105.     }
  106.     /**
  107.      * @return LineItem[]
  108.      */
  109.     public function getFlat(): array
  110.     {
  111.         return $this->buildFlat($this);
  112.     }
  113.     public function sortByPriority(): void
  114.     {
  115.         $lineItemsByPricePriority = [];
  116.         /** @var LineItem $lineItem */
  117.         foreach ($this->elements as $lineItem) {
  118.             $priceDefinitionPriority QuantityPriceDefinition::SORTING_PRIORITY;
  119.             if ($lineItem->getPriceDefinition()) {
  120.                 $priceDefinitionPriority $lineItem->getPriceDefinition()->getPriority();
  121.             }
  122.             if (!\array_key_exists($priceDefinitionPriority$lineItemsByPricePriority)) {
  123.                 $lineItemsByPricePriority[$priceDefinitionPriority] = [];
  124.             }
  125.             $lineItemsByPricePriority[$priceDefinitionPriority][] = $lineItem;
  126.         }
  127.         // Sort all line items by their price definition priority
  128.         krsort($lineItemsByPricePriority);
  129.         if (\count($lineItemsByPricePriority)) {
  130.             $this->elements array_merge(...$lineItemsByPricePriority);
  131.         }
  132.     }
  133.     public function filterGoods(): self
  134.     {
  135.         return $this->filter(
  136.             function (LineItem $lineItem) {
  137.                 return $lineItem->isGood();
  138.             }
  139.         );
  140.     }
  141.     /**
  142.      * @return LineItem[]
  143.      */
  144.     public function filterGoodsFlat(): array
  145.     {
  146.         $lineItems $this->getFlat();
  147.         $filtered = [];
  148.         foreach ($lineItems as $lineItem) {
  149.             if ($lineItem->isGood()) {
  150.                 $filtered[] = $lineItem;
  151.             }
  152.         }
  153.         return $filtered;
  154.     }
  155.     public function getTypes(): array
  156.     {
  157.         return $this->fmap(
  158.             function (LineItem $lineItem) {
  159.                 return $lineItem->getType();
  160.             }
  161.         );
  162.     }
  163.     public function getReferenceIds(): array
  164.     {
  165.         return $this->fmap(
  166.             function (LineItem $lineItem) {
  167.                 return $lineItem->getReferencedId();
  168.             }
  169.         );
  170.     }
  171.     public function getApiAlias(): string
  172.     {
  173.         return 'cart_line_item_collection';
  174.     }
  175.     public function getTotalQuantity(): int
  176.     {
  177.         return $this->reduce(function ($result$item) {
  178.             return $result $item->getQuantity();
  179.         }, 0);
  180.     }
  181.     protected function getKey(LineItem $element): string
  182.     {
  183.         return $element->getId();
  184.     }
  185.     protected function getExpectedClass(): ?string
  186.     {
  187.         return LineItem::class;
  188.     }
  189.     private function buildFlat(LineItemCollection $lineItems): array
  190.     {
  191.         $flat = [];
  192.         foreach ($lineItems as $lineItem) {
  193.             $flat[] = $lineItem;
  194.             foreach ($this->buildFlat($lineItem->getChildren()) as $nest) {
  195.                 $flat[] = $nest;
  196.             }
  197.         }
  198.         return $flat;
  199.     }
  200. }