PHP code example of ols / php-ruler

1. Go to this page and download the library: Download ols/php-ruler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ols / php-ruler example snippets




use Ols\PhpRuler\ExpressionEvaluator;

$eval = new ExpressionEvaluator();

$context = [
    'cart'     => ['total' => 150.00],
    'customer' => ['group' => 'vip', 'vip' => true],
    'product'  => ['price' => 49.99],
];

$eval->evaluate("cart.total > 100 AND customer.group = 'vip'", $context); // true

$eval->evaluate("round(cart.total * 0.9, 2)", $context);   // mixed  → 135.0
$eval->evaluateBoolean("cart.total >= 50", $context);      // bool   → true
$eval->evaluateNumeric("product.price * 2", $context);     // float  → 99.98

$result = $eval->evaluateSafe("cart.total > customer.creditLimit", $context);

$result->success;       // false
$result->missingVars;   // ['customer.creditLimit']
$result->getValueOr(false);

use Ols\PhpRuler\Explainer\ExpressionExplainer;

$explainer = new ExpressionExplainer($eval);
$result    = $explainer->explain(
    "customer.vip = true AND cart.total >= 50 AND product.price < 10 OR customer.group = 'vip'",
    $context
);

$result->passed;        // true | false | null (null = could not be fully evaluated)
$result->failures();    // evaluated leaves that returned false
$result->missing();     // leaves that needed an absent variable
$result->root;          // the full tree (expression, status, passed, resolved values, children)

use Ols\PhpRuler\AliasResolver;

$resolver = (new AliasResolver())
    ->add('customer.group', 'customer group')
    ->add('cart.total',     'cart amount');

// Human input → technical expression → evaluate
$expr = $resolver->humanToExpression("customer group = 'vip' AND cart amount > 100");
// "customer.group = 'vip' AND cart.total > 100"
$eval->evaluate($expr, $context);

// Stored technical expression → human form for display
$resolver->expressionToHuman("cart.total > 100"); // "cart amount > 100"

// Basic comparison
"cart.total > 100"
"user.role = 'admin'"

// Membership
"product.category IN ['clothing', 'shoes', 'boots']"
"article.status NOT IN ['draft', 'archived']"

// Combining conditions
"cart.total > 100 AND customer.group = 'vip'"
"article.author = user.id OR user.role = 'editor'"

// Null-coalescing and ternary
"customer.discount ?? 0"
"(customer.score ?? 0) >= 50"
"user.plan = 'pro' ? 'full_access' : 'limited'"

// Functions — math, strings, dates
"round(cart.total * 1.2, 2) < 500"
"dateDiff(today(), article.published_at) <= 30"
"upper(customer.country) IN ['FR', 'BE', 'CH']"

// Multi-criteria, realistic rules
"customer.group = 'vip' AND cart.total >= 100 AND cart.country IN ['FR', 'BE', 'CH']"
"article.status = 'published' AND dateDiff(today(), article.published_at) <= 90 AND article.author = user.id"
"(customer.score ?? 0) >= 50 AND NOT customer.country IN ['IR', 'KP'] AND dateAdd(user.created_at, 30, 'day') < today()"

// Not just boolean rules — expressions can return values
"customer.vip ? round(cart.total * 0.85, 2) : cart.total"
// → float: the discounted price, or the original

"clamp((customer.score ?? 0) / 10, 0, 100)"
// → a numeric score between 0 and 100

"dateDiff(today(), subscription.expires_at) <= 0 ? 'expired' : concat('expires in ', str(dateDiff(today(), subscription.expires_at)), ' days')"
// → string: 'expired' or 'expires in 14 days'

// The same rules, with human-readable aliases registered via AliasResolver:
// "vip customer AND cart amount >= 100 AND shipping country IN ['FR', 'BE', 'CH']"

// You can also register custom functions and call them directly in expressions:
// "is_eligible(customer.score, plan.threshold) AND quota.remaining > 0"
bash
composer