PHPackages                             arekx/array-expression-engine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. arekx/array-expression-engine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

arekx/array-expression-engine
=============================

Array Expression Engine Parser

1.1.2(7y ago)34.7k↓41.1%2Apache-2.0PHPPHP &gt;=7.0

Since Apr 27Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ArekX/array-expression-engine)[ Packagist](https://packagist.org/packages/arekx/array-expression-engine)[ RSS](/packages/arekx-array-expression-engine/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (0)

Array Expression Engine
=======================

[](#array-expression-engine)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6b32848cffbd2276268337e2120ef1219445bd249ffa40d03b3b1b4ef18b4e8e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4172656b582f61727261792d65787072657373696f6e2d656e67696e652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/?branch=master) [![Build Status](https://camo.githubusercontent.com/8add06551b3eb1a6f50808f8356f3c5e0ade0350ea283e2aeaecaafe1ec76c9f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4172656b582f61727261792d65787072657373696f6e2d656e67696e652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/build-status/master) [![Code Coverage](https://camo.githubusercontent.com/4c2d3d4a668f7f5cc259772a2e9b9524bef117209db9b7e90cd39bb839fcb56a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4172656b582f61727261792d65787072657373696f6e2d656e67696e652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ArekX/array-expression-engine/?branch=master)

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
------------

[](#installation)

Run `composer require arekx/array-expression-engine` in your project.

Usage
-----

[](#usage)

```
$isAStark = [
    'or',
    [
        'and',
        ['compare', ['get', 'first'], 'in', ['value', ['Arya', 'Sansa']]],
        ['compare', ['get', 'last'], '=', ['value', 'Stark']],
    ],
    ['regex', ['get', 'emblem'], '/stark/i']
];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$values = [
    ['first' => 'John', 'last' => 'Snow', 'emblem' => 'stark'],
    ['first' => 'Arya', 'last' => 'Stark', 'emblem' => 'stark'],
    ['first' => 'Sansa', 'last' => 'Stark', 'emblem' => 'stark'],
    ['first' => 'Joffrey', 'last' => 'Lannister', 'emblem' => 'lannister']
];

foreach ($values as $value) {
    var_dump($evaluator->run($isAStark, $value));
}

// Output: bool(true), bool(true), bool(true), bool(false)
```

### Operators

[](#operators)

#### Summary

[](#summary)

Following operators are available:

OperatorNameDefinitionANDAND operator`['and', , ..., ]`OROR operator`['or', , ..., ]`XORXOR operator (exclusive OR)`['xor', , ..., ]`NOTNOT operator (inverts check)`['not', ]`BETWEENBETWEEN operator, checks if the value is between minimum and maximum (inclusive)`['between', , , ]`COMPAREComparison operator`['compare', , ]`, `['compare, , '=', ]`REGEXRegex operator`['regex', , '/pattern/']`, `['regex', , ]`VALUEValue operator, returns static values`['value', 'this is a static value']`GETGET operator, returns values by name from passed value`['get', 'keyFromValue']`CONCATCONCAT operator, concatenates strings`['concat', , ..., ]`##### AND Operator

[](#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:

```
$nameMustBeTestAndAgeAbove2 = ['and', ['compare', ['get', 'name'], ['value', 'test']], ['compare', ['get', 'age'], '>', ['value', 2]]];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($nameMustBeTestAndAgeAbove2, ['name' => 'test', 'age' => 1]); // returns false
```

##### OR Operator

[](#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:

```
$nameMustBeTestOrAgeAbove2 = ['or', ['compare', ['get', 'name'], ['value', 'test']], ['compare', ['get', 'age'], '>', ['value', 2]]];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($nameMustBeTestOrAgeAbove2, ['name' => 'test', 'age' => 1]); // returns true
```

##### XOR Operator

[](#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:

```
$nameMustBeTestXOrAgeAbove2 = ['xor', ['compare', ['get', 'name'], ['value', 'test']], ['compare', ['get', 'age'], '>', ['value', 2]]];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($nameMustBeTestXOrAgeAbove2, ['name' => 'test', 'age' => 2]); // returns false
```

##### NOT Operator

[](#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:

```
$expression = ['not', ['or', ['compare', ['get', 'name'], ['value', 'test']], ['compare', ['get', 'age'], '>', ['value', 2]]]];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($expression, ['name' => 'test', 'age' => 5]); // returns false
```

##### BETWEEN Operator

[](#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:

```
$expression = ['between', ['get', 'age'], ['value', 1], ['value', 20]]; // Check if age is >= 1 and run($expression, ['age' => 5]); // returns true
```

##### COMPARE Operator

[](#compare-operator)

Comparison operator for comparing two expressions. It is defined in `ArekX\ArrayExpression\Operators\CompareOperator`.

Comparison operator accepts multiple formats:

**Short format**

`['compare', , ]`

Checks if `` equals (strict) to ``

**Relation format**

`['compare', , '>=', ]`

Checks if `` is greater or equal to `` and returns `true`/`false`

Supported relation operators:

- `>` - Greater than
- `>=` - Greater than or equal
- `run($expression, ['name' => 'test', 'age' => 5]); // returns true
```

##### REGEX Operator

[](#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', , '/pattern/']`

Checks if `` matches specific pattern. Return value from `` must be a string.

**Expression format**

`['regex', , ]`

Checks if `` matches specific pattern defined by ``.

Return value from `` must be a string.

Return value from `` must be a string.

Example:

```
$expression = ['regex', ['get', 'name'], '/o/i'];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($expression, ['name' => 'John']); // returns true
```

##### Value Operator

[](#value-operator)

Value operator is defined in `ArekX\ArrayExpression\Operators\ValueOperator` class and is used to return a static value.

Example:

```
$expression = ['value', 50];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($expression, ['name' => 'John']); // returns 50
```

##### Get Operator

[](#get-operator)

Get operator is defined in `ArekX\ArrayExpression\Operators\GetOperator` class and is used to return a value from a key.

Example:

```
$expression = ['get', 'name'];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($expression, ['name' => 'John']); // returns 'John'
```

##### Concat Operator

[](#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:

```
$expression = ['concat', ['get', 'first'], ['value', ' '], ['get', 'last']];

$evaluator = \ArekX\ArrayExpression\Evaluator::create();

$evaluator->run($expression, ['first' => 'John', 'last' => 'Snow']); // returns 'John Snow'
```

#### Custom operators

[](#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', ]`

First we implement an `Operator` class

```
use ArekX\ArrayExpression\Interfaces\ExpressionParser;
use ArekX\ArrayExpression\Interfaces\Operator;
use ArekX\ArrayExpression\Interfaces\ValueParser;

class DogOperator implements Operator
{
    /** @var ExpressionParser */
    public $parser;

    /** @var string */
    public $name;

    /** @var Operator */
    public $subExpression;

    public function configure(array $config)
    {
        $this->name = $config[0];
        $this->assertIsExpression($config[1]); // Assert that the value is an expression.
        $this->subExpression = $this->parser->parse($config[1]);
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setParser(ExpressionParser $parser)
    {
        $this->parser = $parser;
    }

    public function evaluate(ValueParser $value)
    {
        return str_ireplace('cat', 'dog', $this->subExpression->evaluate($value));
    }
}
```

After creating this class we need to add it to the evaluator's expression parser and we are set:

```
$evaluator = \ArekX\ArrayExpression\Evaluator::create();
$evaluator->getExpressionParser()->setType('dog', DogOperator::class);

$test = ['dog', ['get', 'sentence']];

$result = $evaluator->run($test, ['sentence' => 'Hello this is cat.']); // Returns: Hello this is dog.
```

Tests
-----

[](#tests)

Run `composer test` to run tests.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

4

Last Release

2620d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c16806a6fc4805b54005c72ea7aa0b6bc33f889a4a657a3071953f6b4e4a23c?d=identicon)[ArekXV](/maintainers/ArekXV)

---

Top Contributors

[![ArekX](https://avatars.githubusercontent.com/u/4344776?v=4)](https://github.com/ArekX "ArekX (22 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/arekx-array-expression-engine/health.svg)

```
[![Health](https://phpackages.com/badges/arekx-array-expression-engine/health.svg)](https://phpackages.com/packages/arekx-array-expression-engine)
```

###  Alternatives

[rainlab/location-plugin

Location plugin for October CMS

2123.3k6](/packages/rainlab-location-plugin)[bostos/reorderable-columns

This is my package reorderable-columns

124.6k](/packages/bostos-reorderable-columns)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
