Download the PHP package ruler/ruler without Composer
On this page you can find all versions of the php package ruler/ruler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package ruler
Short Description A simple stateless production rules engine for modern PHP.
License MIT
Homepage https://github.com/bobthecow/Ruler
Informations about the package ruler
Ruler
Ruler is a simple stateless production rules engine for PHP 5.3+.
Ruler has an easy, straightforward DSL
... provided by the RuleBuilder:
Of course, if you're not into the whole brevity thing
... you can use it without a RuleBuilder:
But that doesn't sound too fun, does it?
Things you can do with your Ruler
Compare things
Math even more things
Reason about sets
Combine Rules
Combine more Rules
evaluate
and execute
Rules
evaluate()
a Rule with Context to figure out whether it is true.
If a Rule has an action, you can execute()
it directly and save yourself a
couple of lines of code.
Even execute
a whole grip of Rules at once
Dynamically populate your evaluation Context
Several of our examples above use static values for the context Variables. While that's good for examples, it's not as useful in the Real World. You'll probably want to evaluate Rules based on all sorts of things...
You can think of the Context as a ViewModel for Rule evaluation. You provide the static values, or even code for lazily evaluating the Variables needed by your Rules.
Now you have all the information you need to make Rules based on Order count or the current User, or any number of other crazy things. I dunno, maybe this is for a shipping price calculator?
If the current User has placed 5 or more orders, but isn't "really annoying", give 'em free shipping.
Access variable properties
As an added bonus, Ruler lets you access properties, methods and offsets on your Context Variable values. This can come in really handy.
Say we wanted to log the current user's name if they are an administrator:
That was a bit of a mouthful. Instead of creating context Variables for everything we might need to access in a rule, we can use VariableProperties, and their convenient RuleBuilder interface:
If the parent Variable resolves to an object, and this VariableProperty name is "bar", it will do a prioritized lookup for:
- A method named
bar
- A public property named
bar
- ArrayAccess + offsetExists named
bar
If the Variable resolves to an array it will return:
- Array index
bar
If none of the above are true, it will return the default value for this VariableProperty.
Add your own Operators
If none of the default Ruler Operators fit your needs, you can write your own! Just define additional operators like this:
Then you can use them with RuleBuilder like this:
But that's not all...
Check out the test suite for more examples (and some hot CS 320 combinatorial logic action).
Ruler is plumbing. Bring your own porcelain.
Ruler doesn't bother itself with where Rules come from. Maybe you have a RuleManager wrapped around an ORM or ODM. Perhaps you write a simple DSL and parse static files.
Whatever your flavor, Ruler will handle the logic.