Download the PHP package mouadziani/xstate without Composer
On this page you can find all versions of the php package mouadziani/xstate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mouadziani/xstate
More information about mouadziani/xstate
Files in mouadziani/xstate
Package xstate
Short Description State machine library to play with any complex behavior of your PHP objects
License MIT
Homepage https://github.com/mouadziani/xstate
Informations about the package xstate
XState - State Machine for PHP
XState is a state machine library to play with any complex behavior of your PHP objects (inspired by xstate.js)
Installation
The recommended way to install Xstate is through Composer
Define state machine workflow
Let's say we want to define a state machine workflow for a video object, generally a video may have 3 states (playing, stopped, paused),
as a first step you have to create a new object from StateMachine
class
Then you have to define the allowed states as well as the default state
And finally the transitions
The Transition
class expect 3 required params:
- Trigger: As a name of the transition which will be used to trigger a specific transition (should be unique)
- From: Expect a string for a single / or array for multiple initial allowed states
- To: Expect string which is the next target state (should match one of the defined allowed states)
Guards (optional)
You can either define a guard callback for a specific transition using guard
method, which must return a bool. If a guard returns false, the transition cannot be performed.
💡 You can define the whole workflow using a single statement:
Work with states & transitions
Trigger transition
There are two ways to trigger a specific defined transition
1- Using transitionTo
method and specify the name of the transition as an argument
2- Or just calling the name of the transition from your machine object as a method
Occasionally triggering a transition may throw an exception if the target transition is not defined /or not allowed:
Get the current state
Get the allowed transitions
Adding in-demand transition
Upcoming features
- [x] Add the ability to define guard for a specific transition
- [ ] Define/handle hooks before/after triggering transition
Testing
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Mouad Ziani
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
featured_repository