/** * @param \Khaos\FSM\State\State $initialState * @param \Khaos\FSM\Stateful $context * @param \Khaos\FSM\Transition\Transition $t1 * @param \Khaos\FSM\Transition\Transition $t2 * @param \Khaos\FSM\State\State $s1 * @param \Khaos\FSM\State\State $s2 */ function let($initialState, $context, $t1, $t2, $s1, $s2) { $context->setCurrentState($initialState)->willReturn(); $context->getCurrentState()->willReturn($initialState); $this->beConstructedWith($initialState, $context); $t1->can('t1', $context, $this->getWrappedObject())->willReturn(true); $t1->can(Argument::any(), $context, $this->getWrappedObject())->willReturn(false); $t1->apply('t1', $context, $this->getWrappedObject())->willReturn('S1'); $t1->getTo()->willReturn($s1); $t2->can('t2', $context, $this->getWrappedObject())->willReturn(true); $t2->can(Argument::any(), $context, $this->getWrappedObject())->willReturn(false); $t2->apply('t2', $context, $this->getWrappedObject())->willReturn('S2'); $t2->getTo()->willReturn($s2); $initialState->getTransitions()->willReturn([$t1, $t2]); }
/** * Can Advance * * Determine if the machine can be advanced, if so set transition * to the path we can take. * * Usage :- * * can(Input) * can(Input, Transition) * * @param mixed $input Input against which transition(s) will be tested * @param Transition $transition Placeholder for found transition or the only transition to be tested * * @return bool * @throws Exception */ public function can($input, &$transition = null) { $state = $this->context->getCurrentState(); if (!$transition instanceof Transition) { foreach ($state->getTransitions() as $candidate) { if (!$candidate->can($input, $this->context, $this)) { continue; } $transition = $candidate; return true; } return false; } return $transition->can($input, $this->context, $this); }
/** * Apply Transition * * @param mixed $input * @param Stateful $context * @param Runner $runner * * @return mixed */ public function apply($input, Stateful $context, Runner $runner) { $context->setCurrentState($this->to); if ($this->action !== null && is_callable($this->action)) { return call_user_func($this->action, $input, $context, $runner, $this); } if ($input instanceof InputSequence) { $input->pop(); } return $this->action ?: (string) $this->to; }
/** * @inheritDoc * * @var InputSequence $input * @var UsageParserContext $context * @var BacktrackingRunner $runner */ public function apply($input, Stateful $context, Runner $runner) { $context->setCurrentState($this->to); return new Argument($this->argument, $input->pop()); }
/** * @inheritDoc * * @var InputSequence $input * @var UsageParserContext $context * @var BacktrackingRunner $runner */ public function apply($input, Stateful $context, Runner $runner) { $context->setCurrentState($this->to); return null; }
/** * @inheritDoc * * @var InputSequence $input * @var UsageParserContext $context * @var BacktrackingRunner $runner */ public function apply($input, Stateful $context, Runner $runner) { $context->setCurrentState($this->to); return $input->getOption($this->optionDefinition); }
/** * Get Current State * * @return State */ public function getCurrentState() { return $this->context->getCurrentState(); }