1. Go to this page and download the library: Download uuf6429/state-engine 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/ */
uuf6429 / state-engine example snippets
use App\Models\Door; // example model that implements StateAwareInterface
use uuf6429\StateEngine\Implementation\Builder;
use uuf6429\StateEngine\Implementation\Entities\State;
$doorStateManager = Builder::create()
->defState('open', 'Open')
->defState('closed', 'Closed')
->defState('locked', 'Locked')
->defTransition('open', 'closed', 'Close the door')
->defTransition('closed', 'locked', 'Lock the door')
->defTransition('locked', 'closed', 'Unlock the door')
->defTransition('closed', 'open', 'Open the door')
->getEngine(); // you can pass an event dispatcher to the engine here
// find Door 123 (laravel-style repository-model)
$door = Door::find(123);
// close the door :)
$doorStateManager->changeState($door, new State('closed'));
use App\Models\Door; // example model
use uuf6429\StateEngine\Implementation\Builder;
use uuf6429\StateEngine\Implementation\Entities\State;
$door = Door::find(123);
$doorStateMutator = Builder::makeStateMutator(
// define how we get the state
static function () use ($door): State {
if ($door->is_locked) {
return new State('locked');
}
return $door->is_open
? new State('open')
: new State('closed');
},
// define how we set the state
static function (State $newState) use ($door): void {
$door->update([
'is_locked' => $newState->getName() === 'locked',
'is_open' => $newState->getName() === 'open',
]);
}
);
// assumes engine $doorStateManager was already defined
$doorStateManager->changeState($doorStateMutator, new State('closed'));
use App\Models\Turnstile; // example model that implements StateAwareInterface
use uuf6429\StateEngine\Implementation\Builder;
$turnstileStateMachine = Builder::create()
// make states
->defState('locked', 'Impassable')
->defState('open', 'Passable')
// make transitions
->defDataTransition('locked', ['insert_coin'], 'open', 'Coin placed')
->defDataTransition('open', ['walk_through'], 'locked', 'Person walks through')
->getMachine();
$turnstile = Turnstile::find(123);
// put coin in turnstile (notice that the final state is not mentioned)
$turnstileStateMachine->processInput($turnstile, ['insert_coin']);
// now $turnstile will be in "open" state
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.