1. Go to this page and download the library: Download byjg/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/ */
byjg / statemachine example snippets
$stA = new State("A");
$stB = new State("B");
$stC = new State("C");
$stD = new State("D");
use ByJG\StateMachine\TransitionConditionInterface;
$transitionA_B = new Transition($stA, $stB);
$transitionA_C = new Transition($stA, $stC);
$condition = new class implements TransitionConditionInterface {
public function canTransition(?array $data): bool {
return !is_null($data);
}
};
$transitionB_D = new Transition($stB, $stD, $condition);
use ByJG\StateMachine\StateActionInterface;
$action = new class implements StateActionInterface {
public function execute(?array $data): void {
// Execute some operation with the data
// This is the STATE action, not the transition condition
echo "Processing state with: " . json_encode($data);
}
};
$stN = new State('SOMESTATE', $action);
// After autoTransition returns the $stN state object
// You can execute its action:
$resultState = $stateMachine->autoTransitionFrom('STATE', [... data ...]);
$resultState->process(); // This will run the state's action with the data
$condition = new class implements TransitionConditionInterface {
public function canTransition(?array $data): bool {
return isset($data["approved"]) && $data["approved"] === true;
}
};
$transitions = Transition::createMultiple([$from1, $from2], $to, $condition);
$machine = FiniteStateMachine::createMachine()
->addTransitions($transitions);
$stateMachine->possibleTransitions($stA);
// Return null if doesn't exist, otherwise return the object State
$state = $stateMachine->state('OUT_OF_STOCK');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.