https://github.com/half0wl/php-statemachine
https://github.com/half0wl/php-statemachine
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/half0wl/php-statemachine
- Owner: half0wl
- License: mit
- Created: 2021-10-23T19:14:24.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-14T17:09:05.000Z (over 3 years ago)
- Last Synced: 2025-04-02T03:46:00.224Z (over 1 year ago)
- Language: PHP
- Size: 43.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-StateMachine
[](https://packagist.org/packages/halfowl/statemachine) [](https://packagist.org/packages/halfowl/statemachine) [](https://packagist.org/packages/halfowl/statemachine) [](https://packagist.org/packages/halfowl/statemachine)
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
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](LICENSE)