vendor/shopware/core/Content/Rule/RuleCollection.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Rule;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\Rule\CartRuleScope;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\RuleAreas;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Rule\Rule;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. /**
  11.  * @extends EntityCollection<RuleEntity>
  12.  */
  13. #[Package('business-ops')]
  14. class RuleCollection extends EntityCollection
  15. {
  16.     /**
  17.      * @return RuleCollection
  18.      */
  19.     public function filterMatchingRules(Cart $cartSalesChannelContext $context)
  20.     {
  21.         return $this->filter(
  22.             function (RuleEntity $rule) use ($cart$context) {
  23.                 if (!$rule->getPayload() instanceof Rule) {
  24.                     return false;
  25.                 }
  26.                 return $rule->getPayload()->match(new CartRuleScope($cart$context));
  27.             }
  28.         );
  29.     }
  30.     public function filterForContext(): self
  31.     {
  32.         return $this->filter(
  33.             function (RuleEntity $rule) {
  34.                 return !$rule->getAreas() || !\in_array(RuleAreas::FLOW_CONDITION_AREA$rule->getAreas(), true);
  35.             }
  36.         );
  37.     }
  38.     public function filterForFlow(): self
  39.     {
  40.         return $this->filter(
  41.             function (RuleEntity $rule) {
  42.                 return $rule->getAreas() && \in_array(RuleAreas::FLOW_AREA$rule->getAreas(), true);
  43.             }
  44.         );
  45.     }
  46.     /**
  47.      * @return array<string, string[]>
  48.      */
  49.     public function getIdsByArea(): array
  50.     {
  51.         $idsByArea = [];
  52.         foreach ($this->getElements() as $rule) {
  53.             foreach ($rule->getAreas() ?? [] as $area) {
  54.                 $idsByArea[$area] = array_unique(array_merge($idsByArea[$area] ?? [], [$rule->getId()]));
  55.             }
  56.         }
  57.         return $idsByArea;
  58.     }
  59.     public function sortByPriority(): void
  60.     {
  61.         $this->sort(function (RuleEntity $aRuleEntity $b) {
  62.             return $b->getPriority() <=> $a->getPriority();
  63.         });
  64.     }
  65.     public function equals(RuleCollection $rules): bool
  66.     {
  67.         if ($this->count() !== $rules->count()) {
  68.             return false;
  69.         }
  70.         foreach ($this->elements as $element) {
  71.             if (!$rules->has($element->getId())) {
  72.                 return false;
  73.             }
  74.         }
  75.         return true;
  76.     }
  77.     public function getApiAlias(): string
  78.     {
  79.         return 'rule_collection';
  80.     }
  81.     protected function getExpectedClass(): string
  82.     {
  83.         return RuleEntity::class;
  84.     }
  85. }