Commit 4cedd7d1 authored by Sebastian Kehr's avatar Sebastian Kehr 🚣🏼
Browse files

let priority be last rule arg defaulting to zero (refs #12817)

parent 66b34b7a
......@@ -42,6 +42,8 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage as Engine;
class Evaluator
{
const STOP_FLAG = 'STOP';
const DEFAULT_PRIO = 0;
/**
* @var Engine The engine to use for rule evaluation.
*/
......@@ -70,12 +72,17 @@ class Evaluator
$this->rules = $rules;
$this->stopFlags = $stopFlags;
uksort(
$this->rules, function ($firstIdx, $secondIdx) {
$firstPrio = intval($this->rules[$firstIdx][0]);
$secondPrio = intval($this->rules[$secondIdx][0]);
return $secondPrio - $firstPrio ?: $secondIdx - $firstIdx;
foreach ($this->rules as $index => list($head, $body)) {
$this->rules[$index] += [$head, $body, static::DEFAULT_PRIO];
}
uksort(
$this->rules,
function ($firstIdx, $secondIdx) {
$firstPrio = intval($this->rules[$firstIdx][2]);
$secondPrio = intval($this->rules[$secondIdx][2]);
return $secondPrio - $firstPrio ?: $secondIdx - $firstIdx;
}
);
}
......@@ -110,10 +117,12 @@ class Evaluator
}
foreach ($this->stopFlags as $stopFlag) {
if ($context[$stopFlag]) return $context;
if ($context[$stopFlag]) {
return $context;
}
}
list(, $head, $body) = $rule;
list($head, $body) = $rule;
$context[$head] = $this->engine->evaluate($body, $context);
return $this->evaluate($context, ...$rules);
}
......
......@@ -44,17 +44,17 @@ class EvaluatorTest extends TestCase
protected $rules
= [
// 5th rule
[80, 't', '[1]'],
['t', '[1]', 80],
// 4th rule
[85, 'e', 'e + e'],
['e', 'e + e', 85],
// 3rd rule
[90, 's', '[]'],
['s', '[]', 90],
// 2nd rule
[95, 'e', '1'],
['e', '1', 95],
// 1st rule
['100', 'd', 'a.f("xyz") * 2 - 2'],
// last rule due to lowest priority
[75, 'e', 'd * d'],
['d', 'a.f("xyz") * 2 - 2', '100'],
// last rule due to lowest priority (defaults to 0)
['e', 'd * d'],
];
public function testSimple()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment