Download the PHP package arekx/array-expression-engine without Composer
On this page you can find all versions of the php package arekx/array-expression-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download arekx/array-expression-engine
More information about arekx/array-expression-engine
Files in arekx/array-expression-engine
Package array-expression-engine
Short Description Array Expression Engine Parser
License Apache-2.0
Informations about the package array-expression-engine
Array Expression Engine
This is an array expression parser which can be used to parse values using configuration specified in PHP arrays. These arrays can be loaded from anywhere, like from JSON string, PHP files, etc.
These expressions are used to configure the expression parser engine which runs a value through the rules defined in the array expression to return a result.
Installation
Run composer require arekx/array-expression-engine
in your project.
Usage
Operators
Summary
Following operators are available:
Operator | Name | Definition |
---|---|---|
AND | AND operator | ['and', <expression1>, ..., <expressionN>] |
OR | OR operator | ['or', <expression1>, ..., <expressionN>] |
XOR | XOR operator (exclusive OR) | ['xor', <expression1>, ..., <expressionN>] |
NOT | NOT operator (inverts check) | ['not', <expression>] |
BETWEEN | BETWEEN operator, checks if the value is between minimum and maximum (inclusive) | ['between', <valueExpression>, <minExpression>, <maxExpression>] |
COMPARE | Comparison operator | ['compare', <expressionA>, <expressionB>] , ['compare, <expressionA>, '=', <expressionB>] |
REGEX | Regex operator | ['regex', <expression>, '/pattern/'] , ['regex', <expression>, <patternExpression>] |
VALUE | Value operator, returns static values | ['value', 'this is a static value'] |
GET | GET operator, returns values by name from passed value | ['get', 'keyFromValue'] |
CONCAT | CONCAT operator, concatenates strings | ['concat', <expression1>, ..., <expressionN>] |
AND Operator
AND operator is defined in ArekX\ArrayExpression\Operators\AndOperator
class and is used to represent
AND operation between two or more expressions, those expressions can by any other operator including AND operator.
Example:
OR Operator
OR operator is defined in ArekX\ArrayExpression\Operators\OrOperator
class and is used to represent
OR operation between two or more expressions, those expressions can by any other operator including OR operator.
Example:
XOR Operator
XOR operator is defined in ArekX\ArrayExpression\Operators\XOrOperator
class and is used to represent
XOR operation between two or more expressions, those expressions can by any other operator including XOR operator.
Example:
NOT Operator
Not operator is defined in ArekX\ArrayExpression\Operators\NotOperator
class and is used to represent
NOT operation or the inversion of the expression passed to it.
Example:
BETWEEN Operator
Between operator is defined in ArekX\ArrayExpression\Operators\BetweenOperator
class and is used to check if a value is between minimum and maximum value.
Example:
COMPARE Operator
Comparison operator for comparing two expressions. It is defined in ArekX\ArrayExpression\Operators\CompareOperator
.
Comparison operator accepts multiple formats:
Short format
['compare', <expressionA>, <expressionB>]
Checks if <expressionA>
equals (strict) to <expressionB>
Relation format
['compare', <expressionA>, '>=', <expressionB>]
Checks if <expressionB>
is greater or equal to <expressionB>
and returns true
/false
Supported relation operators:
>
- Greater than>=
- Greater than or equal<
- Less than<=
- Less than or equal<>
- Not equalin
- Is one of the values. Example['compare', <expressionA>, ['value', [1,2,10]]]
checks if<expressionA>
is1
,2
or10
.
Example:
REGEX Operator
REGEX operator is defined in ArekX\ArrayExpression\Operators\RegexOperator
class and is used to check if a value matches a specific regex pattern.
Regex operator accepts multiple formats:
String format
['regex', <expression>, '/pattern/']
Checks if <expression>
matches specific pattern. Return value from <expression>
must be a string.
Expression format
['regex', <expression>, <expressionFormat>]
Checks if <expression>
matches specific pattern defined by <expressionFormat>
.
Return value from <expression>
must be a string.
Return value from <expressionFormat>
must be a string.
Example:
Value Operator
Value operator is defined in ArekX\ArrayExpression\Operators\ValueOperator
class and is used to return a static value.
Example:
Get Operator
Get operator is defined in ArekX\ArrayExpression\Operators\GetOperator
class and is used to return a value from a key.
Example:
Concat Operator
Concat operator is defined in ArekX\ArrayExpression\Operators\ConcatOperator
class and is used to concatenate two or more strings.
It requires evaluation results to be strings.
Example:
Custom operators
You can create your own custom operator manually by implementing Operator
interface and adding that operator
to ExpressionParser
of your Evaluator
.
We will implement a custom operator which transforms all instances of a word cat
into dog
.
Operator definition we want to implement is: ['dog', <expression>]
First we implement an Operator
class
After creating this class we need to add it to the evaluator's expression parser and we are set:
Tests
Run composer test
to run tests.