Operators
Ruler provides over 50 operators for building expressive rule conditions.
Comparison Operators
Section titled “Comparison Operators”Define variables for rule evaluation:
$a = $rb['a'];$b = $rb['b'];Equality
Section titled “Equality”$a->equalTo($b); // true if $a == $b$a->notEqualTo($b); // true if $a != $b$a->sameAs($b); // true if $a === $b$a->notSameAs($b); // true if $a !== $bNumeric Comparison
Section titled “Numeric Comparison”$a->greaterThan($b); // true if $a > $b$a->greaterThanOrEqualTo($b); // true if $a >= $b$a->lessThan($b); // true if $a < $b$a->lessThanOrEqualTo($b); // true if $a <= $bString Comparison
Section titled “String Comparison”$a->stringContains($b); // true if strpos($b, $a) !== false$a->stringDoesNotContain($b); // true if strpos($b, $a) === false$a->stringContainsInsensitive($b); // true if stripos($b, $a) !== false$a->stringDoesNotContainInsensitive($b); // true if stripos($b, $a) === false$a->startsWith($b); // true if strpos($b, $a) === 0$a->startsWithInsensitive($b); // true if stripos($b, $a) === 0$a->endsWith($b); // true if ends with substring$a->endsWithInsensitive($b); // case-insensitive ends withMathematical Operators
Section titled “Mathematical Operators”Mathematical operators return values rather than boolean results, so they must be combined with comparison operators:
$rb['price'] ->add($rb['shipping']) ->greaterThanOrEqualTo(50);Arithmetic
Section titled “Arithmetic”$c = $rb['c'];$d = $rb['d'];
$c->add($d); // $c + $d$c->subtract($d); // $c - $d$c->multiply($d); // $c * $d$c->divide($d); // $c / $d$c->modulo($d); // $c % $d$c->exponentiate($d); // $c ** $dUnary Math
Section titled “Unary Math”$c->negate(); // -$c$c->ceil(); // ceil($c)$c->floor(); // floor($c)Set Operators
Section titled “Set Operators”For working with arrays:
$e = $rb['e']; // Array$f = $rb['f']; // ArraySet Manipulation
Section titled “Set Manipulation”$e->union($f); // Elements in either set$e->intersect($f); // Elements in both sets$e->complement($f); // Elements in $e but not $f$e->symmetricDifference($f); // Elements in one set but not both$e->min(); // Minimum value$e->max(); // Maximum valueSet Propositions
Section titled “Set Propositions”$e->containsSubset($f); // true if $f ⊆ $e$e->doesNotContainSubset($f); // true if $f ⊄ $e$e->setContains($a); // true if $a ∈ $e$e->setDoesNotContain($a); // true if $a ∉ $eLogical Operators
Section titled “Logical Operators”Combine propositions:
$rb->logicalAnd($propA, $propB); // True if both are true$rb->logicalOr($propA, $propB); // True if either is true$rb->logicalXor($propA, $propB); // True if exactly one is true$rb->logicalNot($propA); // Inverts the resultCustom Operators
Section titled “Custom Operators”Extend Ruler with domain-specific operators:
namespace My\Ruler\Operators;
use Ruler\Context;use Ruler\Operator\VariableOperator;use Ruler\Proposition;use Ruler\Value;
class ALotGreaterThan extends VariableOperator implements Proposition{ public function evaluate(Context $context): bool { list($left, $right) = $this->getOperands(); $value = $right->prepareValue($context)->getValue() * 10;
return $left->prepareValue($context)->greaterThan(new Value($value)); }
protected function getOperandCardinality() { return static::BINARY; }}Register and use:
$rb->registerOperatorNamespace('My\\Ruler\\Operators');$rb->create($rb['a']->aLotGreaterThan(10));