Download the PHP package rogervila/php-simple-rules-engine without Composer
On this page you can find all versions of the php package rogervila/php-simple-rules-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rogervila/php-simple-rules-engine
More information about rogervila/php-simple-rules-engine
Files in rogervila/php-simple-rules-engine
Package php-simple-rules-engine
Short Description PHP Simple Rules Engine
License MIT
Homepage https://github.com/rogervila/php-simple-rules-engine
Informations about the package php-simple-rules-engine
PHP Simple Rules Engine
About
Evaluate rules based on a subject.
Usage
The package expects a subject and a list of rules.
Each rule must be a class that extends \SimpleRulesEngine\Rule
.
The subject parameter can be any type of object (mixed
)
Basic usage
Rules return a \SimpleRulesEngine\Evaluation
object that should contain a result
property defined by the user.
Also, the user can define the value of the stop
property to determine if the evaluation process should stop or continue.
In this example, the stop
property value does not affect the evaluation process since we are evaluating only one rule.
Advanced usage
When evaluating multiple rules you can retrieve the history of the rules evaluated for a specific evaluation process by passing the withHistory
parameter as true
.
The final \SimpleRulesEngine\Evaluation
object will contain a history
list with evaluations returned by the rules evaluated during the evaluation process.
Check test_evaluation_with_history
method on tests/RulesEngineTest.php
for a more detailed implementation.
Recursive usage
You might pass rules and array of rules to recursively evaluate them.
Note: A future version of this library might come with Openswoole support to parallelize the evaluation of each subset of rules.
Examples
The examples are very simple for demo purposes, but they show the basic features this package comes with.
There is a python rules engine called durable rules that comes with some examples. We will recreate them with this package.
Pattern matching
Find a credit card type based on its number.
Check test_match_example_with_cards
method on tests/RulesEngineTest.php
for a more detailed implementation.
Set of facts
Find an animal based on facts.
In this case, we will compare the current rule result with the previous evaluation result. If they match, we stop the evaluation process.
Check test_facts_example
method on tests/RulesEngineTest.php
for a more detailed implementation.
Author
Created by Roger Vilà
License
PHP Simple Rules Engine is open-sourced software licensed under the MIT license.