Skip to content

Instantly share code, notes, and snippets.

@tortuetorche
Last active December 21, 2015 18:59
Show Gist options
  • Save tortuetorche/6351092 to your computer and use it in GitHub Desktop.
Save tortuetorche/6351092 to your computer and use it in GitHub Desktop.
Finite Trait
<?php
/**
* The Finite Trait.
* It provides easy ways to deal with Stateful objects and StateMachine
* Prerequisite: install Finite package (https://github.com/yohang/Finite#readme)
*
* @author Tortue Torche <tortuetorche@spam.me>
*/
trait FiniteTrait
{
/**
* @var \Finite\StateMachine\StateMachine
*/
protected $stateMachine;
public function setFiniteState($state)
{
$this->state = $state;
}
public function getFiniteState()
{
return $this->state;
}
/**
* @return \Finite\StateMachine\StateMachine
*/
protected function getStateMachine()
{
return $this->stateMachine;
}
/**
* @return \Finite\State\State
*/
public function getCurrentState()
{
return $this->getStateMachine()->getCurrentState();
}
/**
* @return string
*/
public function getState()
{
return $this->getCurrentState()->getName();
}
/**
* @return array<string>
*/
public function getTransitions()
{
return $this->getCurrentState()->getTransitions();
}
/**
* @return array<string>
*/
public function getProperties()
{
return $this->getCurrentState()->getProperties();
}
/**
* @param string $property
*
* @return bool
*/
public function hasProperty($property)
{
return $this->getCurrentState()->has($property);
}
// Helpers
/**
* @param string $targetState
*
* @return bool
*/
public function is($targetState)
{
return $this->getState() === $targetState;
}
/**
* @param string $transitionName
*
* @return bool
*/
public function can($transitionName)
{
return $this->getStateMachine()->can($transitionName);
}
/**
* @param string $transitionName
*
* @return NULL
*/
public function apply($transitionName)
{
return $this->getStateMachine()->apply($transitionName);
}
}
// Usage:
// In your Stateful Class, add the initStateMachine() method and call it at initialization (__contruct() method):
/*
class MyStatefulClass implements \Finite\StatefulInterface
{
use FiniteTrait;
public function __construct()
{
$this->initStateMachine();
}
protected function initStateMachine()
{
$sm = new \Finite\StateMachine\StateMachine;
$sm->addState(new \Finite\State\State('s1', 'initial'));
$sm->addState('s2');
$sm->addState('s3');
$sm->addState(new \Finite\State\State('s4', 'final'));
$sm->addTransition('t12', 's1', 's2');
$sm->addTransition('t23', 's2', 's3');
$sm->addTransition('t34', 's3', 's4');
$sm->addTransition('t42', 's4', 's2');
$sm->setObject($this);
$sm->initialize();
$this->stateMachine = $sm;
}
}
$myStatefulObject = new MyStatefulClass;
$myStatefulObject->getState(); // → "s1"
$myStatefulObject->can('t23'); // → false
$myStatefulObject->can('t12'); // → true
$myStatefulObject->apply('t12'); // → NULL
$myStatefulObject->is('s2'); // → true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment