PHP code example of dddphp / state-machine
1. Go to this page and download the library: Download dddphp/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/ */
dddphp / state-machine example snippets
$builder = StateMachineBuilderFactory::create();
$builder->externalTransition()
->from(self::STATEA)
->to(self::STATEB)
->on(self::EVENTGoToB)
->when($this->checkCondition())
->perform($this->doAction());
$builder->internalTransition()
->within(self::STATEA)
->on(self::INTERNAL_EVENT)
->when($this->checkCondition())
->perform($this->doAction());
$builder->externalTransition()
->from(self::STATEC)
->to(self::STATED)
->on(self::EVENTGoToD)
->when(
new class () implements ConditionInterface {
public function isSatisfied($context): bool
{
echo "Check condition : " . $context . "\n";
return true;
}
public function name(): string
{
return '';
}
})
->perform(
new class () implements ActionInterface {
public function execute($from, $to, $event, $context): void
{
echo $context . " from:" . $from . " to:" . $to . " on:" . $event;
}
};
);
$stateMachine = $builder->build(self::MACHINE_ID);
$target = $stateMachine->fire(self::STATE1, self::EVENT1, $this->context);