softonic/jaxer

PHP引擎,一统天下

1.0.0 2022-03-02 10:27 UTC

This package is auto-updated.

Last update: 2024-08-29 05:50:36 UTC


README

Latest Version Software License Build Status Total Downloads Average time to resolve an issue Percentage of issues still open

一统天下的规则引擎

Jaxer 允许您轻松评估复杂规则,并了解验证结果的背后原因。

主要功能

安装

您可以使用 composer 需求最新版本的软件包

composer require softonic/jaxer

使用方法

规则是需要实现 \Softonic\Jaxer\Rules\Rule 接口类。它们通常通过构造函数获取输入参数,例如

<?php

use \Softonic\Jaxer\Rules\Rule;

class IsNumberHigherThanFive implements Rule {
    /**
     * We receive the parameters from constructor.
     * You could also use specific setters or direct access to attribute.
     */
    public function __construct(private int $number)
    {
    }

    /**
     * Business logic to be evaluated.
     */
    public function isValid(): bool
    {
        return $this->number > 5;
    }
    
    /**
     * [Tracing] Context information to provide in case to know what happened in the evaluation.
     */
    public function getContext(): array
    {
        return [
            'number' => $this->number,
        ];
    }
}

如你所见,在 isValid 方法中有一个专门用于业务逻辑的部分,还有一个 getContext 方法允许你跟踪规则信息。

另一方面,这个库提供了通用规则,有助于评估复杂规则。这些逻辑操作包括:ANDORNOT

使用 AndRule、OrRule 和 NotRule 的示例

<?php

use Softonic\Jaxer\Rules\AndRule;
use Softonic\Jaxer\Rules\NotRule;
use Softonic\Jaxer\Rules\OrRule;
use Softonic\Jaxer\Rules\Rule;
use Softonic\Jaxer\Validation;

class FalseRule implements Rule {
    public function isValid(): bool
    {
        return false;
    }

    public function getContext(): array
    {
        return [];
    }
}

class TrueRule implements Rule {
    public function isValid(): bool
    {
        return true;
    }

    public function getContext(): array
    {
        return [];
    }
}

$logicalRule = new OrRule( //true
    new AndRule( //false
        new TrueRule(),
        new FalseRule(),
        new TrueRule(),
    ),
    new AndRule( //true
        new NotRule( // true
            new FalseRule()
        ),
        new TrueRule()
    )
);

$validation = new Validation($logicalRule);
$validation->isValid(); // true
$validation->getContext(); // ['rule' => \Softonic\Jaxer\Rules\OrRule, 'context' => [...], 'evaluated' => true, 'isValid' => true]

在验证评估后,你可以多次执行 isValid,而不会影响性能,因为它只会在第一次调用时进行评估。

高级用法

包含内部规则的规则

有时你需要创建一个包含其他规则的规则,例如 ANDORNOT。这些规则包含其他需要在验证时评估的规则。

要评估一个规则,你需要将其封装在 Validation 对象中。它将为规则添加状态和通用的上下文格式。

逻辑非示例

<?php

namespace Softonic\Jaxer\Rules;

use Softonic\Jaxer\Validation;

class NotRule implements Rule
{
    // Validation decorator
    private Validation $validation;

    public function __construct(private Rule $rule)
    {
        // Encapsulate rule inside a validation
        $this->validation = new Validation($rule);
    }

    public function isValid(): bool
    {
        // Validate the rule
        return !$this->validation->isValid();
    }

    public function getContext(): array
    {
        // Get the validation context
        return $this->validation->getContext();
    }
}

测试

softonic/jaxer 有一个 PHPUnit 测试套件和一个使用 PHP CS Fixer 的编码风格合规性测试套件。

要从项目文件夹中运行测试,请执行以下命令。

$ make tests

在开发环境中打开终端

$ make debug

许可证

Apache 2.0 许可证。有关更多信息,请参阅 LICENSE