PHP Expression Language

Overview

PHP Expression Language

The purpose of this library is to provide a common base for an PHP Expression Language.

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).

The idea is to take this code outside of the Johannes's bundle and to standardize it.

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:

  1. First you need to create a compiler for your function

    <?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

    <?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:

LICENSE
You might also like...
A high-level machine learning and deep learning library for the PHP language.

Rubix ML A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Zephir is a compiled high level language aimed to the creation of C-extensions for PHP.

Zephir - is a high level programming language that eases the creation and maintainability of extensions for PHP. Zephir extensions are exported to C c

Simple custom chat bot developing framework for telegram, qq and more in PHP (the best language)
Simple custom chat bot developing framework for telegram, qq and more in PHP (the best language)

RinoBot RinoBot 是一个为统一聊天机器人扩展开发的框架,编写一份插件用于多种机器人协议。 简体中文 | English 🚧 开发中 🚧 暂不适用于生产环境 特性 插件扩展机制 一份代码运行于多平台多协议机器人 并减小开发难度 插件提供 Yaml 配置 供使用者修改 基于机器人 We

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform. It allows you to hear the sound

Rubix ML - A high-level machine learning and deep learning library for the PHP language.

A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Bearer client for the PHP programming language

Bearer PHP Client This is the official PHP client for interacting with Bearer.sh. Installation Install the package by running: composer require bearer

This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database.
This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database.

PHP basic template This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database. Running To

An extension for PHPStan for adding analysis for PHP Language Extensions.

PHPStan PHP Language Extensions (currently in BETA) This is an extension for PHPStan for adding analysis for PHP Language Extensions. Language feature

Attributes to define PHP language extensions (to be enforced by static analysis)

PHP Language Extensions (currently in BETA) This library provides attributes for extending the PHP language (e.g. adding package visibility). The inte

Comments
  • partial variable names are not handled correctly

    partial variable names are not handled correctly

    the ConcatExpressionCompiler works with this syntax:

    • "Hello " ~ name ~ "!"
    • myVar ~ "abc"

    but doesn't support syntax where you have a variable that's has the same name but with some other suffix:

    • myVar ~ myVar2
    • myVar ~ "" ~ myVar2
    //concat
    echo $e = 'myVar ~ myVar2';
    echo "<br/>\r\n";
    $evaluator = eval($compiler->compileExpression(new Expression($e)));
    $context = array(
        'myVar' => 'abc',
        'myVar2' => '123'
    );
    

    When we change myVar2 to something else it starts working. Further on our investigation, it might the number that causes the problem. In this case a special exception should be thrown to notify the developer that the variable names are not language standards.

    @Kitano @adrienbrault How this should work?

    opened by pentium10 0
Owner
KitanoLabs
KitanoLabs
Arithmetic expression solver PHP library

arithmexp An arithmetic expression solver Usage $operators = OperatorRegistry::default(); $expression = new ArithmeticExpression($operators, "4 - 3 +

Muqsit Rayyan 17 Nov 13, 2022
Best regular expression for gmail

best regular expression for gmail Gmail Regular expression with all details (not start with dot,number , is it possible to use multiple dot but not in

null 3 Feb 2, 2022
PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

null 464 Dec 28, 2022
A repository for showcasing my knowledge of the PHP programming language, and continuing to learn the language.

Learning PHP (programming language) I know very little about PHP. This document will list all my knowledge of the PHP programming language. Basic synt

Sean P. Myrick V19.1.7.2 2 Oct 29, 2022
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

Felix Becker 1.1k Jan 4, 2023
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP

Hprose 3.0 for PHP Introduction Hprose is a High Performance Remote Object Service Engine. It is a modern, lightweight, cross-language, cross-platform

Hprose 2k Jan 4, 2023
Simple customizable captcha script for bot prevention in php language.

phpCaptcha Simple customizable captcha script for bot prevention in php language. Usage <?php session_start(); $status = ""; if ($_SESSION['captcha']

Филип Арсовски 11 Oct 10, 2022