定制 coral-media/binary-rules-evaluator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

coral-media/binary-rules-evaluator

最新稳定版本:0.1

Composer 安装命令:

composer require coral-media/binary-rules-evaluator

包简介

Choose an option based on binary rules for a set of conditions

README 文档

README

Purpose

Evaluate set of conditions avoiding redundant if/else statements.

How it works

Having the following table:

Condition1 Condition2 Condition2
Result1 true false false
Result2 false true true
Result3 null true false
Result3 true null false

We translate that table into an array

$rulesTable = [
    'Result1' => [
        [
            'Condition1' => true,
            'Condition2' => false,
            'Condition3' => false
        ],
    ],
    'Result2' => [
        [
            'Condition1' => false,
            'Condition2' => true,
            'Condition3' => true
        ],
    ],
    'Result3' => [
        [
            'Condition1' => null,
            'Condition2' => true,
            'Condition3' => false
        ],
        [
            'Condition1' => true,
            'Condition2' => null,
            'Condition3' => false
        ],
    ],
];

Having as example the following inputs:

$input1 = [true, null, false] //matches Result3
$input2 = [null, null, false] //matches none

The validator will return the condition label or false if none matches. The null was added to represent unknown values and will not be evaluated.

$binaryRulesEvaluator = (new BinaryRulesEvaluator())
    ->setRulesTable($rulesTable);

$binaryRulesEvaluator->evaluate($input1); // 'Result3'
$binaryRulesEvaluator->evaluate($input2); // false

If more than one result share rules set, the method validate will return the first matching result.

This utility class helps to keep code consistent, clean and maintainable. Trust me, code with a lot of if/elseif/else statements can be really messy and hard to understand. Keep in mind almost always you would be able to turn any set of evaluations into a true/false table.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-28