mstovicek/state-machine

此包已被废弃,不再维护。未建议替代包。
此包最新版本(dev-master)没有提供许可证信息。

简单的状态机

dev-master 2018-01-16 13:42 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:11:53 UTC


README

创建一个库,该库将验证状态之间的转换,并使用它实现以下广告状态的图

               ,---------.
      |------> | Limited |
      |        `---------'
      |            |
      |            ˅
   ,-----.     ,--------.     ,----------.     ,---------.
   | New | --> | Active | --> | Outdated | --> | Removed |
   `-----'     `--------'     `----------'     `---------'
                   ˄               |
                   |---------------|

包仅用于演示目的!

使用方法

作为项目依赖项安装

$ composer require mstovicek/state-machine

类和方法

\StateMachine\StateMachine 实现了 \StateMachine\StateMachineInterface,具有以下方法

  • allowStates - 接受一个允许状态的数组作为唯一参数,仅允许此类状态之间的转换
  • allowTransition - 接受两个参数 - fromto - 并在它们之间注册转换
  • canTransit - 接受两个参数 - fromto - 并返回布尔值 true 如果允许此类转换,否则返回 false

开发者使用

安装依赖项

$ composer install

运行测试

$ composer tests

运行示例

$ cd examples/user-account

# install dependencies, incl. mstovicek/state-machine
$ composer install

# get current state
$ php index.php get

# set state
$ php index.php set <state>

# reset state to default
$ php index.php reset

示例输出

$ php index.php get
Current state: new

$ php index.php set active
State was set to: active

$ php index.php get
Current state: active

$ php index.php set outdated
State was set to: outdated

$ php index.php get
Current state: outdated

$ php index.php set limited
Cannot set state to limited! Check current state and allowed transitions.

$ php index.php get
Current state: outdated

$ php index.php set removed
State was set to: removed

$ php index.php get
Current state: removed
This state is terminal.

$ php index.php set active
Cannot set state to active! Check current state and allowed transitions.

$ php index.php reset
State was reset to default

$ php index.php get
Current state: new