Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jun 27, 2022
2 parents 5e78cc3 + 9f3e2b9 commit fd3f0d0
Show file tree
Hide file tree
Showing 128 changed files with 2,095 additions and 2,560 deletions.
45 changes: 0 additions & 45 deletions .github/workflows/hhvm.yml

This file was deleted.

6 changes: 5 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ jobs:

strategy:
matrix:
php-version: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.4']
php-version:
- '7.4'
- '8.0'
- '8.1'
# - '8.2'

steps:
- uses: actions/checkout@v3
Expand Down
21 changes: 21 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
risky: true

preset: symfony

enabled:
- align_double_arrow
- native_function_invocation
- ordered_use
- strict

disabled:
- native_function_invocation_symfony
- no_superfluous_phpdoc_tags_symfony
- pow_to_exponentiation
- pre_increment
- unalign_double_arrow
- yoda_style

finder:
name:
- "*.php"
47 changes: 20 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Ruler is a simple stateless production rules engine for PHP 5.3+.

[![Package version](http://img.shields.io/packagist/v/ruler/ruler.svg?style=flat-square)](https://packagist.org/packages/ruler/ruler)
[![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)
[![StyleCI](https://styleci.io/repos/1906921/shield)](https://styleci.io/repos/1906921)

Ruler has an easy, straightforward DSL
--------------------------------------
Expand All @@ -23,13 +24,11 @@ $rule = $rb->create(
}
);

$context = new Context(array(
$context = new Context([
'minNumPeople' => 5,
'maxNumPeople' => 25,
'actualNumPeople' => function() {
return 6;
},
));
'actualNumPeople' => fn() => 6,
]);

$rule->execute($context); // "Yay!"
```
Expand All @@ -42,22 +41,20 @@ $rule->execute($context); // "Yay!"
```php
$actualNumPeople = new Variable('actualNumPeople');
$rule = new Rule(
new Operator\LogicalAnd(array(
new Operator\LogicalAnd([
new Operator\LessThanOrEqualTo(new Variable('minNumPeople'), $actualNumPeople),
new Operator\GreaterThanOrEqualTo(new Variable('maxNumPeople'), $actualNumPeople)
)),
]),
function() {
echo 'YAY!';
}
);

$context = new Context(array(
$context = new Context([
'minNumPeople' => 5,
'maxNumPeople' => 25,
'actualNumPeople' => function() {
return 6;
},
));
'actualNumPeople' => fn() => 6,
]);

$rule->execute($context); // "Yay!"
```
Expand Down Expand Up @@ -165,10 +162,10 @@ $eitherOne = $rb->create($rb->logicalOr($aEqualsB, $aDoesNotEqualB));

// Just to mix things up, we'll populate our evaluation context with completely
// random values...
$context = new Context(array(
$context = new Context([
'a' => rand(),
'b' => rand(),
));
]);

// Hint: this is always true!
$eitherOne->evaluate($context);
Expand All @@ -190,11 +187,9 @@ $rb->logicalXor($aEqualsB, $aDoesNotEqualB); // True if only one condition is tr
`evaluate()` a Rule with Context to figure out whether it is true.

```php
$context = new Context(array(
'userName' => function() {
return isset($_SESSION['userName']) ? $_SESSION['userName'] : null;
}
));
$context = new Context([
'userName' => fn() => $_SESSION['userName'] ?? null,
]);

$userIsLoggedIn = $rb->create($rb['userName']->notEqualTo(null));

Expand Down Expand Up @@ -238,7 +233,7 @@ $hiEveryoneElse = $rb->create(
}
);

$rules = new RuleSet(array($hiJustin, $hiJon, $hiEveryoneElse));
$rules = new RuleSet([$hiJustin, $hiJon, $hiEveryoneElse]);

// Let's add one more Rule, so non-authenticated users have a chance to log in
$redirectForAuthentication = $rb->create($rb->logicalNot($userIsLoggedIn), function() {
Expand Down Expand Up @@ -271,12 +266,10 @@ Rules.
$context = new Context;

// Some static values...
$context['reallyAnnoyingUsers'] = array('bobthecow', 'jwage');
$context['reallyAnnoyingUsers'] = ['bobthecow', 'jwage'];

// You'll remember this one from before
$context['userName'] = function() {
return isset($_SESSION['userName']) ? $_SESSION['userName'] : null;
};
$context['userName'] = fn() => $_SESSION['userName'] ?? null;

// Let's pretend you have an EntityManager named `$em`...
$context['user'] = function() use ($em, $context) {
Expand Down Expand Up @@ -333,7 +326,7 @@ $context['userRoles'] = function() use ($em, $context) {
return $user->roles();
} else {
// return a default "anonymous" role if there is no current user
return array('anonymous');
return ['anonymous'];
}
};

Expand Down Expand Up @@ -365,7 +358,7 @@ their convenient RuleBuilder interface:
// We can skip over the Context Variable building above. We'll simply set our,
// default roles on the VariableProperty itself, then go right to writing rules:

$rb['user']['roles'] = array('anonymous');
$rb['user']['roles'] = ['anonymous'];

$rb->create(
$rb->logicalAnd(
Expand Down Expand Up @@ -410,7 +403,7 @@ use Ruler\Value;

class ALotGreaterThan extends VariableOperator implements Proposition
{
public function evaluate(Context $context)
public function evaluate(Context $context): bool
{
list($left, $right) = $this->getOperands();
$value = $right->prepareValue($context)->getValue() * 10;
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "ruler/ruler",
"description": "A simple stateless production rules engine for PHP 5.3.",
"description": "A simple stateless production rules engine for modern PHP.",
"keywords": ["rules", "engine"],
"homepage": "https://github.com/bobthecow/Ruler",
"license": "MIT",
"require": {
"php": ">=5.3.0"
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "4.8.35 | ^5.6.3 | ^6.5"
"phpunit/phpunit": "^8.5.12 | ^9"
},
"autoload": {
"psr-4": {
"Ruler\\": "src/Ruler"
"Ruler\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Ruler\\Test\\": "tests/Ruler/Test"
"Ruler\\Test\\": "tests/"
}
},
"authors": [
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Ruler Test Suite">
<directory suffix="Test.php">./tests/Ruler/Test/</directory>
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/Ruler/</directory>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit fd3f0d0

Please sign in to comment.