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
π₯ Input:
new Expression(
    'has([attr1])',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'in_array([keywords], "hello")',
    [
        'keywords' => [
            'hello',
            'world',
        ],
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'in_array(explode([keywords]), "hello")',
    [
        'keywords' => 'hello,world',
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'matches_in_array([keywords], "pool")',
    [
        'keywords' => [
            'swimming pool',
        ],
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'is_empty(explode([keywords]))',
    [
        'keywords' => '',
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'implode(([attr1],[attr2]))',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: hello world
π₯ Input:
new Expression(
    'implode(([attr1],[attr2]), ",")',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: hello,world
π₯ Input:
new Expression(
    'explode([Rooms])',
    [
        'Rooms' => 'Pantry,Study',
    ]
)
π€ Output: ['Pantry', 'Study']
π₯ Input:
new Expression(
    'explode([Rooms], ";")',
    [
        'Rooms' => 'Pantry;Study',
    ]
)
π€ Output: ['Pantry', 'Study']
π₯ Input:
new Expression(
    'get([attr1], {"count":true, "nullable":false})',
    [
        'attr1' => [
            'a',
            'b',
            'c',
        ],
    ]
)
π€ Output: 3
π₯ Input:
new Expression(
    'get([attr1], {"map":{"a":1, "b": 2, "c": 3}})',
    [
        'attr1' => 'b',
    ]
)
π€ Output: 2
π₯ Input:
new Expression(
    'matches_in_array([keywords], "pool", {"sensitive":true})',
    [
        'keywords' => [
            'Swimming Pool',
        ],
    ]
)
π€ Output: false
π₯ Input:
new Expression(
    'equal([attr1], 1)',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'great_than([attr1], 0)',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'not(equal([attr1], 2))',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'not(equal([attr1], 2))',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'and_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
    [
        'attr1' => 1,
        'attr2' => 'hello,world',
    ]
)
π€ Output: true
π₯ Input:
new Expression(
    'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
    [
        'attr1' => 1,
        'attr2' => 'hello,world',
    ]
)
π€ Output: true
π₯ 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
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.
π₯ 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.