vendor/shopware/core/Checkout/Promotion/Cart/CartPromotionsDataDefinition.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Promotion\Cart;
  3. use Shopware\Core\Checkout\Promotion\PromotionEntity;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Shopware\Core\Framework\Struct\Struct;
  6. #[Package('checkout')]
  7. class CartPromotionsDataDefinition extends Struct
  8. {
  9.     /**
  10.      * @var array
  11.      */
  12.     private $codePromotions;
  13.     /**
  14.      * @var array
  15.      */
  16.     private $automaticPromotions;
  17.     public function __construct()
  18.     {
  19.         $this->codePromotions = [];
  20.         $this->automaticPromotions = [];
  21.     }
  22.     /**
  23.      * Adds a list of promotions to the existing
  24.      * list of automatic promotions.
  25.      */
  26.     public function addAutomaticPromotions(array $promotions): void
  27.     {
  28.         $this->automaticPromotions array_merge($this->automaticPromotions$promotions);
  29.     }
  30.     /**
  31.      * Gets all added automatic promotions.
  32.      */
  33.     public function getAutomaticPromotions(): array
  34.     {
  35.         return $this->automaticPromotions;
  36.     }
  37.     /**
  38.      * Gets all added code promotions
  39.      */
  40.     public function getCodePromotions(): array
  41.     {
  42.         return $this->codePromotions;
  43.     }
  44.     /**
  45.      * Adds the provided list of promotions
  46.      * to the existing list of promotions for this code.
  47.      *
  48.      * @param string $code       the promotion code
  49.      * @param array  $promotions a list of promotion entities for this code
  50.      */
  51.     public function addCodePromotions(string $code, array $promotions): void
  52.     {
  53.         if (!\array_key_exists($code$this->codePromotions)) {
  54.             $this->codePromotions[$code] = [];
  55.         }
  56.         /** @var array $existing */
  57.         $existing $this->codePromotions[$code];
  58.         $this->codePromotions[$code] = array_merge($existing$promotions);
  59.     }
  60.     /**
  61.      * Gets a list of all added automatic and
  62.      * code promotions.
  63.      */
  64.     public function getPromotionCodeTuples(): array
  65.     {
  66.         $list = [];
  67.         /** @var PromotionEntity $promotion */
  68.         foreach ($this->automaticPromotions as $promotion) {
  69.             $list[] = new PromotionCodeTuple(''$promotion);
  70.         }
  71.         foreach ($this->codePromotions as $code => $promotionList) {
  72.             /** @var PromotionEntity $promotion */
  73.             foreach ($promotionList as $promotion) {
  74.                 $list[] = new PromotionCodeTuple((string) $code$promotion);
  75.             }
  76.         }
  77.         return $list;
  78.     }
  79.     /**
  80.      * Gets if there is at least an empty list of promotions
  81.      * available for the provided code.
  82.      */
  83.     public function hasCode(string $code): bool
  84.     {
  85.         return \array_key_exists($code$this->codePromotions);
  86.     }
  87.     /**
  88.      * Removes the assigne promotions for the
  89.      * provided code, if existing.
  90.      */
  91.     public function removeCode(string $code): void
  92.     {
  93.         if (!\array_key_exists($code$this->codePromotions)) {
  94.             return;
  95.         }
  96.         unset($this->codePromotions[$code]);
  97.     }
  98.     /**
  99.      * Gets a flat list of all added codes.
  100.      */
  101.     public function getAllCodes(): array
  102.     {
  103.         return array_keys($this->codePromotions);
  104.     }
  105.     public function getApiAlias(): string
  106.     {
  107.         return 'cart_promotions_data_definition';
  108.     }
  109. }