sinevia / php-library-business-rule
PHP Library Business Rule
Installs: 66
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 2
Open Issues: 0
pkg:composer/sinevia/php-library-business-rule
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-09-29 02:50:04 UTC
README
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
- Direct usage
$rule = (new BusinessRule())->context([])->condition(function($context){ return true; }); if ($rule->fails()) { // Execute fail logic } if ($rule->passes()) { // Execute pass logic }
- 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'); }