coral-media / binary-rules-evaluator
Choose an option based on binary rules for a set of conditions
Requires
- php: ^7.4|^8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Purpose
Evaluate set of conditions avoiding redundant if/else
statements.
How it works
Having the following table:
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.