1. Go to this page and download the library: Download gomachan46/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/ */
use StateMachine\Annotations as SM;
use StateMachine\Traits\StateMachineTrait;
/**
* state machine annotations ...
*/
class ClassName
{
use StateMachineTrait;
private $status = 'initial status';
...
## use StateMachine;
use StateMachine\Annotations as SM;
use StateMachine\Traits\StateMachineTrait;
/**
* Job
*
* @SM\StateMachine(
* property="status", // write you want to manage states property name
* states={ // all states write here
* @SM\State(name="sleeping"), // isSleeping() is available
* @SM\State(name="running"), // isRunning() is available
* @SM\State(name="cleaning") // isCleaning() is available
* },
* events={ // all events write here
* @SM\Event(
* name="run", // run() and canRun() are available
* transitions={
* @SM\Transition(from="sleeping", to="running")
* }
* ),
* @SM\Event(
* name="clean", clean() and canClean() are available
* transitions={
* @SM\Transition(from="running", to="cleaning")
* }
* ),
* @SM\Event(
* name="sleep", // sleep() and canSleep() are available
* transitions={
* @SM\Transition(from={"running", "cleaning"}, to="sleeping") // "from" can be set multiple state
* }
* )
* }
* )
*/
class Job
{
use StateMachineTrait; // Do not forget it!
/**
* @var string
*/
private $status = 'sleeping'; // write initial state.
/**
* Get status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* StateMachine added methods setStatus() automatically.
* Please be careful if override setStatus() method.
* I recommend that you do not override. (if you manage the state appropriately)
*/
// public function setStatus()
// {
// }
}