3b837dc5c1
* 2.5: [Translator] Use quote to surround invalid locale [Validator] Fixed memory leak in ValidatorBuilder [FrameworkBundle] changed KernelTestCase::getKernelClass() to check $_SERVER['KERNEL_DIR'] before invoking getPhpUnitXmlDir() Optimize assertLocale regexp [ExpressionLanguage] Fixed an issue with # characters in double quoted string literals [Validator] Fixed object initializers in 2.5 version of the Validator Add some tweaks to the pt_BR translations [Validator] Backported #11410 to 2.3: Object initializers are called only once per object [Translator][FrameworkBundle] Added @ to the list of allowed chars in Translator [Process] Reduce I/O load on Windows platform [Form] Check if IntlDateFormatter constructor returned a valid object before using it Conflicts: src/Symfony/Component/Process/ProcessPipes.php |
||
---|---|---|
.. | ||
Node | ||
ParserCache | ||
Resources/bin | ||
Tests | ||
.gitignore | ||
CHANGELOG.md | ||
Compiler.php | ||
composer.json | ||
Expression.php | ||
ExpressionLanguage.php | ||
Lexer.php | ||
LICENSE | ||
ParsedExpression.php | ||
Parser.php | ||
phpunit.xml.dist | ||
README.md | ||
SerializedParsedExpression.php | ||
SyntaxError.php | ||
Token.php | ||
TokenStream.php |
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', 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:
$ cd path/to/Symfony/Component/ExpressionLanguage/
$ composer.phar install --dev
$ phpunit