vendor/shopware/core/Checkout/Cart/Tax/Struct/TaxRuleCollection.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Tax\Struct;
  3. use Shopware\Core\Framework\Struct\Collection;
  4. /**
  5.  * @method TaxRule[]    getIterator()
  6.  * @method TaxRule[]    getElements()
  7.  * @method TaxRule|null first()
  8.  * @method TaxRule|null last()
  9.  */
  10. class TaxRuleCollection extends Collection
  11. {
  12.     /**
  13.      * @param TaxRule $taxRule
  14.      */
  15.     public function add($taxRule): void
  16.     {
  17.         $this->set($this->getKey($taxRule), $taxRule);
  18.     }
  19.     /**
  20.      * @param string|int $key
  21.      * @param TaxRule    $taxRule
  22.      */
  23.     public function set($key$taxRule): void
  24.     {
  25.         parent::set($this->getKey($taxRule), $taxRule);
  26.     }
  27.     public function removeElement(TaxRule $taxRule): void
  28.     {
  29.         $this->remove($this->getKey($taxRule));
  30.     }
  31.     public function exists(TaxRule $taxRule): bool
  32.     {
  33.         return $this->has($this->getKey($taxRule));
  34.     }
  35.     public function get($taxRate): ?TaxRule
  36.     {
  37.         $key = (string) $taxRate;
  38.         if ($this->has($key)) {
  39.             return $this->elements[$key];
  40.         }
  41.         return null;
  42.     }
  43.     public function merge(self $rules): self
  44.     {
  45.         $new = new self($this->elements);
  46.         $rules->map(
  47.             function (TaxRule $rule) use ($new): void {
  48.                 if (!$new->exists($rule)) {
  49.                     $new->add($rule);
  50.                 }
  51.             }
  52.         );
  53.         return $new;
  54.     }
  55.     public function highestRate(): ?TaxRule
  56.     {
  57.         return $this->reduce(function ($result$item) {
  58.             return $result === null || $item->getTaxRate() > $result->getTaxRate() ? $item $result;
  59.         });
  60.     }
  61.     public function getApiAlias(): string
  62.     {
  63.         return 'cart_tax_rule_collection';
  64.     }
  65.     protected function getExpectedClass(): ?string
  66.     {
  67.         return TaxRule::class;
  68.     }
  69.     protected function getKey(TaxRule $element): string
  70.     {
  71.         return (string) $element->getTaxRate();
  72.     }
  73. }