2017-25 ยฉ Pedro Pelรกez
 

library expression-parser

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

image

di/expression-parser

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

  • Thursday, April 19, 2018
  • by matrunchyk
  • Repository
  • 1 Watchers
  • 3 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

โ›“ Expression Parser v0.2.3

Build Status Last version License , (*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)

The Versions

19/04 2018

dev-master

9999999-dev

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel parser library expression parsing compiler evaluate

19/04 2018

dev-dev

dev-dev

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel parser library expression parsing compiler evaluate

19/04 2018

0.2.2

0.2.2.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel parser library expression parsing compiler evaluate