naghashyan / ngs-state-manager
管理 DTO 状态变更功能
Requires
- php: >=7.3.0
- naghashyan/ngs-php-framework: *
This package is auto-updated.
Last update: 2024-09-27 22:38:05 UTC
README
Naghashyan 状态管理库
此库用于帮助状态管理,它将控制对象状态是否可以从 X -> Y 变更。要创建状态管理实例,您应使用 NgsStateManagerFactory。它有 2 个静态方法:
1. createJsonStateManager
此函数应获取 2 个参数:$statesConfigs,$ngsStateableMapper。$statesConfigs 是一个数组,它将帮助库理解当前状态有哪些后续状态,以及谁可以更改状态。$statesConfigs 的结构应如下所示:
[
"states" => [
[
"name" => "state1", // the name of state, for changing state you should pass name of state as an expecting state change
"description" => "desc1", // the description of state
"nextStates" => [ //nextStates - is array which informs about possible states which can be changed from state 'state1'
[
"name" => "state2", // possible change state name
"userGroups" => ["userGroup1", "userGroup2"] // user goups list who can change to this state
],
[
"name" => "state3",
"userGroups" => ["userGroup1"]
]
],
"beforeActions": ["URL1", "URL2"], //list of urls which will be called before state change functional
"afterActions": ["URL3"] //list of urls which will be called after state change functional
],
[
"name" => "state2",
"description" => "desc2",
"nextStates" => [
{
"name" => "state3",
"userGroups" => ["userGroup2"]
}
],
"beforeActions" => [],
"afterActions" => ["URL3"]
],
[
"name" => "state3",
"description" => "desc3",
"nextStates" => [],
"beforeActions" => ["URL4"],
"afterActions" => []
]
]
]
$ngsStateableMapper is mapper of object, to work with this library, mapper should implement INgsStateableMapper interface
createJsonStateManager function after call will return instance of NgsJsonStateManager which has method changeState($stateableDto, string $newStateName, string $userGroup)
$stateableDto is dto object which should implement INgsStateableDto interface
changeState method will return boolean if state changed or not or can throw exception NgsStateException
2. createDbStateManager
此函数应获取 $ngsStateableMapper 作为参数。$ngsStateableMapper 是对象映射器,为了与该库一起工作,映射器应实现 INgsStateableMapper 接口。
调用 createDbStateManager 函数后,将返回 NgsDBStateManager 的实例。此管理器使用表 ngs_states 来理解状态变更规则。表的行应包含以下列:
name - 状态名称,description - 状态描述,next_states - 与上面描述的 'nextStates' 相同结构的 JSON,before_actions - 在状态变更功能之前调用的 URL 数组的 JSON,after_actions - 在状态变更功能之后调用的 URL 数组的 JSON
NgsDBStateManager 有方法 changeState($stateableDto, string $newStateName, string $userGroup),其中 $stateableDto 是实现 INgsStateableDto 接口的 dto 对象。
changeState 方法将返回布尔值,表示状态是否已更改,或者可以抛出 NgsStateException 异常。