pengboomouch/regulus-php

Small rule library.

dev-main 2023-03-22 09:15 UTC

This package is auto-updated.

Last update: 2025-05-22 13:56:55 UTC


README

Regulus Php Version - Lightweight rule organization.

Install

composer require pengboomouch/regulus-php

Example

// Define some rules and conditions
$disableRow = new \DisableRowRule(
    new SomeRowCondition(),
    new SomeSecurityCondition()
);

// Init a group and add all rules to it
$regulus = new \Regulus\Regulus();
$regulus->createGroup('row_rules');
$regulus->addRuleTo('row_rules', $disableRow);

Resolve all rules

$finalResult = $regulus->resolveAll();

if ($finalResult->isFulfilled()) {
    // Do something final
    // ...
} else {
    $failedRules = $finalResult->getFailedRules();
    $failedConditions = $failedRules->getFailedConditions();
    
    // Log or do something else
}

Resolve a specific rule

$disableRowResult = $regulus->resolveRuleIn('row_rules', DisableRowRule::class);
if ($disableRowResult->isFulfilled()) {
    // Disable the row
    // ...
}

Resolve specific group

$rowRuleGroupResult = $resolver->resolveGroup('row_rules');
if ($rowRuleGroupResult->isFulfilled()) {
    // Do something to the rows
    // ...
}

Create conditions

Define acceptance conditions to be fulfilled.

class MyCondition implements \Regulus\Interface\Condition
{
    // Inject all repositories or services you need
    public function __construct(private SomeService $someService)
    {}
    
    public function isFulfilled(): bool
    {
        // Determine if the condition is fulfilled
        // ...
        
        return true;
    }
}

Create rules

A rule can have multiple conditions. You can decide for yourself when to return a fail or success result.

class MyRule extends \Regulus\Core\AbstractRule
{
    // Inject all needed conditions for this rule
    public function __construct(private MyCondition $someRowCondition)
    {}

    public function resolve(): RuleResult
    {
        $succeededConditions = [];
        $failedConditions = [];
        
        // Determine if the result is fulfilled
        if(!$this->someRowCondition->isFulfilled()) {
            $isFulfilled = false;
            $failedConditions[] = $this->someRowCondition;
        } else {
            $isFulfilled = true;
            $succeededConditions[] = $this->someRowCondition;
        }

        return $this->createResult(
            $isFulfilled,
            self,
            $succeededConditions,
            $failedConditions
        );
    }
}

Logs

You can track all conditions for debugging.

    $allConditions = $disableRowResult->getAllConditions();
    
    $fulfilledConditions = $disableRowResult->getFulfilledConditions();
    
    $failedConditions = $disableRowResult->getFailedConditions();