PHP code example of philiprehberger / php-rule-engine
1. Go to this page and download the library: Download philiprehberger/php-rule-engine 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/ */
philiprehberger / php-rule-engine example snippets
use PhilipRehberger\RuleEngine\RuleEngine;
$result = RuleEngine::create()
->rule('adult')
->when('age', '>=', 18)
->then(fn ($ctx) => "Welcome, {$ctx['name']}!")
->build()
->evaluate(['name' => 'Alice', 'age' => 25]);
$result->hasMatches(); // true
$result->first()->actionResult; // "Welcome, Alice!"
$result = RuleEngine::create()
->rule('vip-discount')
->when('role', '=', 'vip')
->andWhen('total', '>=', 100)
->then(fn () => ['discount' => 0.2])
->build()
->rule('bulk-discount')
->when('quantity', '>=', 50)
->orWhen('total', '>=', 500)
->then(fn () => ['discount' => 0.1])
->build()
->evaluate(['role' => 'vip', 'total' => 150, 'quantity' => 10]);
$engine = RuleEngine::create()
->rule('active-non-banned')
->when('active', '=', true)
->notWhen('status', '=', 'banned')
->then(fn () => 'access granted')
->build();
$engine = RuleEngine::create()
->rule('high-priority')
->when('value', '>', 0)
->then(fn () => 'important')
->priority(10)
->stopOnMatch()
->build()
->rule('low-priority')
->when('value', '>', 0)
->then(fn () => 'fallback')
->priority(1)
->build();
$result = $engine->evaluate(['value' => 5]);
// Only 'high-priority' matches because stopOnMatch is set
$result = $engine->evaluateFirst(['value' => 5]);
// Returns a single RuleResult or null
$result = RuleEngine::create()
->rule('city-check')
->when('user.address.city', '=', 'Vienna')
->then(fn () => 'local')
->build()
->evaluate([
'user' => [
'address' => ['city' => 'Vienna'],
],
]);
$compiled = RuleEngine::create()
->rule('adult')
->when('age', '>=', 18)
->then(fn () => 'allowed')
->build()
->compile();
// Evaluate multiple contexts with pre-compiled closures
$result = $compiled->evaluate(['age' => 25]);
$first = $compiled->evaluateFirst(['age' => 25]);
$result = RuleEngine::create()
->rule('a')
->when('x', '>', 0)
->then(fn () => 'matched')
->build()
->withAudit()
->evaluate(['x' => 5]);
$result->auditEntries(); // Array of AuditEntry objects
$result->evaluatedCount(); // Number of rules evaluated
$result->skippedCount(); // Number of rules skipped
$engine = RuleEngine::create();
$engine->addRule(new Rule('no-conditions', [], fn () => 'x'));
$warnings = $engine->validate();
// ["Rule 'no-conditions' has no conditions and will always match."]
use PhilipRehberger\RuleEngine\Contracts\ContextAccessor;
class EloquentAccessor implements ContextAccessor
{
public function get(mixed $context, string $path): mixed
{
return data_get($context, $path);
}
public function has(mixed $context, string $path): bool
{
return data_get($context, $path) !== null;
}
}
$engine = RuleEngine::create(new EloquentAccessor());
bash
composer