/** * Transitions selected object * * @param \Illuminate\Database\Eloquent\Model $object * @param $value * @throws \Finite\Exception\StateException */ public function apply(\Illuminate\Database\Eloquent\Model $object, $value, $properties = []) { $this->setStateMachine($object); $this->stateMachine->apply($value); $object->saveFiniteRelationship($this->stateMachine->getCurrentState()->getName(), $properties); event(new StateSuccessfullyApplied($object)); }
public function test() { $this->callbacksMock->expects($this->once())->method('afterItWasProposed'); $this->callbacksMock->expects($this->exactly(2))->method('afterItWasProposedOrReviewed'); $this->callbacksMock->expects($this->exactly(2))->method('afterItWasAnythingButProposed'); $this->callbacksMock->expects($this->once())->method('onReview'); $this->callbacksMock->expects($this->once())->method('afterItLeavesReviewed'); $this->stateMachine->apply('propose'); $this->stateMachine->apply('review'); $this->stateMachine->apply('publish'); $this->alternativeStateMachine->apply('propose'); $this->alternativeStateMachine->apply('review'); $this->alternativeStateMachine->apply('publish'); }
/** * Apply a transition. * * @param string $transitionName * @param array $parameters * * @return mixed * @throws StateException */ public function apply($transitionName, array $parameters = []) { return $this->stateMachine->apply($transitionName, $parameters); }
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();
/** * @param $name * * @return mixed * @throws \Finite\Exception\StateException */ public function transition($name) { return $this->stateMachine->apply($name); }