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

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