2017 © Pedro Peláez
 

library ruling

An stateless rule engine

image

subzeta/ruling

An stateless rule engine

  • Thursday, December 7, 2017
  • by subzeta
  • Repository
  • 1 Watchers
  • 12 Stars
  • 79 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 5 Versions
  • 11 % Grown

The README.md

Rule engine

Stateless rule engine to assert statements given a context., (*1)

Install

$ composer require "subzeta/ruling" : "^2.0"

Usage example


require '/path/to/vendor/autoload.php'; use subzeta\Ruling\Ruling; $my = new \stdClass(); $my->sensitivity = 80; $my->joyfulness = 10; (new Ruling()) ->given([ 'sensitivity' => $my->sensitivity, 'joyfulness' => $my->joyfulness ])->when( ':sensitivity is greater than 90 or :joyfulness is less than 20' )->then(function() { echo 'Hell yeah, I should listen music right now!'; })->otherwise(function() { echo 'I\'m happy enough, thanks.'; })->execute(); // Outputs: Hell yeah, I should listen music right now!

Calls

There are three main entrances:, (*2)

interpret

Returns the interpreted rules. Using the example above the output would be: ['80 > 90 || 10 < 20'], (*3)

assert

Returns a boolean indicating the output. Using the example above the output would be: true, (*4)

execute

Fires the success or fail callback if defined. Using the example above the output would be: 'Hell yeah, I should listen it!', (*5)

Error handling

Different types of exceptions are thrown when something goes wrong:, (*6)

InvalidContextException

When the provided context isn't valid (accepts: ['string-key-1' => value-1, ..., 'string-key-x' => value-x])., (*7)

InvalidRuleException

When the provided rules aren't valid (accepts: 'string' or ['string-1', ..., 'string-x']), (*8)

InvalidCallbackException

When the provided success/fail callback isn't callable (accepts: function(){return 'Hey Ho! Let\'s go!';}), (*9)

Supported Operators

Type Operator Representation
Comparison is greater than >
Comparison is greater or equal to >=
Comparison is less than <
Comparison is less or equal to <=
Comparison is equal to (alias: is) ==
Comparison is not equal to (aliases: is not, isn't) !=
Comparison same as ===
Comparison not same as !==
Logical and &&
Logical or |\
Containment contained in (alias: in) in

Notes

  • It's not necessary to provide callbacks for execute method, it will return a boolean instead as assert does.
  • Rules respect the operator precedence and evaluate the parenthesis from right to left.

Testing

$ phpunit

Recursive to do list

  • Increase the number of unit tests to prevent bad contexts or bad formatted rules from being executed.

To do

  • Improve the interpreted method response.

Changelist

  • Allow aliases ("is equal to" can be written as "is" and "is not equal to" as "is not"/"isn't").
  • Context values may permit callable functions too.
  • Added the strict comparison operators (same as, not same as).
  • It can be interesting to implement a kind of dump method to show the interpreted rule.
  • Added the "in" operator.
  • Context accepts array values.

License

MIT, (*10)

The Versions

07/12 2017

dev-master

9999999-dev https://github.com/subzeta/ruling

An stateless rule engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergio Cadu

assert engine ruler rule rule engine ruling rule eval

07/12 2017

v2.1

2.1.0.0 https://github.com/subzeta/ruling

An stateless rule engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergio Cadu

assert engine ruler rule rule engine ruling rule eval

07/12 2017

v2.0

2.0.0.0 https://github.com/subzeta/ruling

An stateless rule engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergio Cadu

assert engine ruler rule rule engine ruling rule eval

29/01 2016

v1.1.0

1.1.0.0 https://github.com/subzeta/ruling

An stateless rule engine

  Sources   Download

The Requires

 

The Development Requires

by Sergio Cadu

assert engine ruler rule rule engine ruling rule eval

24/01 2016

v1.0.0

1.0.0.0 https://github.com/subzeta/ruling

An stateless rule engine

  Sources   Download

The Requires

 

The Development Requires

by Sergio Cadu

assert engine ruler rule rule engine ruling rule eval