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

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