1. Go to this page and download the library: Download cosma/simple-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/ */
cosma / simple-state-machine example snippets
{
" "cosma/simple-state-machine": "1.0.*"
}
}
namespace \MyProject;
/**
* Simple State Machine
*/
$priceStateMachine = \Cosma\SimpleStateMachine\StateMachine('Price Calculator State Machine');
/**
* Your Data object which can be modify by the State Machines
* Has to implement the interface \Cosma\SimpleStateMachine\InterfaceData
*/
$price = new \YourProject\Price();
/**
* Start State of the State Machine
* Has to extends the abstract \Cosma\SimpleStateMachine\AbstractState
*/
$initialPriceState = \YourProject\PriceStateMachine\States\InitialPrice($price);
/**
* Simple State Machine cannot run without setting the start State
*/
$priceStateMachine->setState($initialPriceState);
/**
* Running the State Machine
* During this process the Data object will be modified depending on teh configuration of the Machine
*/
$priceStateMachine->run();
/**
* Retrieve the Data object at the end of the process
*/
$finalPrice = $priceStateMachine->getState()->getData();
/**
* Generate the Diagram of the State Machine.
* Choose the format
*/
$graphic = new Graphic('svg');
$diagramSVG = $priceStateMachine->draw($graphic);
echo $diagramSVG;
namespace \MyProject\PriceStateMachine;
class Price implements \Cosma\SimpleStateMachine\InterfaceData
{
/**
* @var float
*/
private $value;
public function __constructor()
{
$this->value = $this->getPriceFromDB();
}
/**
* getters, setters and other functions
*/
...
}
namespace \MyProject\PriceStateMachine\States;
class AddVATState extends \Cosma\SimpleStateMachine\AbstractState
{
/**
* Set the label for this State used in State Machine diagram
*/
public function getLabel()
{
return 'Add VAT Tax';
}
/**
* Modify the Data object
*/
protected function process()
{
$price = $this->getData();
$price->setValue($price->getValue() * 1.19);
...
}
/**
* Configure the Transitions from this State to another States or itself in case of a loop
* You may set in what Condition that Transition takes place
* The order to check upon the validity of conditions and forward to next State is from up to down
*/
protected function configureAvailableTransitions()
{
$this->addTransition(
'\YourProject\PriceStateMachine\States\AddDiscount',
'\YourProject\PriceStateMachine\Conditions\IfGreaterThan1000'
);
$this->addTransition('NewStateClass', 'ConditionClass');
$this->addTransition('\YourProject\PriceStateMachine\States\AddDiscount');
...
}
}
namespace namespace \MyProject\PriceStateMachine\Conditions;
class SomeWildCondition extends \Cosma\SimpleStateMachine\AbstractCondition
{
/**
* @return string
*/
public function getLabel()
{
return "Some Wild Condition";
}
/**
* @return bool
*/
public function isTrue()
{
$data = $this->getData();
return $this->checkSomething($data);
}
...
}
namespace \MyProject;
/**
* Generate the Diagram of the State Machine.
* Choose the format
*/
$graphic = new Graphic('svg');
$diagramSVG = $priceStateMachine->draw($graphic);
echo $diagramSVG;
namespace \MyProject\PriceStateMachine\States;
class MyState extends \Cosma\SimpleStateMachine\AbstractState
{
...
/**
* An array of DOT attributes to overwrite the default style of a State/Condition
*/
protected $styleAttributes = array(
'fillcolor' => '#A8CE9F',
'style' => 'filled',
'fontcolor' => '#000000',
'fontsize' => 12,
'penwidth' => 1,
);
...
}