2. Usage

2.1. Using Rule Applicator Chain Service

The Rule Applicator Chain service is used to execute all rule applicators added to the chain.

Usage:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
 // example.php
 // ..

 use Acme\DemoBundle\Model\Subject;
 use SWP\Component\Rule\Applicator\RuleApplicatorChain;
 use SWP\Component\Rule\Model\Rule;
 // ..

 $applicatorChain = new RuleApplicatorChain();
 $applicatorChain->addApplicator(/* instance of RuleApplicatorInterface */)

 $subject = new Subject(); // an instance of RuleSubjectInterface
 $applicatorChain->isSupported($subject); // return true or false

 $rule = new Rule();
 // ..

 $applicatorChain->apply($rule, $subject);

2.2. What is Rule Evaluator?

Rule evaluators are used to evaluate rule on an given object and make sure it matches the rule’s criteria. By default, the Symfony Expression Language Component is used which perfectly fits into the business rules engine concept.

2.3. Adding new Rule Evaluator

There is a possibility to create your custom implementation for Rule Evaluator. All you need to do is to create a new class and implement SWP\Component\Rule\Evaluator\RuleEvaluatorInterface interface.

2.4. What is Rule Processor?

Rule processor is responsible for processing all rules and apply them respectively to an object, based on the defined rule priority. The greater the priority value, the higher the priority.

Rule Processor implements SWP\Component\Rule\Processor\RuleProcessorInterface interface.

2.5. What is Rule Applicator?

Rule applicators are used to apply given rule’s configuration to an object. You create your custom rule applicators and register them in Rule Applicator Chain service which triggers apply method on them if the given applicator is supported by the rule subject.

2.6. Adding new Rule Applicator

There is a possibility to create your custom implementation for Rule Applicator. All you need to do is to create a new class and implement SWP\Component\Rule\Applicator\RuleApplicatorInterface interface.