addRule example

/** * @var Container|null */
    protected $filter;

    public function addRule(Rule $rule): void
    {
        if ($this->filter === null) {
            $this->filter = new AndRule();
        }

        $this->filter->addRule($rule);
    }

    /** * @param Rule[] $rules */
    public function setRules(array $rules): void
    {
        $this->filter = new AndRule($rules);
    }

    /** * @return Rule[] */
private function decodeRule(array $rule): Rule
    {
        if (!$this->ruleConditionRegistry->has($rule['_name'])) {
            throw new ConditionTypeNotFound($rule['_name']);
        }

        $ruleClass = $this->ruleConditionRegistry->getRuleClass($rule['_name']);
        $object = new $ruleClass();

        if (\array_key_exists('rules', $rule) && $object instanceof Container) {
            foreach ($rule['rules'] as $item) {
                $object->addRule($this->decodeRule($item));
            }
        } else {
            $object->assign($rule);
        }

        return $object;
    }

    private function validateProperties(array $data, array $constraints, string $path): void
    {
        foreach ($constraints as $key => $constraint) {
            

    public function testIfMatchesWithMatchAllLineItemsRule(
        array $lineItems,
        string $operator,
        bool $expected
    ): void {
        $this->rule->assign([
            'operator' => $operator,
            'amount' => 100,
        ]);
        $allLineItemsRule = new MatchAllLineItemsRule([], null, 'product');
        $allLineItemsRule->addRule($this->rule);

        $lineItemCollection = new LineItemCollection($lineItems);

        $cart = $this->createCart($lineItemCollection);

        $match = $allLineItemsRule->match(new CartRuleScope(
            $cart,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }
continue;
            }

            if ($rule['value'] !== null) {
                $object->assign(json_decode((string) $rule['value'], true, 512, \JSON_THROW_ON_ERROR));
            }

            if ($object instanceof ContainerInterface) {
                $children = $this->buildNested($rules$rule['id']);
                foreach ($children as $child) {
                    $object->addRule($child);
                }
            }

            $nested[] = $object;
        }

        return $nested;
    }
}
static::assertSame($matching$rule->match($scope));
    }

    public function testAndRuleNameIsStillTheSame(): void
    {
        static::assertSame('andContainer', (new AndRule())->getName());
    }

    public function testICanAddRulesAfterwards(): void
    {
        $rule = new AndRule([new TrueRule()]);
        $rule->addRule(new TrueRule());

        static::assertEquals([new TrueRule()new TrueRule()]$rule->getRules());

        $rule->setRules([new FalseRule()]);
        static::assertEquals([new FalseRule()]$rule->getRules());
    }

    public function testConstraintsAreStillTheSame(): void
    {
        static::assertEquals(
            ['rules' => [new ArrayOfType(Rule::class)]],
            (
string $operator,
        array $categoryIds,
        bool $expected
    ): void {
        $lineItemRule = new LineItemInCategoryRule();
        $lineItemRule->assign([
            'categoryIds' => $categoryIds,
            'operator' => $operator,
        ]);

        $allLineItemsRule = new MatchAllLineItemsRule();
        $allLineItemsRule->addRule($lineItemRule);

        $lineItemCollection = new LineItemCollection([
            $this->createLineItemWithCategories($categoryIdsProductA),
            $this->createLineItemWithCategories($categoryIdsProductB),
        ]);

        $cart = $this->createCart($lineItemCollection);

        $match = $allLineItemsRule->match(new CartRuleScope(
            $cart,
            $this->createMock(SalesChannelContext::class)
        ));
            // if so, create customer rules for it and add that also as             // a separate OR condition to our main persona rule             if ($this->getPersonaCustomers() !== null) {
                $personaCustomerOR = new OrRule();

                foreach ($this->getPersonaCustomers()->getElements() as $customer) {
                    // build our new rule for this                     // customer and his/her customer number                     $custRule = new CustomerNumberRule();
                    $custRule->assign(['numbers' => [$customer->getCustomerNumber()], 'operator' => CustomerNumberRule::OPERATOR_EQ]);

                    $personaCustomerOR->addRule($custRule);
                }

                // add the rule to our main rule                 $requirements->addRule($personaCustomerOR);
            }
        } else {
            // we use persona rules.             // check if we have persona rules and add them             // to our persona OR as a separate OR rule with all configured rules             if ($this->getPersonaRules() !== null && \count($this->getPersonaRules()->getElements()) > 0) {
                $personaRuleOR = new OrRule();

                
// we do only need to build a target rule if user has allowed it         // and the rule collection is not empty         if ($discountRuleCollection instanceof RuleCollection && $discount->isConsiderAdvancedRules() && $discountRuleCollection->count() > 0) {
            $targetFilter = new OrRule();

            foreach ($discountRuleCollection as $discountRule) {
                /** @var Rule|string|null $rule */
                $rule = $discountRule->getPayload();

                if ($rule instanceof Rule) {
                    $targetFilter->addRule($rule);
                }
            }
        }

        // our promotion values are always negative values.         // either type percentage or absolute needs to be negative to get         // automatically subtracted within the calculation process         $promotionValue = -abs($discount->getValue());

        switch ($discount->getType()) {
            case PromotionDiscountEntity::TYPE_ABSOLUTE:
                
$discountFilter = $this->getFakeRule($amount$operator);

        $discountRuleEntity = new RuleEntity();
        $discountRuleEntity->setId('foo');
        $discountRuleEntity->setPayload($discountFilter);

        $ruleCollection = new RuleCollection();
        $ruleCollection->add($discountRuleEntity);
        $discount->setDiscountRules($ruleCollection);

        $expectedRule = new OrRule();
        $expectedRule->addRule($discountFilter);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, 'C1', $currencyFactor);

        static::assertInstanceOf(AbsolutePriceDefinition::class$item->getPriceDefinition());
        static::assertEquals($expectedRule$item->getPriceDefinition()->getFilter());
    }

    /** * This test verifies that the correct discount filter * is set in the discountItemBuilder * * @group promotions */
Home | Imprint | This part of the site doesn't use cookies.