sinevia / php-library-business-rule
PHP 库商业规则
v1.1.0
2022-03-29 18:17 UTC
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-29 05:43:14 UTC
README
商业规则定义或限制商业的某些方面。在给定的指定上下文(数据)中,商业规则始终解析为真或假。
形式化规范:// Given {context} When {condition(s)} Then {pass} Or {fail}
用法
- 直接使用
$rule = (new BusinessRule())->context([])->condition(function($context){ return true; }); if ($rule->fails()) { // Execute fail logic } if ($rule->passes()) { // Execute pass 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'); }