protected function initialize() { $this->addStates(); $this->addTransitions(); $this->object->setObject($this->getStatefulObjectMock()); $this->object->initialize(); }
/** * @param NULL|mixed $state scalar */ public function __construct($state = NULL) { $this->stateMachine = new StateMachine($this); $this->getLoader()->load($this->stateMachine); // intentionally set after ArrayLoader::load, as it sets symfony accessor $this->stateMachine->setStateAccessor(new StatefulPropertyStateAccessor()); $this->stateMachine->initialize(); $this->setFiniteState($state ?: $this->getInitialState()); $this->onModify(); }
/** * Configures $stateMachine according to selected object * * @param \Illuminate\Database\Eloquent\Model $object * @throws \Finite\Exception\ObjectException */ private function setStateMachine(\Illuminate\Database\Eloquent\Model $object) { $config = array_merge(['class' => get_class($object)], $object->getFiniteConfig()); if (!$object->finiteStates->isEmpty()) { $object->setFiniteState($object->latestFiniteState); } $loader = new ArrayLoader($config); $loader->load($this->stateMachine); $this->stateMachine->setObject($object); $this->stateMachine->initialize(); }
protected function setUp() { $this->object = new \stdClass(); $this->object->finiteState = null; $this->alternativeObject = new \stdClass(); $this->alternativeObject->finiteState = null; $this->stateMachine = new StateMachine($this->object); $this->alternativeStateMachine = new StateMachine($this->alternativeObject, $this->stateMachine->getDispatcher()); $this->callbacksMock = $this->getMockBuilder('\\stdClass')->setMethods(array('afterItWasProposed', 'afterItWasProposedOrReviewed', 'afterItWasAnythingButProposed', 'onReview', 'afterItLeavesReviewed'))->getMock(); $states = array('draft' => array('type' => 'initial'), 'proposed' => array(), 'reviewed' => array(), 'published' => array('type' => 'final'), 'declined' => array('type' => 'final')); $transitions = array('propose' => array('from' => 'draft', 'to' => 'proposed'), 'return_to_draft' => array('from' => 'proposed', 'to' => 'draft'), 'review' => array('from' => 'proposed', 'to' => 'reviewed'), 'publish' => array('from' => 'reviewed', 'to' => 'published'), 'decline' => array('from' => array('proposed', 'reviewed'), 'to' => 'declined')); $loader = new ArrayLoader(array('states' => $states, 'transitions' => $transitions, 'callbacks' => array('after' => array(array('to' => 'proposed', 'do' => array($this->callbacksMock, 'afterItWasProposed')), array('to' => array('proposed', 'reviewed'), 'do' => array($this->callbacksMock, 'afterItWasProposedOrReviewed')), array('to' => array('-proposed'), 'do' => array($this->callbacksMock, 'afterItWasAnythingButProposed')), array('on' => 'review', 'do' => array($this->callbacksMock, 'onReview')), array('from' => 'reviewed', 'do' => array($this->callbacksMock, 'afterItLeavesReviewed')))))); $loader->load($this->stateMachine); $this->stateMachine->initialize(); $alternativeLoader = new ArrayLoader(array('states' => $states, 'transitions' => $transitions)); $alternativeLoader->load($this->alternativeStateMachine); $this->alternativeStateMachine->initialize(); }
use Finite\State\State; use Finite\State\StateInterface; use Finite\StateMachine\StateMachine; // Composer Autoloader require_once '../vendor/autoload.php'; // Create a new State Machine $stateMachine = new StateMachine(); // define some States $stateMachine->addState(new State('state1', StateInterface::TYPE_INITIAL)); $stateMachine->addState(new State('state2')); $stateMachine->addState(new State('state3')); $stateMachine->addState(new State('state4', StateInterface::TYPE_FINAL)); $thing = new Thing(); // add some transitions $stateMachine->addTransition('from_1_to_2', 'state1', 'state2'); $stateMachine->addTransition('from_2_to_3', 'state2', 'state3'); $stateMachine->addTransition('from_3_to_4', 'state3', 'state4'); $stateMachine->addTransition('from_4_to_2', 'state4', 'state2'); // add stateful object into state machine $stateMachine->setObject($thing); $stateMachine->initialize(); // current state echo $stateMachine->getCurrentState(); // can transition to a valid state var_dump($stateMachine->can('from_1_to_2')); // can transition to a invalid state var_dump($stateMachine->can('from_2_to_3')); // switch state $stateMachine->apply('from_1_to_2'); var_dump($stateMachine->can('from_2_to_3')); echo $stateMachine->getCurrentState();
public function applyContext(Context $context) { $this->context = $context; $this->stateMachine->setObject($context); $this->stateMachine->initialize(); }