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

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\CashRounding;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  5. use Shopware\Core\Framework\Feature;
  6. use Shopware\Core\Framework\Struct\Collection;
  7. use Shopware\Core\Framework\Util\FloatComparator;
  8. /**
  9.  * @extends Collection<CalculatedTax>
  10.  */
  11. class CalculatedTaxCollection extends Collection
  12. {
  13.     /**
  14.      * @param CalculatedTax $calculatedTax
  15.      */
  16.     public function add($calculatedTax): void
  17.     {
  18.         $this->set($this->getKey($calculatedTax), $calculatedTax);
  19.     }
  20.     /**
  21.      * @param string|int    $key
  22.      * @param CalculatedTax $calculatedTax
  23.      */
  24.     public function set($key$calculatedTax): void
  25.     {
  26.         parent::set($this->getKey($calculatedTax), $calculatedTax);
  27.     }
  28.     public function removeElement(CalculatedTax $calculatedTax): void
  29.     {
  30.         $this->remove($this->getKey($calculatedTax));
  31.     }
  32.     public function exists(CalculatedTax $calculatedTax): bool
  33.     {
  34.         return $this->has($this->getKey($calculatedTax));
  35.     }
  36.     public function sortByTax(): CalculatedTaxCollection
  37.     {
  38.         $this->sort(function (CalculatedTax $aCalculatedTax $b) {
  39.             return $a->getTaxRate() <=> $b->getTaxRate();
  40.         });
  41.         return $this;
  42.     }
  43.     /**
  44.      * Returns the total calculated tax for this item
  45.      */
  46.     public function getAmount(): float
  47.     {
  48.         $amounts $this->map(
  49.             function (CalculatedTax $calculatedTax) {
  50.                 return $calculatedTax->getTax();
  51.             }
  52.         );
  53.         return FloatComparator::cast(array_sum($amounts));
  54.     }
  55.     /**
  56.      * @deprecated tag:v6.5.0 - keep parameter will be removed. Additionally the function always keeps the existing collection
  57.      */
  58.     public function merge(self $taxCollectionbool $keep false): self
  59.     {
  60.         $new $this;
  61.         //@deprecated tag:v6.5.0 remove complete if $new should be always $this
  62.         if (!$keep) {
  63.             Feature::triggerDeprecationOrThrow(
  64.                 'v6.5.0.0',
  65.                 \sprintf(
  66.                     'Passing second parameter `$keep` to method "%s" of class "%s" is deprecated, the parameter will be removed in v6.5.0.0. and the behaviour for $keep=true will be the default behaviour.',
  67.                     __METHOD__,
  68.                     __CLASS__
  69.                 )
  70.             );
  71.             $new = new self($this->elements);
  72.         }
  73.         foreach ($taxCollection as $calculatedTax) {
  74.             $exists $new->get($this->getKey($calculatedTax));
  75.             if (!$exists) {
  76.                 $new->add(clone $calculatedTax);
  77.                 continue;
  78.             }
  79.             $exists->increment($calculatedTax);
  80.         }
  81.         return $new;
  82.     }
  83.     public function round(CashRounding $roundingCashRoundingConfig $config): void
  84.     {
  85.         foreach ($this->elements as $tax) {
  86.             $tax->setTax(
  87.                 $rounding->mathRound($tax->getTax(), $config)
  88.             );
  89.         }
  90.     }
  91.     public function getApiAlias(): string
  92.     {
  93.         return 'cart_tax_calculated_collection';
  94.     }
  95.     protected function getExpectedClass(): ?string
  96.     {
  97.         return CalculatedTax::class;
  98.     }
  99.     protected function getKey(CalculatedTax $element): string
  100.     {
  101.         return (string) $element->getTaxRate();
  102.     }
  103. }