PHP code example of halfowl / statemachine

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

    

halfowl / statemachine example snippets




use Halfowl\StateMachine\{State, StateMachine, StateTransition};

$draft = new State("DRAFT");
$awaitingCopy = new State("AWAITING_COPY_EDIT");
$published = new State("PUBLISHED");

// StateTransitions define legal state transitions for the StateMachine.
// The second parameter of the constructor takes in an array of States
// that the first State can transition to.
$fromDraft = new StateTransition($draft, [$awaitingCopy]);  // draft->awaiting copy
$fromAwaitingCopy = new StateTransition($awaitingCopy, [$draft, $published]);  // awaiting copy->draft/published

// Put that together into a StateMachine:
$sm = new StateMachine(
    transitions: [
        $fromDraft,
        $fromAwaitingCopy,
    ],
    starting: $draft,
);

$sm->current();                  // => DRAFT
$sm->transition($awaitingCopy);  // => AWAITING_COPY_EDIT
$sm->transition($published);     // => PUBLISHED