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