PHP code example of uuf6429 / expression-language-arrowfunc

1. Go to this page and download the library: Download uuf6429/expression-language-arrowfunc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uuf6429 / expression-language-arrowfunc example snippets

 no-test
   $el = new ExpressionLanguage();
   $el->addFunction(new ExpressionFunction(
       'array_map',
       static fn ($callback, $array) => sprintf('\array_map(%s, %s)', $callback, $array),
       static fn (array $variables, callable $callback, array $array) => array_map($callback, $array),
   ));
   
   // Intended expression
   $result = $el->evaluate('array_map([" aa "], "trim")');  // => ['aa']
   
   // Malicious expression
   $result = $el->evaluate('array_map([123], "exit")');     // 💥 application exits with code 123
   
 no-test
   $el = new ExpressionLanguage();
   
   // Intended expression
   $filter = $el->evaluate('(value) -> { value > 20 }');
   $values = array_filter([18, 23, 40], $filter);           // => [23, 49]
   
   // Malicious expression
   $filter = $el->evaluate('"exit"');
   $values = array_filter([18, 23, 40], $filter);           // 💥 application exits with code 18
   

use uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunctions;

$el = new ExpressionLanguageWithArrowFunctions();

$phpCode = $el->compile('(val) -> { val * 2 }', []);
assert($phpCode === 'function ($val) { return ($val * 2); }');

use Symfony\Component\ExpressionLanguage\ExpressionLanguage as SymfonyExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ParsedExpression as SymfonyParsedExpression;
use uuf6429\ExpressionLanguage\ArrowFunctionTrait;

class MyCustomExpressionLanguage
{
    use ArrowFunctionTrait;

    public function evaluate($expression, $values = [])
    {
        return $this->evaluateWithArrowFunctions($expression, $values);
    }

    public function compile($expression, $names = [])
    {
        return $this->compileWithArrowFunctions($expression, $names);
    }

    protected function compileWithoutArrowFunctions($expression, array $names = []): string
    {
        // TODO Implement method
    }

    protected function evaluateWithoutArrowFunctions($expression, array $values = [])
    {
        // TODO Implement method
    }

    protected function parseWithoutArrowFunctions($expression, array $names = []): SymfonyParsedExpression
    {
        // TODO Implement method
    }

    protected function lintWithoutArrowFunctions($expression, array $names = []): void
    {
        // TODO Implement method
    }
}

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunctions;
use uuf6429\ExpressionLanguage\SafeCallable;

$el = new ExpressionLanguageWithArrowFunctions();

// Expose array_map() as map()
$el->addFunction(new ExpressionFunction(
   'map',
   static fn (string $callbackExpr, string $arrayExpr) => sprintf('\array_map(%s->getCallback(), %s)', $callbackExpr, $arrayExpr),
   static fn (array $variables, SafeCallable $callback, array $array) => array_map($callback->getCallback(), $array),
));

// Compiling
$phpCode = $el->compile('map((val) -> { val * 2 }, values)', ['values']);
assert($phpCode === '\array_map(function ($val) { return ($val * 2); }->getCallback(), $values)');

// Evaluating
$result = $el->evaluate('map((val) -> { val * 2 }, values)', ['values' => [1, 2, 3]]);
assert($result === [2, 4, 6]);