2017 © Pedro Peláez
 

library php-rule-parser

PHP 7 Rule Engine - Rule Parser & Evaluator

image

nicoswd/php-rule-parser

PHP 7 Rule Engine - Rule Parser & Evaluator

  • Friday, June 1, 2018
  • by nicoSWD
  • Repository
  • 5 Watchers
  • 30 Stars
  • 597 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 21 Versions
  • 19 % Grown

The README.md

PHP Rule Engine

Latest Stable Version ![Build status][Master coverage image] ![Code Quality][Master quality image] StyleCI, (*1)

Total Downloads Latest Stable Version, (*2)

You're looking at a standalone PHP library to parse and evaluate text based rules with a Javascript-like syntax. This project was born out of the necessity to evaluate hundreds of rules that were originally written and evaluated in JavaScript, and now needed to be evaluated on the server-side, using PHP., (*3)

This library has initially been used to change and configure the behavior of certain "Workflows" (without changing actual code) in an intranet application, but it may serve a purpose elsewhere., (*4)

Find me on Twitter: @nicoSWD, (*5)

(If you're using PHP 5, you might want to take a look at version 0.4.0), (*6)

SensioLabsInsight, (*7)

Install

Via Composer, (*8)

$ composer require nicoswd/php-rule-parser

Bundles

This library works best with one of these bundles below, but they're not required, (*9)

Bundle Framework Packagist
nicoSWD/rule-engine-bundle Symfony Latest Stable Version

Usage Examples

Test if a value is in a given array, (*10)

$variables = ['foo' => 6];

$rule = new Rule('foo in [4, 6, 7]', $variables);
var_dump($rule->isTrue()); // bool(true)

Simple array manipulation, (*11)

$rule = new Rule('[1, 4, 3].join(".") === "1.4.3"');
var_dump($rule->isTrue()); // bool(true)

Test if a value is between a given range, (*12)

$variables = ['threshold' => 80];

$rule = new Rule('threshold >= 50 && threshold <= 100', $variables);
var_dump($rule->isTrue()); // bool(true)

Call methods on objects from within rules, (*13)

class User
{
    // ...

    public function points(): int
    {
        return 1337;    
    }
}

$variables = [
    'user' => new User(),
];

$rule = new Rule('user.points() > 300', $variables);
var_dump($rule->isTrue()); // bool(true)

For security reasons, PHP's magic methods like __construct and __destruct cannot be called from within rules. However, __call will be invoked automatically if available, unless the called method is defined., (*14)

Built-in Methods

Name Example
charAt "foo".charAt(2) === "o"
concat "foo".concat("bar", "baz") === "foobarbaz"
endsWith "foo".endsWith("oo") === true
startsWith "foo".startsWith("fo") === true
indexOf "foo".indexOf("oo") === 1
join ["foo", "bar"].join(",") === "foo,bar"
replace "foo".replace("oo", "aa") === "faa"
split "foo-bar".split("-") === ["foo", "bar"]
substr "foo".substr(1) === "oo"
test "foo".test(/oo$/) === true
toLowerCase "FOO".toLowerCase() === "foo"
toUpperCase "foo".toUpperCase() === "FOO"

Built-in Functions

Name Example
parseInt parseInt("22aa") === 22
parseFloat parseFloat("3.1") === 3.1

Supported Operators

Type Description Operator
Comparison greater than >
Comparison greater than or equal to >=
Comparison less than <
Comparison less or equal to <=
Comparison equal to ==
Comparison not equal to !=
Comparison identical ===
Comparison not identical !==
Containment contains in
Containment does not contain not in
Logical and &&
Logical or |\

Error Handling

Both, $rule->isTrue() and $rule->isFalse() will throw an exception if the syntax is invalid. These calls can either be placed inside a try / catch block, or it can be checked prior using $rule->isValid()., (*15)

$ruleStr = '
    (2 == 2) && (
        1 < 3 && 3 == 2 ( // Missing and/or before parentheses
            1 == 1
        )
    )';

$rule = new Rule($ruleStr);

try {
    $rule->isTrue();
} catch (\Exception $e) {
    echo $e->getMessage();
}

Or alternatively:, (*16)

if (!$rule->isValid()) {
    echo $rule->getError();
}

Both will output: Unexpected token "(" at position 25 on line 3, (*17)

Syntax Highlighting

A custom syntax highlighter is also provided., (*18)

use nicoSWD\Rule;

$ruleStr = '
    // This is true
    2 < 3 && (
        // This is false
        foo in [4, 6, 7] ||
        // True
        [1, 4, 3].join("") === "143"
    ) && (
        // True
        "foo|bar|baz".split("|" /* uh oh */) === ["foo", /* what */ "bar", "baz"] &&
        // True
        bar > 6
    )';

$highlighter = new Rule\Highlighter\Highlighter(new Rule\Tokenizer());

// Optional custom styles
$highlighter->setStyle(
    Rule\Constants::GROUP_VARIABLE,
    'color: #007694; font-weight: 900;'
);

echo $highlighter->highlightString($ruleStr);

Outputs:, (*19)

Syntax preview, (*20)

Notes

  • Parentheses can be nested, and will be evaluated from right to left.
  • Only value/variable comparison expressions with optional logical ANDs/ORs, are supported.

Security

If you discover any security related issues, please email security@nic0.me instead of using the issue tracker., (*21)

Testing

$ composer test

Contributing

Pull requests are very welcome! If they include tests, even better. This project follows PSR-2 coding standards, please make sure your pull requests do too., (*22)

To Do

  • Support for object properties (foo.length)
  • Support for returning actual results, other than true or false
  • Support for array / string dereferencing: "foo"[1]
  • Don't force boolean comparison for tokens that are already booleans. my_func() && 2 > 1 should work
  • Change regex and implementation for method calls. ".split(" should not be the token
  • Add / implement missing methods
  • Add "typeof" construct
  • Do math (?)
  • Allow string concatenating with "+"
  • Invalid regex modifiers should not result in an unknown token
  • Duplicate regex modifiers should throw an error
  • ~~Add support for function calls~~
  • ~~Support for regular expressions~~
  • ~~Fix build on PHP 7 / Nightly~~
  • ~~Allow variables in arrays~~
  • ~~Verify function and method name spelling (.tOuPpErCAse() is currently valid)~~
  • ...

License

License, (*23)

The Versions

01/06 2018

dev-master

9999999-dev https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Engine - Rule Parser & Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript engine dsl workflow tokenizer evaluator rule highlighter

30/10 2017

dev-analysis-8AB11Z

dev-analysis-8AB11Z https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Engine - Rule Parser & Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript engine dsl workflow tokenizer evaluator rule highlighter

13/10 2017

0.5.7

0.5.7.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/10 2017

0.5.6

0.5.6.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/10 2017

0.5.5

0.5.5.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/10 2017

0.5.4

0.5.4.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/10 2017

0.5.3

0.5.3.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/02 2017

v0.3.x-dev

0.3.9999999.9999999-dev https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/02 2017

0.5.2

0.5.2.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

13/02 2017

0.5.1

0.5.1.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

02/02 2017

0.5.0

0.5.0.0 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

02/02 2017

dev-php7

dev-php7 https://github.com/nicoSWD/php-rule-parser

PHP 7 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser javascript dsl workflow tokenizer evaluator rule highlighter

15/02 2016

0.4.0

0.4.0.0 https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

29/08 2015

dev-develop

dev-develop https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

21/08 2015

v0.3.5

0.3.5.0 https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

10/08 2015

v0.3.4

0.3.4.0 https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

25/07 2015

v0.3.3

0.3.3.0 https://github.com/nicoSWD/php-rule-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

23/07 2015

0.3.2.1

0.3.2.1 https://github.com/nicoSWD/php-rules-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

23/07 2015

0.3.2

0.3.2.0 https://github.com/nicoSWD/php-rules-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript dsl workflow tokenizer evaluator rule highlighter

22/07 2015

0.3.1

0.3.1.0 https://github.com/nicoSWD/php-rules-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript workflow tokenizer evaluator rule highlighter

22/07 2015

0.3.0

0.3.0.0 https://github.com/nicoSWD/php-rules-parser

PHP 5.4 Rule Parser and Evaluator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

parser javascript workflow tokenizer evaluator rule highlighter