@@ -5,6 +5,7 @@ Ruler is a simple stateless production rules engine for PHP 5.3+.
5
5
6
6
[ ![ Package version] ( http://img.shields.io/packagist/v/ruler/ruler.svg?style=flat-square )] ( https://packagist.org/packages/ruler/ruler )
7
7
[ ![ Build status] ( https://img.shields.io/github/workflow/status/bobthecow/Ruler/Unit%20Tests/main.svg?style=flat-square )] ( https://github.com/bobthecow/Ruler/actions?query=branch:main )
8
+ [ ![ StyleCI] ( https://styleci.io/repos/1906921/shield )] ( https://styleci.io/repos/1906921 )
8
9
9
10
Ruler has an easy, straightforward DSL
10
11
--------------------------------------
@@ -23,13 +24,11 @@ $rule = $rb->create(
23
24
}
24
25
);
25
26
26
- $context = new Context(array(
27
+ $context = new Context([
27
28
'minNumPeople' => 5,
28
29
'maxNumPeople' => 25,
29
- 'actualNumPeople' => function() {
30
- return 6;
31
- },
32
- ));
30
+ 'actualNumPeople' => fn() => 6,
31
+ ]);
33
32
34
33
$rule->execute($context); // "Yay!"
35
34
```
@@ -42,22 +41,20 @@ $rule->execute($context); // "Yay!"
42
41
``` php
43
42
$actualNumPeople = new Variable('actualNumPeople');
44
43
$rule = new Rule(
45
- new Operator\LogicalAnd(array(
44
+ new Operator\LogicalAnd([
46
45
new Operator\LessThanOrEqualTo(new Variable('minNumPeople'), $actualNumPeople),
47
46
new Operator\GreaterThanOrEqualTo(new Variable('maxNumPeople'), $actualNumPeople)
48
- ) ),
47
+ ] ),
49
48
function() {
50
49
echo 'YAY!';
51
50
}
52
51
);
53
52
54
- $context = new Context(array(
53
+ $context = new Context([
55
54
'minNumPeople' => 5,
56
55
'maxNumPeople' => 25,
57
- 'actualNumPeople' => function() {
58
- return 6;
59
- },
60
- ));
56
+ 'actualNumPeople' => fn() => 6,
57
+ ]);
61
58
62
59
$rule->execute($context); // "Yay!"
63
60
```
@@ -165,10 +162,10 @@ $eitherOne = $rb->create($rb->logicalOr($aEqualsB, $aDoesNotEqualB));
165
162
166
163
// Just to mix things up, we'll populate our evaluation context with completely
167
164
// random values...
168
- $context = new Context(array(
165
+ $context = new Context([
169
166
'a' => rand(),
170
167
'b' => rand(),
171
- ) );
168
+ ] );
172
169
173
170
// Hint: this is always true!
174
171
$eitherOne->evaluate($context);
@@ -190,11 +187,9 @@ $rb->logicalXor($aEqualsB, $aDoesNotEqualB); // True if only one condition is tr
190
187
` evaluate() ` a Rule with Context to figure out whether it is true.
191
188
192
189
``` php
193
- $context = new Context(array(
194
- 'userName' => function() {
195
- return isset($_SESSION['userName']) ? $_SESSION['userName'] : null;
196
- }
197
- ));
190
+ $context = new Context([
191
+ 'userName' => fn() => $_SESSION['userName'] ?? null,
192
+ ]);
198
193
199
194
$userIsLoggedIn = $rb->create($rb['userName']->notEqualTo(null));
200
195
@@ -238,7 +233,7 @@ $hiEveryoneElse = $rb->create(
238
233
}
239
234
);
240
235
241
- $rules = new RuleSet(array( $hiJustin, $hiJon, $hiEveryoneElse) );
236
+ $rules = new RuleSet([ $hiJustin, $hiJon, $hiEveryoneElse] );
242
237
243
238
// Let's add one more Rule, so non-authenticated users have a chance to log in
244
239
$redirectForAuthentication = $rb->create($rb->logicalNot($userIsLoggedIn), function() {
@@ -271,12 +266,10 @@ Rules.
271
266
$context = new Context;
272
267
273
268
// Some static values...
274
- $context['reallyAnnoyingUsers'] = array( 'bobthecow', 'jwage') ;
269
+ $context['reallyAnnoyingUsers'] = [ 'bobthecow', 'jwage'] ;
275
270
276
271
// You'll remember this one from before
277
- $context['userName'] = function() {
278
- return isset($_SESSION['userName']) ? $_SESSION['userName'] : null;
279
- };
272
+ $context['userName'] = fn() => $_SESSION['userName'] ?? null;
280
273
281
274
// Let's pretend you have an EntityManager named `$em`...
282
275
$context['user'] = function() use ($em, $context) {
@@ -333,7 +326,7 @@ $context['userRoles'] = function() use ($em, $context) {
333
326
return $user->roles();
334
327
} else {
335
328
// return a default "anonymous" role if there is no current user
336
- return array( 'anonymous') ;
329
+ return [ 'anonymous'] ;
337
330
}
338
331
};
339
332
@@ -365,7 +358,7 @@ their convenient RuleBuilder interface:
365
358
// We can skip over the Context Variable building above. We'll simply set our,
366
359
// default roles on the VariableProperty itself, then go right to writing rules:
367
360
368
- $rb['user']['roles'] = array( 'anonymous') ;
361
+ $rb['user']['roles'] = [ 'anonymous'] ;
369
362
370
363
$rb->create(
371
364
$rb->logicalAnd(
@@ -410,7 +403,7 @@ use Ruler\Value;
410
403
411
404
class ALotGreaterThan extends VariableOperator implements Proposition
412
405
{
413
- public function evaluate(Context $context)
406
+ public function evaluate(Context $context): bool
414
407
{
415
408
list($left, $right) = $this->getOperands();
416
409
$value = $right->prepareValue($context)->getValue() * 10;
0 commit comments