PHP code example of particle / state

1. Go to this page and download the library: Download particle/state 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/ */

    

particle / state example snippets


$initialState = State::withName('pending');

$machine = StateMachine::withStates($initialState, StateCollection::withStates([
    State::withName('pending'),
    State::withName('started'),
    State::withName('accepted'),
    State::withName('rejected'),
    State::withName('completed'),
    State::withName('failed'),
]));

$machine->addTransition(Transition::withStates('start', ['pending'], 'started'));
$machine->addTransition(Transition::withStates('accept', ['started'], 'accepted'));

$machine->addListener(function (Event\TransitionApplied $event) {
    if ($event->wasFromState(State:withName('pending')) {
        echo "The state was pending. Not anymore though :)";
    }
});

var_dump($machine->canApplyTransition('accept')); // bool(false), because not in 'started' state;
var_dump($machine->canApplyTransition('start')); // bool(true);
var_dump($machine->transition('start')); // bool(true);
var_dump($machine->canApplyTransition('accept')); // bool(true);
var_dump($machine->canApplyTransition('start')); // bool(false);