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

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