This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ExpressionLanguage
2013-09-19 12:59:10 +02:00
..
Node [ExpressionLanguage] added support for regexes 2013-09-19 12:59:10 +02:00
Tests [ExpressionLanguage] added support for regexes 2013-09-19 12:59:10 +02:00
.gitignore [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
CHANGELOG.md [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Compiler.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
composer.json [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Expression.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
ExpressionLanguage.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Lexer.php [ExpressionLanguage] added support for regexes 2013-09-19 12:59:10 +02:00
LICENSE [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Parser.php [ExpressionLanguage] added support for regexes 2013-09-19 12:59:10 +02:00
phpunit.xml.dist [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
README.md [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
SyntaxError.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Token.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
TokenStream.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00

ExpressionLanguage Component

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

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$language = new ExpressionLanguage();

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

echo $language->compile('1 + 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->addFunction('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:

$ cd path/to/Symfony/Component/ExpressionLanguage/
$ composer.phar install --dev
$ phpunit