davispeixoto / workflow
A PHP package for managing state transitions
Requires
- php: >=7.1
- myclabs/php-enum: 1.*
Requires (Dev)
- behat/behat: ~3.0
- friendsofphp/php-cs-fixer: ^2.11
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ~0.8
- phpunit/phpunit: ~6.2
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-10-24 22:22:46 UTC
README
A PHP package for dealing with state transitions.
State transitions are a good way to manage lifecycles and pipelines on applications, as for example:
- Order and payment status on an e-commerce
- Invoice Status on a finance system
- Ticket Status on ticket service desk system
- Sales status on a CRM
Installation
The workflow package can be installed via Composer by requiring the
davispeixoto/workflow
package in your project's composer.json
.
{ "require": { "davispeixoto/workflow": "~1.0" } }
Or
$ php composer.phar require davispeixoto/workflow
And running a composer update from your terminal:
php composer.phar update
Usage
To use it, first you need to create the status you are going to use for representing your states.
<?php use MyCLabs\Enum\Enum; class SalesStates extends Enum { public const NEW = 'new'; public const DEALING = 'dealing'; public const WON = 'won'; public const LOST = 'lost'; }
Then you can create your workflow based on valid transitions
<?php use Davispeixoto\WorkflowInterface\Transition; use Davispeixoto\WorkflowInterface\WorkflowInterface; class SalesWorkflow extends WorkflowInterface { public function __construct(SalesStates $initialStatus) { parent::__construct($initialStatus); // setup the transitions $transitions = []; $transitions[] = new Transition( new SalesStates(SalesStates::NEW), new SalesStates(SalesStates::DEALING) ); $transitions[] = new Transition( new SalesStates(SalesStates::DEALING), new SalesStates(SalesStates::WON) ); $transitions[] = new Transition( new SalesStates(SalesStates::DEALING), new SalesStates(SalesStates::LOST) ); $this->allowedTransitions = $transitions; // setup the finished status, if any/needed $finished = []; $finished[] = new SalesStates(SalesStates::WON); $finished[] = new SalesStates(SalesStates::LOST); $this->finishedStatus = $finished; } }
Now you can use this workflow to manage state transitions on you applications.
License
This software is licensed under the MIT license
Versioning
This project follows the Semantic Versioning
Thanks
For all PHP community