PHP code example of slava-basko / finite-state-machine
1. Go to this page and download the library: Download slava-basko/finite-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/ */
slava-basko / finite-state-machine example snippets
$stateDraft = new State('draft');
$stateReview = new State('review');
$stateApproved = new State('approved');
$stateRejected = new State('rejected');
$stateEditing = new State('editing');
$statePublished = new State('published');
$transitionToReview = new Transition('to_review', [$stateDraft, $stateEditing], $stateReview);
$transitionToApproved = new Transition('to_approved', [$stateReview], $stateApproved);
$transitionToRejected = new Transition('to_rejected', [$stateReview], $stateRejected);
$transitionToEditing = new Transition('to_editing', [$stateReview], $stateEditing);
$transitionToPublished = new Transition('to_published', [$stateApproved], $statePublished);
class Post {
public $state = 'draft';
}
$stateMachine = new StateMachine(
function (Post $post) {
return new State($post->state);
},
function (Post $post, StateInterface $newState) {
$post->state = $newState->getName();
}
);
$stateMachine->addTransitions([
$transitionToReview,
$transitionToApproved,
$transitionToRejected,
$transitionToEditing,
$transitionToPublished,
]);
$post = new Post();
$stateMachine->transition('to_review', $post); // Ok
$stateMachine->transition('to_published', $post); // NoSuitableTransitionException
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.