|
| 1 | +# PHP Rules Engine |
| 2 | + |
| 3 | +`kennyth01/php-rules-engine` is a lightweight and flexible PHP rules engine that evaluates complex conditional logic using JSON-based rule configurations. It is designed to handle dynamic, reusable, and maintainable rule logic, making it ideal for applications with complex business requirements that must adapt to changing conditions. |
| 4 | + |
| 5 | +This library, inspired by the `json-rules-engine`, ([link](https://github.com/CacheControl/json-rules-engine)) enables developers to define rules with nested conditions, logical operators (`all`, `any`, `not`), and rule dependencies. |
| 6 | + |
| 7 | +## Features |
| 8 | +- **JSON-Configurable Rules**: Easily define rules and conditions in JSON format. |
| 9 | +- **Rule Dependencies**: Reference other rules as conditions to create complex evaluations. |
| 10 | +- **Logical Operators**: Supports `all` (AND), `any` (OR), and `not` operators, allowing for nested conditions. |
| 11 | +- **Custom Events and Failure Messages**: Attach custom messages for success or failure, making evaluations easy to interpret. |
| 12 | +- **Interpret Rules**: Outputs a human readable English interpretation of the condition using logical operators. |
| 13 | + |
| 14 | + |
| 15 | +## Installation |
| 16 | +Install via Composer: |
| 17 | +```bash |
| 18 | +composer require kennyth01/php-rules-engine |
| 19 | + |
| 20 | +``` |
| 21 | + |
| 22 | +## Basic Example |
| 23 | +This example demonstrates an engine for detecting whether a basketball player has fouled out (a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out). |
| 24 | + |
| 25 | +### |
| 26 | +1. Define the rule (lets assume you store this in `rule.player.isFouledOut.json`) |
| 27 | +```json |
| 28 | +{ |
| 29 | + "name":"rule.player.isFouledOut", |
| 30 | + "conditions": { |
| 31 | + "any": [ |
| 32 | + { |
| 33 | + "all": [ |
| 34 | + { |
| 35 | + "fact": "gameDuration", |
| 36 | + "operator": "equal", |
| 37 | + "value": 40 |
| 38 | + }, |
| 39 | + { |
| 40 | + "fact": "personalFoulCount", |
| 41 | + "operator": "greaterThanInclusive", |
| 42 | + "value": 5 |
| 43 | + } |
| 44 | + ], |
| 45 | + "name": "short foul limit" |
| 46 | + }, |
| 47 | + { |
| 48 | + "all": [ |
| 49 | + { |
| 50 | + "fact": "gameDuration", |
| 51 | + "operator": "equal", |
| 52 | + "value": 48 |
| 53 | + }, |
| 54 | + { |
| 55 | + "not": { |
| 56 | + "fact": "personalFoulCount", |
| 57 | + "operator": "lessThan", |
| 58 | + "value": 6 |
| 59 | + } |
| 60 | + } |
| 61 | + ], |
| 62 | + "name": "long foul limit" |
| 63 | + } |
| 64 | + ] |
| 65 | + }, |
| 66 | + "event": { |
| 67 | + "type": "fouledOut", |
| 68 | + "params": { |
| 69 | + "message": "Player has fouled out!" |
| 70 | + } |
| 71 | + }, |
| 72 | + "failureEvent": { |
| 73 | + "type": "fouledOut", |
| 74 | + "params": { |
| 75 | + "message": "Player has not fouled out" |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +``` |
| 81 | +### |
| 82 | +2. Trigger the engine and evaluate |
| 83 | +```php |
| 84 | +$engine = new Engine(); |
| 85 | +$rule = json_decode(file_get_contents('rule.player.isFouledOut.json'), true); |
| 86 | + |
| 87 | +$engine->addRule(new Rule($rule)); |
| 88 | +$engine->addFact('personalFoulCount', 6); |
| 89 | +$engine->addFact('gameDuration', 40); |
| 90 | + |
| 91 | +$engine->setTargetRule('rule.player.isFouledOut'); |
| 92 | + |
| 93 | +$result = $engine->evaluate(); |
| 94 | +print_r($result); |
| 95 | +``` |
| 96 | +### |
| 97 | +3. Output Example |
| 98 | +```php |
| 99 | +[ |
| 100 | + 'type' => 'fouledOut', |
| 101 | + 'params' => [ |
| 102 | + 'message' => 'Player has fouled out!' |
| 103 | + ], |
| 104 | + 'facts' => [ |
| 105 | + 'personalFoulCount' => 6, |
| 106 | + 'gameDuration' => 40 |
| 107 | + |
| 108 | + ], |
| 109 | + 'interpretation' => '((gameDuration is equal to 40 AND personalFoulCount is >= 5) OR (gameDuration is equal to 48 AND NOT (personalFoulCount is less than 6)))' |
| 110 | +] |
| 111 | +``` |
| 112 | +## Advanced Examples |
| 113 | +For other examples, refer to the `tests` directory |
| 114 | + |
| 115 | +## Run the test |
| 116 | +```bash |
| 117 | +./vendor/bin/phpunit tests |
| 118 | +``` |
| 119 | + |
| 120 | +## License |
| 121 | +[ISC](./LICENSE) |
0 commit comments