PHP code example of di / expression-parser
1. Go to this page and download the library: Download di/expression-parser 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/ */
di / expression-parser example snippets
use DI\ExpressionParser\Expression;
$expression = 'or_x(equal([attr1], 1), in_array(explode([keywords]), "hello"))';
$mappings = [
'attr1' => 1,
'keywords' => 'hello,world',
];
$ex = new Expression($expression, $mappings);
echo $ex->value(); // true
new Expression(
'in_array([keywords], "hello")',
[
'keywords' => [
'hello',
'world',
],
]
)
new Expression(
'in_array(explode([keywords]), "hello")',
[
'keywords' => 'hello,world',
]
)
new Expression(
'explode([Rooms], ";")',
[
'Rooms' => 'Pantry;Study',
]
)
new Expression(
'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
new Expression(
'not(or_x(equal([attr1], 1), in_array(explode([attr2]), "word")))',
[
'attr1' => 2,
'attr2' => 'hello,world',
]
)
new Expression(
'first(take(sort(filter([attr1], [filter_func]), [dir]), [offset]))',
[
'attr1' => [
10,
30,
20,
],
'filter_func' => function (ExpressionParser $context, $value) {
return array_filter($value, function ($item) use ($context) {
return $item < $context->getMappings('filter_attr');
});
},
'filter_attr' => 30,
'dir' => 'desc',
'offset' => 1,
]
)