You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
10 years ago | |
|---|---|---|
| .. | ||
| Node | 10 years ago | |
| ParserCache | 10 years ago | |
| Resources/bin | 10 years ago | |
| Tests | 10 years ago | |
| .gitignore | 10 years ago | |
| CHANGELOG.md | 10 years ago | |
| Compiler.php | 10 years ago | |
| Expression.php | 10 years ago | |
| ExpressionFunction.php | 10 years ago | |
| ExpressionFunctionProviderInterface.php | 10 years ago | |
| ExpressionLanguage.php | 10 years ago | |
| LICENSE | 10 years ago | |
| Lexer.php | 10 years ago | |
| ParsedExpression.php | 10 years ago | |
| Parser.php | 10 years ago | |
| README.md | 10 years ago | |
| SerializedParsedExpression.php | 10 years ago | |
| SyntaxError.php | 10 years ago | |
| Token.php | 10 years ago | |
| TokenStream.php | 10 years ago | |
| composer.json | 10 years ago | |
| phpunit.xml.dist | 10 years ago | |
README.md
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