Search by

adachsoft / rule-evaluator-contract

Arkadiusz Adach

Contract for evaluating boolean rule expressions against a data context.

Package info

gitlab.com/a.adach/rule-evaluator-contract

Issues

pkg:composer/adachsoft/rule-evaluator-contract

Statistics

Installs: 203

Dependents: 5

Suggesters: 0

Stars: 0

v0.2.0 2026-11-23 13:29 UTC

This package is auto-updated.

Last update: 2026-11-23 12:30:12 UTC


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

  • RuleEvaluatorInterface defines the evaluation port implemented by rule engines.
  • RuleExpressionVo represents a non-empty rule expression without coupling it to a specific expression language.
  • RuleEvaluatorExceptionInterface identifies exceptions raised by rule evaluator implementations.
  • InvalidRuleExpressionException identifies empty expressions.
  • RuleEvaluationFailedException identifies 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.