PHP code example of szykra / state-machine

1. Go to this page and download the library: Download szykra/state-machine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

szykra / state-machine example snippets


$stateDraft = new State('draft');
$statePublished = new State('published');

$stateDraft->addTransition('publish', 'draft', 'published');

$transitionReject = new Transition('reject', 'published', 'rejected');
$statePublished->putTransition($transitionReject);

$stateMachine = new StateMachine();

// api inconsistent, should called putState
$stateMachine->addState($stateDraft);
$stateMachine->addState($statePublished);

$document = new Document();
$stateMachine->initialize($document);

$stateMachine->can('publish'); // true
$stateMachine->can('reject'); // false

echo $document->getState();    // draft
$stateMachine->run('publish');
echo $document->getState();    // published