zef-dev / zef-expression-language
A JUEL like extension for Symfony Expression Language
Installs: 1 357
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 2
Open Issues: 1
Requires
- php: ^7.2.5
- psr/cache: ^1.0.1
- symfony/expression-language: ^5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2025-03-17 16:15:56 UTC
README
A simple extension to the Symfony Expression Language which enables more JUEL like behaviour.
- it will not break on non existing values - it will evaluate to
null
- it enables you to use JUEL style accessing to array values and object methods (dot notation)
Installation
# With composer
$ composer require zef-dev/zef-expression-language
Usage
Just replace Symfony ExpressionLanguage
with the Zef one.
use Zef\Zel\Symfony\ExpressionLanguage; $expressionLanguage = new ExpressionLanguage(); $evaluated = $expressionLanguage->evaluate( 'true', []); // will not throw an exception any more $evaluated = $expressionLanguage->evaluate( 'myvar', []); $evaluated = $expressionLanguage->evaluate( 'myvar[\'myfield\']', ['myvar'=>[]]);
JUEL like usage
To gain JUEL like evaluation addon, wrap your values array in ArrayResolver
.
use Zef\Zel\Symfony\ExpressionLanguage; use Zef\Zel\ArrayResolver; $expressionLanguage = new ExpressionLanguage(); $resolver = new ArrayResolver([]); $evaluated = $expressionLanguage->evaluate( 'true', $resolver->getValues()); // now you can access array fields in dot notation $resolver = new ArrayResolver(['myvar'=>['myfield'=>true]]); $evaluated = $expressionLanguage->evaluate( 'myvar.myfield', $resolver->getValues()); // now you can access getters in a shorter way $obj = new Myclass(); $obj->getName(); $obj->isValid(); $obj->hasErrror(); $resolver = new ArrayResolver(['myvar'=>$obj]); $evaluated = $expressionLanguage->evaluate( 'myvar.name', $resolver->getValues()); $evaluated = $expressionLanguage->evaluate( 'myvar.valid', $resolver->getValues()); $evaluated = $expressionLanguage->evaluate( 'myvar.error', $resolver->getValues());
Note about implementation
We wanted to extend Symfony classes and override just what was necessary, but as the original classes were not written in a manner to allow it easily, we ended up copy/pasting some classes completely just to be able implement few minor modifications.
This package is created based on the PHP Boilerplate