Search by

adachsoft / rule-evaluator-expression-language

Arkadiusz Adach

Symfony ExpressionLanguage implementation of adachsoft/rule-evaluator-contract.

Package info

gitlab.com/a.adach/rule-evaluator-expression-language

Issues

pkg:composer/adachsoft/rule-evaluator-expression-language

Statistics

Installs: 146

Dependents: 8

Suggesters: 2

Stars: 0

v0.2.0 2026-11-23 14:21 UTC

This package is auto-updated.

Last update: 2026-11-23 13:21:53 UTC


README

Symfony ExpressionLanguage implementation of the adachsoft/rule-evaluator-contract package.

Requirements

  • PHP 8.3 or higher
  • Symfony ExpressionLanguage 7.1 or 8.x

Installation

composer require adachsoft/rule-evaluator-expression-language

Usage

Create an evaluator through the factory:

use AdachSoft\RuleEvaluatorContract\Vo\RuleExpressionVo;
use AdachSoft\RuleEvaluatorExpressionLanguage\ExpressionLanguageRuleEvaluatorFactory;

$evaluator = (new ExpressionLanguageRuleEvaluatorFactory())->createDefault();

$isEligible = $evaluator->evaluate(
    new RuleExpressionVo("data['order_total'] > 100"),
    ['data' => ['order_total' => 150]],
);

The evaluator returns a boolean. Syntax errors, evaluation failures, and expressions that return a non-boolean value are reported as RuleEvaluationFailedException.

Expression syntax

Expressions use the native Symfony ExpressionLanguage syntax. Access nested array values with bracket notation, for example:

new RuleExpressionVo("data['user']['role'] == 'admin'");

Use ['key'] to access array values. Dot notation such as data.user.role is not the syntax for nested arrays.

Both word and symbolic logical operators are supported:

data['active'] and data['enabled']
data['active'] && data['enabled']

Custom ExpressionLanguage configuration

Use create() when the application needs custom functions or a configured ExpressionLanguage instance:

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->register(
    'is_admin',
    static fn (string $role): bool => $role === 'admin',
    static fn (string $role): string => sprintf('(%s === "admin")', $role),
);

$evaluator = (new ExpressionLanguageRuleEvaluatorFactory())->create($expressionLanguage);

The factory is the public creation API; the concrete evaluator is an internal implementation detail.

Value expressions

Use createExpressionEvaluator() for expressions that return arbitrary values. Functions are made available only through ExpressionFunctionProviderInterface providers; Symfony's built-in constant() and enum() functions are not enabled.

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;
use AdachSoft\RuleEvaluatorExpressionLanguage\ExpressionLanguageRuleEvaluatorFactory;

$taxFunctions = new class implements ExpressionFunctionProviderInterface {
    public function functions(): ExpressionFunctionCollection
    {
        return new ExpressionFunctionCollection([
            new ExpressionFunctionDto(
                'tax',
                'tax(amount, rate)',
                'Calculates tax for an amount.',
                static fn (float $amount, float $rate): float => $amount * $rate,
            ),
        ]);
    }
};

$evaluator = (new ExpressionLanguageRuleEvaluatorFactory())
    ->createExpressionEvaluator([$taxFunctions]);

$priceIncludingTax = $evaluator->evaluate(
    new RuleExpressionVo('price + tax(price, 0.2)'),
    new ArrayVariableResolver(['price' => 100.0]),
); // 120.0