Skip to content

Commit 10bca00

Browse files
author
Kenneth Ocastro
committed
Add README and LICENSE
1 parent 7bb6589 commit 10bca00

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2017 Cache Hamm
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14+
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15+
PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)