PHP code example of zenithsu / easy-rules
1. Go to this page and download the library: Download zenithsu/easy-rules 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/ */
zenithsu / easy-rules example snippets
#[Rule(name: 'weather rule', description: 'if it rains then take an umbrella')]
class WeatherRule
{
#[Condition]
public function itRains(#[Fact(value: 'rain')] $fact, Facts $facts)
{
return $fact;
}
#[Action]
public function takeAnUmbrella(): void
{
echo 'It rains, take an umbrella!';
}
}
$facts = new Facts();
$facts->put('rain', true);
$weatherRule = new WeatherRule();
$rules = new Rules();
$rules->register($weatherRule);
$ruleEngine = new DefaultRulesEngine();
$ruleEngine->fire($rules, $facts);