承接 arekx/array-expression-engine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arekx/array-expression-engine

Composer 安装命令:

composer require arekx/array-expression-engine

包简介

Array Expression Engine Parser

README 文档

README

Scrutinizer Code Quality Build Status Code Coverage

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

$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

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:

$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 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 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 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 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 <= 20

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

$evaluator->run($expression, ['age' => 5]); // returns true
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 equal
  • in - Is one of the values. Example ['compare', <expressionA>, ['value', [1,2,10]]] checks if <expressionA> is 1, 2 or 10.

Example:

$expression = ['compare', ['get', 'name'], ['value', 'test']];

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

$evaluator->run($expression, ['name' => 'test', 'age' => 5]); // returns true
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:

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

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

$evaluator->run($expression, ['name' => 'John']); // returns true
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 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 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

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

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

Run composer test to run tests.

arekx/array-expression-engine 适用场景与选型建议

arekx/array-expression-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.62k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 arekx/array-expression-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 arekx/array-expression-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 arekx/array-expression-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-04-27