承接 halfowl/statemachine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

halfowl/statemachine

最新稳定版本:v1.0.0

Composer 安装命令:

composer require halfowl/statemachine

包简介

State Machines made safe and easy.

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require

State Machines in PHP made safe and easy.

Installation

Using Composer:

$ composer require halfowl/statemachine

Example

Imagine an application that holds the state of an article. It has the following states:

  • Draft
  • Awaiting Copy Edit
  • Published

A new article starts in "Draft", gets progressed to "Awaiting Copy Edit", and subsequently "Published". An article in "Awaiting Copy Edit" can go back to being a "Draft".

With this library, you can model the above as:

<?php

use Halfowl\StateMachine\{State, StateMachine, StateTransition};

$draft = new State("DRAFT");
$awaitingCopy = new State("AWAITING_COPY_EDIT");
$published = new State("PUBLISHED");

// StateTransitions define legal state transitions for the StateMachine.
// The second parameter of the constructor takes in an array of States
// that the first State can transition to.
$fromDraft = new StateTransition($draft, [$awaitingCopy]);  // draft->awaiting copy
$fromAwaitingCopy = new StateTransition($awaitingCopy, [$draft, $published]);  // awaiting copy->draft/published

// Put that together into a StateMachine:
$sm = new StateMachine(
    transitions: [
        $fromDraft,
        $fromAwaitingCopy,
    ],
    starting: $draft,
);

$sm->current();                  // => DRAFT
$sm->transition($awaitingCopy);  // => AWAITING_COPY_EDIT
$sm->transition($published);     // => PUBLISHED

API

(Proper auto-generated docs is WIP, tracking in #4)

State

Reference: https://github.com/half0wl/php-StateMachine/blob/main/src/StateInterface.php

  • getName(): string

StateMachine

Reference: https://github.com/half0wl/php-StateMachine/blob/main/src/StateMachineInterface.php

  • current(): State
  • can(): bool
  • is(State $s): bool
  • transition(State $next): void

StateTransition

Reference: https://github.com/half0wl/php-StateMachine/blob/main/src/StateTransitionInterface.php

  • src(): State
  • dsts(): State[]
  • inDst(): bool

License

MIT

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-14