xavren/expression-language

Symfony ExpressionLanguage Component Fork

dev-master 2015-10-15 15:58 UTC

This package is auto-updated.

Last update: 2024-04-10 20:49:07 UTC


README

The ExpressionLanguage component provides an engine that can compile and evaluate expressions:

use Hydra\Component\ExpressionLanguage\ExpressionLanguage;

$language = new ExpressionLanguage();

echo $language->evaluate('1 + foo', array('foo' => 2));
// would output 3

echo $language->compile('1 + foo', array('foo'));
// would output (1 + $foo)

By default, the engine implements simple math and logic functions, method calls, property accesses, and array accesses.

You can extend your DSL with functions:

$compiler = function ($arg) {
    return sprintf('strtoupper(%s)', $arg);
};
$evaluator = function (array $variables, $value) {
    return strtoupper($value);
};
$language->register('upper', $compiler, $evaluator);

echo $language->evaluate('"foo" ~ upper(foo)', array('foo' => 'bar'));
// would output fooBAR

echo $language->compile('"foo" ~ upper(foo)');
// would output ("foo" . strtoupper($foo))

Resources

You can run the unit tests with the following command:

$ composer.phar install --dev
$ phpunit