Skip to content

This package allows evaluating (parse with mapping) large amounts of data in a flexible manner, providing various processing functions

License

Notifications You must be signed in to change notification settings

matrunchyk/expression-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

54 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

β›“ Expression Parser v0.2.3

Build Status Last version License

This package allows to evaluate (parse with mapping) large amounts of data in flexible manner, providing various processing functions:

πŸ”© Install

composer install di/expression-parser

βš’ 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:

new Expression(
    '[attr1]',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)

πŸ“€ Output: 1

πŸ”— Parameter substitution with has() function

πŸ“₯ Input:

new Expression(
    'has([attr1])',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)

πŸ“€ Output: true

πŸ”— Substitution with in_array() function and scalar value

πŸ“₯ Input:

new Expression(
    'in_array([keywords], "hello")',
    [
        'keywords' => [
            'hello',
            'world',
        ],
    ]
)

πŸ“€ Output: true

πŸ”— Nested in_array() and explode() function and scalar value

πŸ“₯ Input:

new Expression(
    'in_array(explode([keywords]), "hello")',
    [
        'keywords' => 'hello,world',
    ]
)

πŸ“€ Output: true

πŸ”— Substitution with matches_in_array() function

πŸ“₯ Input:

new Expression(
    'matches_in_array([keywords], "pool")',
    [
        'keywords' => [
            'swimming pool',
        ],
    ]
)

πŸ“€ Output: true

πŸ”— Nested 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']

Standard handlers with one or multiple flags

πŸ”— 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

πŸ”— Nested mapping with map flag in get() function

πŸ“₯ Input:

new Expression(
    'get([attr1], {"map":{"a":1, "b": 2, "c": 3}})',
    [
        'attr1' => 'b',
    ]
)

πŸ“€ Output: 2

πŸ”— Case sensitive matching in array with sensitive flag

πŸ“₯ Input:

new Expression(
    'matches_in_array([keywords], "pool", {"sensitive":true})',
    [
        'keywords' => [
            'Swimming Pool',
        ],
    ]
)

πŸ“€ Output: false

Logical handlers

πŸ”— 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

πŸ”— Nested not() and equal() functions

πŸ“₯ Input:

new Expression(
    'not(equal([attr1], 2))',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)

πŸ“€ Output: true

πŸ”— Nested not() and equal() functions

πŸ“₯ Input:

new Expression(
    'not(equal([attr1], 2))',
    [
        'attr1' => 1,
        'attr2' => 2,
    ]
)

πŸ“€ Output: true

Complex functions with unlimited nesting level

πŸ”— Multiple function parameter substitution with nesting with and_x()

πŸ“₯ Input:

new Expression(
    'and_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
    [
        'attr1' => 1,
        'attr2' => 'hello,world',
    ]
)

πŸ“€ Output: true

πŸ”— Multiple function parameter substitution with nesting with or_x()

πŸ“₯ Input:

new Expression(
    'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
    [
        'attr1' => 1,
        'attr2' => 'hello,world',
    ]
)

πŸ“€ Output: true

πŸ”— Multiple function parameter substitution with nesting with 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

😳 Multiple nesting with a Closure

πŸ“₯ 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 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.

πŸ”— Example usage of Laravel Collection functions

πŸ“₯ Input:

new Expression(
    'first(collect([attr1], [attr2]))',
    [
        'attr1' => 'value 1',
        'attr2' => 'value 2',
    ]
)

πŸ“€ Output: 'value 1'

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:

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.

πŸ“ƒ License

MIT

About

This package allows evaluating (parse with mapping) large amounts of data in a flexible manner, providing various processing functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages