Download the PHP package louishrg/state-flow without Composer
On this page you can find all versions of the php package louishrg/state-flow. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download louishrg/state-flow
More information about louishrg/state-flow
Files in louishrg/state-flow
Package state-flow
Short Description Simple implementation of state machine for Laravel
License MIT
Homepage https://github.com/louishrg/state-flow
Informations about the package state-flow
Laravel State Flow
Simple state machine / resilient states for your Laravel application!
Installation
You can install the package via composer:
Create your states classes with artisan :
Creating files for every available states is repetitive, that's why this package provide an artisan command to speed up the process :
Simple states AKA Stack
A Stack is a simple state machine that doesn't need to register transitions. It's a very convenient way to add a hardcoded type or category to your model.
How to use :
You need at least 1 variable: key which is the real value of the column in the database.
If you want to use other variables as key, you can give the name of the variable in you stateStack creation (see below)
- First parameter is all your available state as an array.
- Second parameter is the default value when creating a model (optional).
- Third parameter is to override the default key-value (optional).
Declare your states classes in the directory of your choice, for example :
Now, add all the needed declaration in your model :
Now you can get your state like so :
If you want to update/create an object with a state:
Laravel Nova:
If you want to use the package in nova, you should use it as following :
Example with a select:
Useful Methods :
If you want to compare the current value of a state with another one, you can use
Also, you can directly get the class of your current state :
If you want to retrieve all your states registered in a namespace as a collection :
Complex States AKA Flow:
If you want to use the real state machine pattern in your app you can add register like so:
Methods for flows :
When using flows, you can check if you can transition to another state like so :
Or you can get all the possible transitions for your current state :
Magic methods for your state :
When you are retrieving rows from your database, you new to instanciate your state to get the key :
In order to simplify the syntax, every State values extend a StateAbstract that provide magic methods :
The magic methods can get you every property defined in your State
Features to come:
- Possibility of using getter & setters in your state classes
- Tests
Testing
Changelog
Please see CHANGELOG for more information about 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
- Louis Harang
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.