This package allows to evaluate (parse with mapping) large amounts of data in flexible manner, providing various processing functions
di/expression-parser is a Laravel package for this package allows to evaluate (parse with mapping) large amounts of data in flexible manner, providing various processing functions.
It currently has 12 GitHub stars and 8 downloads on Packagist (latest version 0.2.2).
Install it with composer require di/expression-parser.
Discover more Laravel packages by di
or browse all Laravel packages to compare alternatives.
Last updated
This package allows to evaluate (parse with mapping) large amounts of data in flexible manner, providing various processing functions:
composer install di/expression-parser
// Signature
$expression = new Expression(string $expression[, array $mappings = []]);
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
π₯ Input:
new Expression(
'[attr1]',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: 1
has() functionπ₯ Input:
new Expression(
'has([attr1])',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: true
in_array() function and scalar valueπ₯ Input:
new Expression(
'in_array([keywords], "hello")',
[
'keywords' => [
'hello',
'world',
],
]
)
π€ Output: true
in_array() and explode() function and scalar valueπ₯ Input:
new Expression(
'in_array(explode([keywords]), "hello")',
[
'keywords' => 'hello,world',
]
)
π€ Output: true
matches_in_array() functionπ₯ Input:
new Expression(
'matches_in_array([keywords], "pool")',
[
'keywords' => [
'swimming pool',
],
]
)
π€ Output: true
explode() is_empty() and functionsπ₯ Input:
new Expression(
'is_empty(explode([keywords]))',
[
'keywords' => '',
]
)
π€ Output: true
implode() with inline parameter substitutionπ₯ Input:
new Expression(
'implode(([attr1],[attr2]))',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: hello world
implode() with inline parameter substitution and a separator flagπ₯ Input:
new Expression(
'implode(([attr1],[attr2]), ",")',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: hello,world
explode() with array substitutionπ₯ Input:
new Expression(
'explode([Rooms])',
[
'Rooms' => 'Pantry,Study',
]
)
π€ Output: ['Pantry', 'Study']
explode() with array substitution and a separator flagπ₯ Input:
new Expression(
'explode([Rooms], ";")',
[
'Rooms' => 'Pantry;Study',
]
)
π€ Output: ['Pantry', 'Study']
get() function with count and nullable flagsπ₯ Input:
new Expression(
'get([attr1], {"count":true, "nullable":false})',
[
'attr1' => [
'a',
'b',
'c',
],
]
)
π€ Output: 3
map flag in get() functionπ₯ Input:
new Expression(
'get([attr1], {"map":{"a":1, "b": 2, "c": 3}})',
[
'attr1' => 'b',
]
)
π€ Output: 2
sensitive flagπ₯ Input:
new Expression(
'matches_in_array([keywords], "pool", {"sensitive":true})',
[
'keywords' => [
'Swimming Pool',
],
]
)
π€ Output: false
equal() functionπ₯ Input:
new Expression(
'equal([attr1], 1)',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: true
great_than() functionπ₯ Input:
new Expression(
'great_than([attr1], 0)',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: true
not() and equal() functionsπ₯ Input:
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: true
not() and equal() functionsπ₯ Input:
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
π€ Output: true
and_x()π₯ Input:
new Expression(
'and_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
π€ Output: true
or_x()π₯ Input:
new Expression(
'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
π€ Output: true
or_x() and not()π₯ Input:
new Expression(
'not(or_x(equal([attr1], 1), in_array(explode([attr2]), "word")))',
[
'attr1' => 2,
'attr2' => 'hello,world',
]
)
π€ Output: true
π₯ Input:
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,
]
)
π€ Output: 20
Laravel Collection helpersThe package already has a built-in support of Laravel Collection helpers. For more informations about the available functions it supports please refer to the original Laravel Collection documentation page.
π₯ Input:
new Expression(
'first(collect([attr1], [attr2]))',
[
'attr1' => 'value 1',
'attr2' => 'value 2',
]
)
π€ Output: 'value 1'
In order to extend or override current functionality, you will need to add your own handler class name to config/handlers.php file:
use DI\ExpressionParser\Handlers\Logical;
use DI\ExpressionParser\Handlers\Standard;
use DI\ExpressionParser\Handlers\LaravelCollectionAdapter;
return [
Standard::class,
Logical::class,
LaravelCollectionAdapter::class,
// Add custom expression handlers here:
// \Acme\Handlers\CustomHandler::class,
// 'Acme\Handlers\CustomHandler',
];
Please feel free to fork and help developing.