segfaultinc / finite
Simple yet powerful Finite State Machine implementation
Installs: 27 128
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^7.2
- opis/closure: ^3.4
Requires (Dev)
- graphp/graphviz: ^0.2.1
- mockery/mockery: ^1.2
- phpunit/phpunit: ^8.0
Suggests
- graphp/graphviz: Allows to visualize graph using GraphViz
This package is auto-updated.
Last update: 2021-10-12 19:51:30 UTC
README
Usage
$order = new Order(); // see below $graph = new Graph(); // see below $graph->apply($order, 'transition-input');
use SegfaultInc\Finite\Subject; class Order extends Model implements Subject { public function setFiniteState($state) { $this->update([ 'status' => $state, ]); } public function getFiniteState($state) { return $this->status; } }
Graph definition
(new Graph) ->setStates([ State::initial('new') ->label('New') ->leaving(function (Order $order) { // This is executed every time the `in_progress` is **transitioned from**. // Useful if you want to do something that is "state" dependent $order->update([ 'started_at' => now() ]); }), State::normal('in_progress') ->label('In Progress'), State::final('finished') ->label('Finised'), State::final('canceled') ->label('Canceled') ->entering(function (Order $order) { // If you transition to "canceled" state from multiple states, // this is the place to perform the common logic }), ]) ->setTransitions([ Transition::make('new', 'in_progress', 'progress') ->pre(function (Order $order) { if ($order->validate()) { throw new OrderValidationException('When an exception is thrown in "pre" hook, the transition is not applied'); } }), Transition::make('in_progress', 'finished', 'progress'), Transition::make('in_progress', 'canceled', 'cancel') ->post(function (Order $order) { $order->user->notify(new YourOrderWasCanceled); }), ]);
Visualization
$graph->visualize() // uses GraphViz under the hood