Hoa is a modular, extensible and
structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.
This library allows to manipulate a rule engine. Rules can be written by using a dedicated language, very close to SQL. Therefore, they can be written by a user and saved in a database.
Such rules are useful, for example, for commercial solutions that need to manipulate promotion or special offer rules written by a user. To quote Wikipedia:
A business rules engine is a software system that executes one or more business rules in a runtime production environment. The rules might come from legal regulation (“An employee can be fired for any reason or no reason but not for an illegal reason”), company policy (“All customers that spend more than $100 at one time will receive a 10% discount”), or other sources. A business rule system enables these company policies and other operational decisions to be defined, tested, executed and maintained separately from application code.
With Composer, to include this library into
your dependencies, you need to
require hoa/ruler:
$ composer require hoa/ruler '~2.0'For more installation procedures, please read the Source page.
Before running the test suites, the development dependencies must be installed:
$ composer installThen, to run all the test suites:
$ vendor/bin/hoa test:runFor more information, please read the contributor guide.
As a quick overview, we propose to see a very simple example that manipulates a simple rule with a simple context. After, we will add a new operator in the rule. And finally, we will see how to save a rule in a database.
So first, we create a context with two variables: group and points, and we
then assert a rule. A context holds values to concretize a rule. A value can
also be the result of a callable. Thus:
$ruler = new Hoa\Ruler\Ruler();
// 1. Write a rule.
$rule = 'group in ["customer", "guest"] and points > 30';
// 2. Create a context.
$context = new Hoa\Ruler\Context();
$context['group'] = 'customer';
$context['points'] = function () {
return 42;
};
// 3. Assert!
var_dump(
$ruler->assert($rule, $context)
);
/**
* Will output:
* bool(true)
*/In the next example, we have a User object and a context that is populated
dynamically (when the user variable is concretized, two new variables, group
and points are created). Moreover, we will create a new operator/function
called logged. There is no difference between an operator and a function
except that an operator has two operands (so arguments).
For now, we have the following operators/functions by default: and, or,
xor, not, = (is as an alias), !=, >, >=, <, <=, in and