See More

> For the complete documentation index, see [llms.txt](https://docs.sonsofphp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonsofphp.com/components/state-machine.md). # State Machine ### Installation ```shell composer require sonsofphp/state-machine ``` ### Usage Basic Usage ```php 'order', 'state_getter' => 'getState', 'state_setter' => 'setState', 'supports' => [ OrderInterface::class, ], 'transitions' => [ 'create' => [ 'from' => 'draft', 'to' => 'new', 'callbacks' => [ 'guard' => [ 'guard-create' => [ 'do' => function () { return true; }, ], ], 'pre' => [ 'pre-create' => [ 'do' => function () { }, ], 'another-pre-create' => [ 'do' => function () {}, ], ], 'post' => [ 'post-create' => [ 'do' => function () {}, ], 'another-post-create' => [ 'do' => function () {}, ], ], ], ], 'fulfill' => [ 'from' => 'new', 'to' => 'fulfilled', ], 'cancel' => [ 'from' => ['draft', 'new', 'fulfilled'], 'to' => 'fulfilled', ], ], ]); // Check if state can change $sm->can($order, 'create'); // Apply transition $sm->apply($order, 'fulfil'); // Get Current State $sm->getState($order); ```