vendor/shopware/core/Checkout/Cart/Tax/Struct/CalculatedTaxCollection.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Tax\Struct;
  3. use Shopware\Core\Checkout\Cart\Price\PriceRoundingInterface;
  4. use Shopware\Core\Framework\Struct\Collection;
  5. /**
  6.  * @method CalculatedTax[]    getIterator()
  7.  * @method CalculatedTax[]    getElements()
  8.  * @method CalculatedTax|null get(string $key)
  9.  * @method CalculatedTax|null first()
  10.  * @method CalculatedTax|null last()
  11.  */
  12. class CalculatedTaxCollection extends Collection
  13. {
  14.     /**
  15.      * @param CalculatedTax $calculatedTax
  16.      */
  17.     public function add($calculatedTax): void
  18.     {
  19.         $this->set($this->getKey($calculatedTax), $calculatedTax);
  20.     }
  21.     /**
  22.      * @param string|int    $key
  23.      * @param CalculatedTax $calculatedTax
  24.      */
  25.     public function set($key$calculatedTax): void
  26.     {
  27.         parent::set($this->getKey($calculatedTax), $calculatedTax);
  28.     }
  29.     public function removeElement(CalculatedTax $calculatedTax): void
  30.     {
  31.         $this->remove($this->getKey($calculatedTax));
  32.     }
  33.     public function exists(CalculatedTax $calculatedTax): bool
  34.     {
  35.         return $this->has($this->getKey($calculatedTax));
  36.     }
  37.     public function sortByTax(): CalculatedTaxCollection
  38.     {
  39.         $this->sort(function (CalculatedTax $aCalculatedTax $b) {
  40.             return $a->getTaxRate() <=> $b->getTaxRate();
  41.         });
  42.         return $this;
  43.     }
  44.     /**
  45.      * Returns the total calculated tax for this item
  46.      */
  47.     public function getAmount(): float
  48.     {
  49.         $amounts $this->map(
  50.             function (CalculatedTax $calculatedTax) {
  51.                 return $calculatedTax->getTax();
  52.             }
  53.         );
  54.         return array_sum($amounts);
  55.     }
  56.     public function merge(self $taxCollection): self
  57.     {
  58.         $new = new self($this->elements);
  59.         foreach ($taxCollection as $calculatedTax) {
  60.             $exists $new->get($this->getKey($calculatedTax));
  61.             if (!$exists) {
  62.                 $new->add(clone $calculatedTax);
  63.                 continue;
  64.             }
  65.             $exists->increment($calculatedTax);
  66.         }
  67.         return $new;
  68.     }
  69.     public function round(PriceRoundingInterface $priceRoundingint $precision): void
  70.     {
  71.         foreach ($this->elements as $tax) {
  72.             $tax->setTax(
  73.                 $priceRounding->round($tax->getTax(), $precision)
  74.             );
  75.         }
  76.     }
  77.     public function getApiAlias(): string
  78.     {
  79.         return 'cart_tax_calculated_collection';
  80.     }
  81.     protected function getExpectedClass(): ?string
  82.     {
  83.         return CalculatedTax::class;
  84.     }
  85.     protected function getKey(CalculatedTax $element): string
  86.     {
  87.         return (string) $element->getTaxRate();
  88.     }
  89. }