Download the PHP package tamkeen-tech/laravel-enum-state-machine without Composer
On this page you can find all versions of the php package tamkeen-tech/laravel-enum-state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tamkeen-tech/laravel-enum-state-machine
More information about tamkeen-tech/laravel-enum-state-machine
Files in tamkeen-tech/laravel-enum-state-machine
Package laravel-enum-state-machine
Short Description Control your state using enums
License MIT
Informations about the package laravel-enum-state-machine
Introduction
This package simplify controlling the transition between model states, allowing you to prevent unlogically transition and also controlling the initial state of the model using the PHP enums. Each enum allows you to define your states, the allowed transitions and the initial state, all in one place.
Laravel Enum State Machine, also allows you to automatically record the history of the state transition for each define state machine.
This package is built to help developers with the least amount of work needed to get it working.
Installation
You can install the package via composer:
Next, if you going to record the history
To publish the config file, you can use the following command:
Usage
Imagine you have bill with statuses, so at your bill model you will need to use HasStateMachines
trait, and define your protected $stateMachines
array variable to include all the fields that you going to apply the state machine on, and finally an optional variable protected $recordStateHistory
boolean to either save history or not.
In your BillStatus
class you need to use trait StateMachine
and to add two methods transitions
to define your transition array and initialState
to define the allowed initial states, both must return array.
From the example above the allowed transitions for PENDING
case are PAID
and EXPIRED
, and the initail state for bill must be only PENDING
Now you are all done, you can start writing your code safely. So now if your tried to create a bill with different state you will receive an exception.
Example
-
create bill with different initail state
- try to update to invalid transition
State History Recording
When you enable state history recording by setting protected $recordStateHistory = true
in your model, the package will automatically track all state changes for the fields defined in $stateMachines
.
Each state change will be recorded in the state_machine_histories table with the following information:
- The previous state (from)
- The new state (to)
- The field name that changed
- The model type and ID
- Timestamps of the change
You can access the state history through the stateHistory relationship:
Flow Diagram Generation
You can generate a flow diagram of your state machine using the following command:
You can specify a custom output directory using the --output option
This command will create a visual representation of your state machine as a PNG file, making it easier to understand the transitions between states.
Helper Methods
The StateMachine
trait provides several helper methods to make working with state transitions easier.
canTransitTo
This method checks if the current state can transition to a given state.
Example
inInitialState
This method checks if the current state is one of the allowed initial states.
Example
is
This method checks if the current state is equal to a given state.
Example
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.