โ Expression Parser v0.2.3
, (*1)
This package allows to evaluate (parse with mapping) large amounts of data in flexible manner, providing various processing functions:, (*2)
๐ฉ Install
composer install di/expression-parser
, (*3)
โ Usage
// Signature
$expression = new Expression(string $expression[, array $mappings = []]);
๐ Example
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
Standard function handlers
๐ Parameter substitution
๐ฅ Input:, (*4)
new Expression(
'[attr1]',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: 1
, (*5)
๐ Parameter substitution with has()
function
๐ฅ Input:, (*6)
new Expression(
'has([attr1])',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: true
, (*7)
๐ Substitution with in_array()
function and scalar value
๐ฅ Input:, (*8)
new Expression(
'in_array([keywords], "hello")',
[
'keywords' => [
'hello',
'world',
],
]
)
๐ค Output: true
, (*9)
๐ Nested in_array()
and explode()
function and scalar value
๐ฅ Input:, (*10)
new Expression(
'in_array(explode([keywords]), "hello")',
[
'keywords' => 'hello,world',
]
)
๐ค Output: true
, (*11)
๐ Substitution with matches_in_array()
function
๐ฅ Input:, (*12)
new Expression(
'matches_in_array([keywords], "pool")',
[
'keywords' => [
'swimming pool',
],
]
)
๐ค Output: true
, (*13)
๐ Nested explode()
is_empty()
and functions
๐ฅ Input:, (*14)
new Expression(
'is_empty(explode([keywords]))',
[
'keywords' => '',
]
)
๐ค Output: true
, (*15)
๐ implode()
with inline parameter substitution
๐ฅ Input:, (*16)
new Expression(
'implode(([attr1],[attr2]))',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: hello world
, (*17)
๐ implode()
with inline parameter substitution and a separator flag
๐ฅ Input:, (*18)
new Expression(
'implode(([attr1],[attr2]), ",")',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: hello,world
, (*19)
๐ explode()
with array substitution
๐ฅ Input:, (*20)
new Expression(
'explode([Rooms])',
[
'Rooms' => 'Pantry,Study',
]
)
๐ค Output: ['Pantry', 'Study']
, (*21)
Standard handlers with one or multiple flags
๐ explode()
with array substitution and a separator flag
๐ฅ Input:, (*22)
new Expression(
'explode([Rooms], ";")',
[
'Rooms' => 'Pantry;Study',
]
)
๐ค Output: ['Pantry', 'Study']
, (*23)
๐ get()
function with count
and nullable
flags
๐ฅ Input:, (*24)
new Expression(
'get([attr1], {"count":true, "nullable":false})',
[
'attr1' => [
'a',
'b',
'c',
],
]
)
๐ค Output: 3
, (*25)
๐ Nested mapping with map
flag in get()
function
๐ฅ Input:, (*26)
new Expression(
'get([attr1], {"map":{"a":1, "b": 2, "c": 3}})',
[
'attr1' => 'b',
]
)
๐ค Output: 2
, (*27)
๐ Case sensitive matching in array with sensitive
flag
๐ฅ Input:, (*28)
new Expression(
'matches_in_array([keywords], "pool", {"sensitive":true})',
[
'keywords' => [
'Swimming Pool',
],
]
)
๐ค Output: false
, (*29)
Logical handlers
๐ equal()
function
๐ฅ Input:, (*30)
new Expression(
'equal([attr1], 1)',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: true
, (*31)
๐ great_than()
function
๐ฅ Input:, (*32)
new Expression(
'great_than([attr1], 0)',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: true
, (*33)
๐ Nested not()
and equal()
functions
๐ฅ Input:, (*34)
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: true
, (*35)
๐ Nested not()
and equal()
functions
๐ฅ Input:, (*36)
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
๐ค Output: true
, (*37)
Complex functions with unlimited nesting level
๐ Multiple function parameter substitution with nesting with and_x()
๐ฅ Input:, (*38)
new Expression(
'and_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
๐ค Output: true
, (*39)
๐ Multiple function parameter substitution with nesting with or_x()
๐ฅ Input:, (*40)
new Expression(
'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
๐ค Output: true
, (*41)
๐ Multiple function parameter substitution with nesting with or_x()
and not()
๐ฅ Input:, (*42)
new Expression(
'not(or_x(equal([attr1], 1), in_array(explode([attr2]), "word")))',
[
'attr1' => 2,
'attr2' => 'hello,world',
]
)
๐ค Output: true
, (*43)
๐ณ Multiple nesting with a Closure
๐ฅ Input:, (*44)
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
, (*45)
Laravel Collection
helpers
The 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., (*46)
๐ Example usage of Laravel Collection functions
๐ฅ Input:, (*47)
new Expression(
'first(collect([attr1], [attr2]))',
[
'attr1' => 'value 1',
'attr2' => 'value 2',
]
)
๐ค Output: 'value 1'
, (*48)
Extending with custom handlers
In order to extend or override current functionality, you will need to add your own handler class name to config/handlers.php
file:, (*49)
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',
];
๐ Contribution
Please feel free to fork and help developing., (*50)
๐ License
MIT, (*51)