Download the PHP package juststeveking/state-machine without Composer
On this page you can find all versions of the php package juststeveking/state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download juststeveking/state-machine
More information about juststeveking/state-machine
Files in juststeveking/state-machine
Package state-machine
Short Description A small, framework-agnostic PHP state machine for modeling valid transitions between states.
License MIT
Informations about the package state-machine
State Machine
A small, framework-agnostic PHP state machine for modeling valid transitions between states.
It is built around three contracts:
StateContractTransitionContractStateMachineContract
You define your own states, transitions, and domain events. The package validates transitions and throws clear exceptions when a transition is invalid.
Requirements
- PHP 8.5+
Installation
30-Second Quick Start
If you already have your own states and transitions, this is the smallest working flow:
How It Works
When you attempt a transition, the package checks:
- Is this transition class registered on the machine?
- Is the current state one of the transition's allowed
from()states? - Does the transition
guard()allow it? - If all pass, create and return the domain event from
eventClass().
If any check fails, an InvalidTransitionException is thrown.
Quick Start
This example models publishing a blog post from draft to published.
1. Create your states
2. Create your event
3. Create a transition
4. Create a machine adapter for your model/entity
5. Run a transition
Second Example: Publish And Archive
This example shows one machine with multiple transitions and state-dependent rules.
Use this when you want to support a lifecycle like draft -> published -> archived.
Exception Messages
InvalidTransitionException includes named constructors and clear messages for:
- Transition class is not registered on the machine
- Current state is not allowed by the transition
- Guard rejected the transition
This makes it easy to return user-friendly validation errors or audit denied actions.
Important Notes
- Transition registration is class-based, not instance-based.
- If the machine registers
PublishPost::class, anynew PublishPost()instance is accepted. - A different class with similar logic is still rejected as not registered.
- If the machine registers
guard()must returnnullto allow transition.- Any non-null string from
guard()denies transition and becomes part of the exception message.
Testing
If you are contributing to this package:
License
MIT