2017 © Pedro Peláez
 

library php-expression

PHP Expression language

image

kitano/php-expression

PHP Expression language

  • Monday, September 2, 2013
  • by Kitano
  • Repository
  • 5 Watchers
  • 32 Stars
  • 1,147 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP Expression Language

The purpose of this library is to provide a common base for an PHP Expression Language., (*1)

This is not really a creative library since it burrows almost all the code from the great JMSSecurityExtraBundle which already defines more or less such a base for a powerful EL (Expression Language)., (*2)

The idea is to take this code outside of the Johannes's bundle and to standardize it., (*3)

Simple usage

$compiler = new ExpressionCompiler();
$evaluator = eval($compiler->compileExpression(new Expression("date.format(format)")));

$context = array(
    'date' => new \DateTime(),
    'format' => 'Y',
);
$result = $evaluator($context);

echo $result; // 2013

Adding a custom function compiler

The isNumber() function expression:, (*4)

  1. First you need to create a compiler for your function, (*5)

    <?php
    
    namespace My\Expression\Compiler\Func;
    
    use Pel\Expression\Compiler\Func\FunctionCompilerInterface;
    use Pel\Expression\ExpressionCompiler;
    use Pel\Expression\Ast\FunctionExpression;
    use Pel\Exception\RuntimeException;
    
    class IsNumberFunctionCompiler implements FunctionCompilerInterface
    {
        public function getName()
        {
            return 'isNumber';
        }
    
        public function compilePreconditions(ExpressionCompiler $compiler, FunctionExpression $function)
        {
            if (1 !== count($function->args)) {
                throw new RuntimeException(sprintf('The isNumber() function expects exactly one argument, but got "%s".', var_export($function->args, true)));
            }
        }
    
        public function compile(ExpressionCompiler $compiler, FunctionExpression $function)
        {
            $compiler
                ->write("is_numeric(")
                ->compileInternal($function->args[0])
                ->write(")")
            ;
        }
    }
    
  2. Next, after having instanciated the ExpressionCompiler, you just need to register your custom function compiler, (*6)

    <?php
    
    $compiler = new ExpressionCompiler();
    $compiler->addFunctionCompiler(new IsNumberFunctionCompiler());
    
    $evaluator = eval($compiler->compileExpression(new Expression("isNumber('1234')")));
    var_dump(call_user_func($evaluator, array()));
    // bool(true)
    
    $evaluator = eval($compiler->compileExpression(new Expression("isNumber('1234abc')")));
    var_dump(call_user_func($evaluator, array()));
    // bool(false)
    

License

This bundle is under the MIT license. See the complete license in library:, (*7)

LICENSE

The Versions

02/09 2013

dev-master

9999999-dev

PHP Expression language

  Sources   Download

MIT

The Requires

 

The Development Requires

expression expressions expression language el