sinevia/php-library-business-rule

v1.1.0 2022-03-29 18:17 UTC

This package is auto-updated.

Last update: 2022-04-29 18:33:36 UTC


README

No Dependencies Tests Gitpod Ready-to-Code

A business rule defines or constrains some aspect of business. Given a specified context (data) a business rule always resolves to either true or false.

Formal specification: // Given {context} When {condition(s)} Then {pass} Or {fail}

Usage

  1. Direct usage
$rule = (new BusinessRule())->context([])->condition(function($context){ return true; });

if ($rule->fails()) {
    // Execute fail logic
}

if ($rule->passes()) {
    // Execute pass logic
}
  1. Extend into a separate class. Allows to be re-used (avoid duplication of business logic)
// 1. Specify the business rule class
class AllowAccessRule extends BusinessRule {
    function __construct() {
        $this->condition(function($context){
            return ($context['user']->isEmailConfirmed() AND $context['user']->isActive());
        });
    }
}

// Use the shortcut init function with the context to initialize the rule
if (AllowAccessRule::init(['user'=>$user)->fails()) {
    die('You are not allowed access to this part of the website');
}