adachsoft / rule-evaluator-contract
Contract for evaluating boolean rule expressions against a data context.
Package info
gitlab.com/a.adach/rule-evaluator-contract
pkg:composer/adachsoft/rule-evaluator-contract
Requires
- php: ^8.3
Requires (Dev)
- adachsoft/php-code-style: ^0.5.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^13.3
- rector/rector: ^2.6
- symplify/phpstan-rules: ^14.12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/rule-evaluator-contract provides a framework-independent contract for evaluating boolean rule expressions against a data context.
Requirements
- PHP 8.3 or newer
Installation
composer require adachsoft/rule-evaluator-contract
Public API
RuleEvaluatorInterfacedefines the evaluation port implemented by rule engines.RuleExpressionVorepresents a non-empty rule expression without coupling it to a specific expression language.RuleEvaluatorExceptionInterfaceidentifies exceptions raised by rule evaluator implementations.InvalidRuleExpressionExceptionidentifies empty expressions.RuleEvaluationFailedExceptionidentifies evaluation failures and carries the failed expression.
The package has no production dependencies beyond PHP 8.3. Concrete evaluators belong in implementation packages and may use any suitable expression engine, for example adachsoft/rule-evaluator-expression-language.
Usage
Boolean rules
use AdachSoft\RuleEvaluatorContract\RuleEvaluatorInterface;
use AdachSoft\RuleEvaluatorContract\Vo\RuleExpressionVo;
function isAllowed(RuleEvaluatorInterface $evaluator, array $resource): bool
{
return $evaluator->evaluate(new RuleExpressionVo("resource['owner'] == 'alice'"), ['resource' => $resource]);
}
Value expressions
ExpressionEvaluatorInterface defines a contract for validating and evaluating expressions that return arbitrary values. Variable resolvers are queried when an expression needs a variable. Concrete evaluators receive function providers through an implementation-specific mechanism, such as a constructor or factory.
use AdachSoft\RuleEvaluatorContract\Expression\ExpressionEvaluatorInterface;
use AdachSoft\RuleEvaluatorContract\Function\ExpressionFunctionCollection;
use AdachSoft\RuleEvaluatorContract\Function\ExpressionFunctionDto;
use AdachSoft\RuleEvaluatorContract\Function\ExpressionFunctionProviderInterface;
use AdachSoft\RuleEvaluatorContract\Variable\ArrayVariableResolver;
use AdachSoft\RuleEvaluatorContract\Vo\RuleExpressionVo;
final class ReportFunctionProvider implements ExpressionFunctionProviderInterface
{
public function functions(): ExpressionFunctionCollection
{
return new ExpressionFunctionCollection([
new ExpressionFunctionDto(
'number',
'number(value)',
'Converts a value to a number.',
static fn (mixed $value): float => (float) $value,
),
]);
}
}
function calculateReportValue(ExpressionEvaluatorInterface $evaluator): mixed
{
$expression = new RuleExpressionVo('number(quantity) * 2');
$evaluator->validate($expression);
return $evaluator->evaluate(
$expression,
new ArrayVariableResolver(['quantity' => '3']),
);
}
The $evaluator is supplied by a concrete expression engine configured with ReportFunctionProvider; the provider setup API belongs to that implementation.