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

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