PHPackages                             hongxunpan/business-decision - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hongxunpan/business-decision

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hongxunpan/business-decision
============================

Business decision and denial reason primitives for PHP applications

0.1.0(1mo ago)06MITPHPPHP ^8.0.20

Since May 29Pushed 1mo agoCompare

[ Source](https://github.com/HongXunPan/business-decision)[ Packagist](https://packagist.org/packages/hongxunpan/business-decision)[ Docs](https://github.com/HongXunPan/business-decision)[ RSS](/packages/hongxunpan-business-decision/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (2)Used By (0)

business-decision
=================

[](#business-decision)

`hongxunpan/business-decision` 用于表达业务动作是否允许执行，以及不允许时的结构化原因。

- GitHub 仓库：`HongXunPan/business-decision`
- Composer 包：`hongxunpan/business-decision`
- PHP 命名空间：`HongXunPan\BusinessDecision\`

定位
--

[](#定位)

本包承接中性的业务决策原语：

- `BusinessDecision`：表达业务动作 `allowed / denied` 的决策结果；
- `BusinessError`：表达单个结构化拒绝原因，可被业务项目扩展或包装；
- `BusinessErrorCatalog`：从定义表创建 `BusinessError`，并可生成 `BusinessDecision`；
- `BusinessDecisionException`：shared 默认异常；
- `UndefinedBusinessErrorException`：未定义业务错误码异常。

本包不承接：

- HTTP response；
- simple-framework 的 `ApiException`；
- ORM / DB 查询；
- 权限系统；
- 具体业务错误文案；
- 具体业务 Ability 规则；
- `DecisionGuard` / `BusinessErrorGuard`；
- `denyUnless()` / `whenAllow()`。

基本用法
----

[](#基本用法)

```
use HongXunPan\BusinessDecision\BusinessDecision;
use HongXunPan\BusinessDecision\BusinessError;

$error = new BusinessError(
    'publish_status_invalid',
    '当前状态不允许发布',
    ['status' => 'archived']
);

$decision = BusinessDecision::denied($error);

if ($decision->isDenied()) {
    $blocker = $decision->requireError()->toBlocker();
}
```

允许结果使用：

```
$decision = BusinessDecision::allowed();
$decision->throwIfDenied();
```

拒绝结果默认抛出 `BusinessDecisionException`：

```
$decision = BusinessDecision::denied($error);
$decision->throwIfDenied();
```

错误目录
----

[](#错误目录)

```
use HongXunPan\BusinessDecision\BusinessErrorCatalog;

final class AnnouncementErrorCatalog extends BusinessErrorCatalog
{
    public const PUBLISH_STATUS_INVALID = 'publish_status_invalid';

    protected const DEFINITIONS = [
        self::PUBLISH_STATUS_INVALID => [
            'message' => '当前状态不允许发布',
            'extra' => [
                'field' => 'publish_status',
            ],
        ],
    ];
}

$error = AnnouncementErrorCatalog::when(
    $cannotPublish,
    AnnouncementErrorCatalog::PUBLISH_STATUS_INVALID
);

$decision = AnnouncementErrorCatalog::decisionWhen(
    $cannotPublish,
    AnnouncementErrorCatalog::PUBLISH_STATUS_INVALID
);
```

与业务 Ability 的关系
---------------

[](#与业务-ability-的关系)

具体业务 `Ability` 仍应留在业务项目内。推荐业务项目在自己的 `BaseAbility` 中提供：

- `denyWhen(...)`；
- `assertAllowed(BusinessDecision $decision)`；
- 项目异常适配，例如转换成 `ApiException`。

本包只表达“决策结果”和“拒绝原因”，不判断具体资源动作是否允许。

验证
--

[](#验证)

```
composer validate --strict
composer test
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance89

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

57d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/37c4ffcd9218f6b9722875da1ec37f20b0118651687de5798972f919bd2c1eb4?d=identicon)[HongXunPan](/maintainers/HongXunPan)

---

Top Contributors

[![HongXunPan](https://avatars.githubusercontent.com/u/25816508?v=4)](https://github.com/HongXunPan "HongXunPan (2 commits)")

---

Tags

business rulebusiness-decisionbusiness-errordomain-decision

### Embed Badge

![Health badge](/badges/hongxunpan-business-decision/health.svg)

```
[![Health](https://phpackages.com/badges/hongxunpan-business-decision/health.svg)](https://phpackages.com/packages/hongxunpan-business-decision)
```

###  Alternatives

[chelout/laravel-relationship-events

Missing relationship events for Laravel

5272.5M18](/packages/chelout-laravel-relationship-events)[sroze/kubernetes-client

123.1k](/packages/sroze-kubernetes-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
