adachsoft / rule-evaluator-expression-language
Symfony ExpressionLanguage implementation of adachsoft/rule-evaluator-contract.
Package info
gitlab.com/a.adach/rule-evaluator-expression-language
pkg:composer/adachsoft/rule-evaluator-expression-language
Requires
- php: ^8.3
- adachsoft/rule-evaluator-contract: ^0.2
- symfony/expression-language: ^7.1 || ^8.0
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
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