hdgarau/logic-gate

设置一个参考值并与另一个值进行比较。将一些条件作为树(AND/OR)来评估,并返回true/false。

安装: 5

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:composer-module

v1.0.3 2022-02-20 16:05 UTC

This package is auto-updated.

Last update: 2024-09-20 21:52:43 UTC


README

描述

使用LogicGate,您可以定义一个可评估的逻辑树,然后评估一个值或过滤一个数组。当您需要执行一个或多个复杂过滤器时,这可能很有用。LogicRoot对象包含一个或多个LogicGate对象(或任何实现iEvaluable接口的对象,甚至是另一个LogicRoot对象)。您也可以评估单个LogicGate对象。

用法

LogicGate运算符

允许以下运算符

  • LogicGate::OP_EQ (=)
  • LogicGate::OP_GT (>)
  • LogicGate::OP_LT (<)
  • LogicGate::OP_REGEX (~) 您可以在它之前添加 LogicGate::OP_NOT (!) 来否定条件。您可以使用可调用对象代替运算符

示例

//Using '!='
$gate1 = new LogicGate(4,LogicGate::OP_NOT . LogicGate::OP_EQ);
$gate1->test(1); //true
$gate1->test(4); //false

//Using '>' in a array
$gate2 = new LogicGate(18,LogicGate::OP_GT);
$arr_filtered = $gate2->filter([21,3,33]); // [0=>21,2=>33]

//Using a function
//the function must have two arguments:
// - the value to check
// - value of gate
$fx = function($val, $ref) { return $val->prop1 == $ref;};
$gate3 = new LogicGate(5,$fx);
$gate3->test((object)['prop1'=>5]); //true

如何使用

  • 您可以通过三种不同的方式定义RootGate(树)
    • 使用'methods 'addAND'和'addOR'
    • 使用数组。
    • 使用字符串。

使用'add'方法

这是保持健康顺序的最佳方式,并且可以对具有不同条件的分支进行操作。
您可以将LogicGate或LogicGateRoot(或任何实现iIsEvaluable的类)添加到其中

示例

 //two-digit number greater than 45 or one-digit number greater than five       
        $gatesRoot = new LogicGatesRoot();
        $gatesRoot->addAND (new LogicGate('^\\d\\d$', LogicGate::OP_REGEX));
        $gatesRoot->addAND (new LogicGate(45, LogicGate::OP_GT));
        $gatesRoot->addOR(new LogicGate(5, LogicGate::OP_GT));
        $gatesRoot->addAND(new LogicGate('^\\d$', LogicGate::OP_REGEX));
        $gatesRoot->filter([11,8,178,88,97]); //[1=>8,3=>88,4=>97]
        
        $gatesRoot2 = new LogicGatesRoot();
        $gatesRoot2->addAND (new LogicGate('^8', LogicGate::OP_REGEX));
        $gatesRoot2->addAND ($gatesRoot);
        $gatesRoot2->filter([11,8,178,88,97]); //[1=>8,3=>88]

使用数组

如果您有一个包含条件的数组,您可以使用它来创建树。
键是

  • operator(可选)默认正则表达式
  • value(必需):参考值
  • next_gate(必需)(OR/AND)

示例

 $fx = function ($el, $ref) { return $el %2 ==0;};
        $arr = [
            ['value' => '^\\d+$', 'next_gate' => 'AND'] ,
            ['operator' => $fx, 'value' => null, 'next_gate' => 'OR'] ,
            ['operator' => LogicGate::OP_GT, 'value' => 60, 'next_gate' => 'anything']
        ];
        $gate = new LogicGatesRoot($arr);
        $gate->test(61);//true
        $gate->test(6);//true
        $gate->test(57);//false

使用字符串

使用强力查询制作复杂树的最简模式。

规则是

  • {:operator}:"{:value}" {AND/OR}
  • 支持括号查询
  • 运算符可以是<,>,=,~或名称函数。
  • 使用字符前缀!否定条件(非运算符)
  • 值只能是一个字符串

示例


        //(~:"2$" OR ~:"comodin) AND (~:"1" OR =:"comodin21") AND !=:"comodin12"
        //(finish with two or contains "comodin") and (contains 1 OR is equal "comodin21")
        $lg = new LogicGatesRoot('(' . LogicGate::OP_REGEX . ':"2$" OR ' . LogicGate::OP_REGEX . ':"comodin") '.
            'AND (' . LogicGate::OP_REGEX . ':"1" OR ' . LogicGate::OP_EQ . ':"comodin23") AND '
            . LogicGate::OP_NOT .  LogicGate::OP_EQ . ':"comodin211"');
        
        $lg->test('comodin21'); //true
        $lg->test('comodin22'); //false
        $lg->test('comodin12'); //false
        $lg->test('comodin103'); //true
        $lg->test('12'); //true
        $lg->test('comodin211'); //false

示例

  • 参见TestCases以获取更多示例。