vendor/shopware/core/Checkout/Cart/Price/Struct/PriceCollection.php line 13

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Price\Struct;
  3. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  4. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  5. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Struct\Collection;
  8. /**
  9.  * @extends Collection<CalculatedPrice>
  10.  */
  11. #[Package('checkout')]
  12. class PriceCollection extends Collection
  13. {
  14.     public function get($key): ?CalculatedPrice
  15.     {
  16.         $key = (int) $key;
  17.         if ($this->has($key)) {
  18.             return $this->elements[$key];
  19.         }
  20.         return null;
  21.     }
  22.     public function getTaxRules(): TaxRuleCollection
  23.     {
  24.         $rules = new TaxRuleCollection([]);
  25.         foreach ($this->getIterator() as $price) {
  26.             $rules $rules->merge($price->getTaxRules());
  27.         }
  28.         return $rules;
  29.     }
  30.     public function sum(): CalculatedPrice
  31.     {
  32.         return new CalculatedPrice(
  33.             $this->getUnitPriceAmount(),
  34.             $this->getAmount(),
  35.             $this->getCalculatedTaxes(),
  36.             $this->getTaxRules()
  37.         );
  38.     }
  39.     public function getCalculatedTaxes(): CalculatedTaxCollection
  40.     {
  41.         $taxes = new CalculatedTaxCollection([]);
  42.         foreach ($this->getIterator() as $price) {
  43.             $taxes->merge($price->getCalculatedTaxes());
  44.         }
  45.         return $taxes;
  46.     }
  47.     public function getHighestTaxRule(): TaxRuleCollection
  48.     {
  49.         $rules = new TaxRuleCollection();
  50.         $highestRate $this->getTaxRules()->highestRate();
  51.         if ($highestRate !== null) {
  52.             $rules->add(new TaxRule($highestRate->getTaxRate(), 100));
  53.         }
  54.         return $rules;
  55.     }
  56.     public function merge(self $prices): self
  57.     {
  58.         return new self(array_merge($this->elements$prices->getElements()));
  59.     }
  60.     public function getApiAlias(): string
  61.     {
  62.         return 'cart_price_collection';
  63.     }
  64.     protected function getExpectedClass(): ?string
  65.     {
  66.         return CalculatedPrice::class;
  67.     }
  68.     private function getUnitPriceAmount(): float
  69.     {
  70.         $prices $this->map(fn (CalculatedPrice $price) => $price->getUnitPrice());
  71.         return array_sum($prices);
  72.     }
  73.     private function getAmount(): float
  74.     {
  75.         $prices $this->map(fn (CalculatedPrice $price) => $price->getTotalPrice());
  76.         return array_sum($prices);
  77.     }
  78. }