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

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