izzum\statemachine\State::addTransition PHP Method

addTransition() public method

TRICKY: this method should be package visibility only, so don't use directly. it is used to set the bidirectional association for State and Transition from a Transition instance on the state the transition will be allowed to run from ('state from').
public addTransition ( Transition $transition ) : boolan
$transition Transition
return boolan yes in case the transition was not on the State already or in case of an invalid transition
    public function addTransition(Transition $transition)
    {
        $output = false;
        // check all existing transitions.
        if (!$this->hasTransition($transition->getName()) && $transition->getStateFrom()->getName() == $this->getName() && !$this->isFinal() && !$this->isRegex()) {
            $output = true;
            $this->transitions[] = $transition;
        }
        return $output;
    }

Usage Example

 /**
  *
  * @param State $state_from
  * @param State $state_to
  * @param string $event
  *            optional: an event name by which this transition can be
  *            triggered
  * @param string $rule
  *            optional: one or more fully qualified Rule (sub)class name(s)
  *            to check to see if we are allowed to transition.
  *            This can actually be a ',' seperated string of multiple rules
  *            that will be applied as a chained 'and' rule.
  * @param string $command
  *            optional: one or more fully qualified Command (sub)class
  *            name(s) to execute for a transition.
  *            This can actually be a ',' seperated string of multiple
  *            commands that will be executed as a composite.
  * @param callable $callable_guard
  *            optional: a php callable to call. eg: "function(){echo 'closure called';};"
  * @param callable $callable_transition
  *            optional: a php callable to call. eg: "izzum\MyClass::myStaticMethod"
  */
 public function __construct(State $state_from, State $state_to, $event = null, $rule = self::RULE_EMPTY, $command = self::COMMAND_EMPTY, $callable_guard = self::CALLABLE_NULL, $callable_transition = self::CALLABLE_NULL)
 {
     $this->state_from = $state_from;
     $this->state_to = $state_to;
     $this->setRuleName($rule);
     $this->setCommandName($command);
     $this->setGuardCallable($callable_guard);
     $this->setTransitionCallable($callable_transition);
     // setup bidirectional relationship with state this transition
     // originates from. only if it's not a regex or final state type
     if (!$state_from->isRegex() && !$state_from->isFinal()) {
         $state_from->addTransition($this);
     }
     // set and sanitize event name
     $this->setEvent($event);
 }