systopia/expression-language-ext

Extension for the Symfony ExpressionLanguage Component

v0.1.0 2023-10-20 14:09 UTC

This package is auto-updated.

Last update: 2024-06-13 12:49:50 UTC


README

This is an extension for Symfony's ExpressionLanguage component. It provides the functions date_create and map as well as different PHP functions. For simplified use all those functions are available in the class SystopiaExpressionLanguage.

date_create Function

The date_create function creates an object of type \DateTimeImmutable by using the default constructor.

Example:

$expressionLanguage = new SystopiaExpressionLanguage();
$dateTime = $expressionLanguage->evaluate('date_create("2000-01-02 03:04:05")');

map Function

The function map allows to apply an expression to the values of an array (actually any iterable). Each pair of key and value are provided as variables named key and value to the expression.

Example:

$array = [
    'x' => (object) ['a' => 1, 'b' => 2],
    'y' => (object) ['a' => 3, 'b' => 4],
];

$expressionLanguage = new SystopiaExpressionLanguage();
$mapped = $expressionLanguage->evaluate(
    'map(array, "key ~ \": \" ~ (value.a + value.b)")',
     ['array' => $array]
);

var_dump($mapped);

Output:

array(2) {
  [0]=>
  string(4) "x: 3"
  [1]=>
  string(4) "y: 7"
}