/** * Adds the given state to the state machine setup. * * @param StateInterface $state * * @return StateMachineBuilderInterface */ public function addState(StateInterface $state) { $state_name = $state->getName(); if (isset($this->states[$state_name])) { throw new VerificationError(sprintf('A state with the name "%s" already has been added.' . ' State names must be unique within each StateMachine.', $state_name)); } $this->states[$state_name] = $state; return $this; }
/** * Employs an active-state specific verification. * * @param StateInterface $state * @param int $transition_count Number of transitions attached to the given state. * * @throws VerificationError */ protected function verifyActiveState(StateInterface $state, $transition_count) { if ($transition_count === 0) { throw new VerificationError(sprintf('State "%s" is expected to have at least one transition.' . ' Only "%s" states are permitted to have no transitions.', $state->getName(), StateInterface::TYPE_FINAL)); } }
/** * Determine the correct transition to take while leaving the current state. * * @param StatefulSubjectInterface $subject * @param StateInterface $current_state * @param string $event_name * * @throws Error In cases where either more than one or no transition at all have accpeted the subject. * * @return TransitionInterface */ protected function getActivatedTransition(StatefulSubjectInterface $subject, StateInterface $state, $event_name) { $accepted_transition = null; $possible_transitions = $this->getTransitions($state->getName(), $event_name); foreach ($possible_transitions as $state_transition) { if (!$this->mayProceed($subject, $state_transition)) { continue; } if ($accepted_transition) { throw new Error(sprintf('Only one transition is allowed to be active at a time.', $event_name, $state->getName())); } $accepted_transition = $state_transition; } return $accepted_transition; }
/** * Creates a specific dot-graph node that represents the given state. * * @param StateMachineInterface $state_machine * @param StateInterface $state * * @return string */ protected function createStateNode(StateMachineInterface $state_machine, StateInterface $state) { $state_name = $state->getName(); $attributes = [sprintf('label="%s"', $state_name), sprintf('fontname="%s"', $this->getStyle('state_node.fontname', self::STATE_NODE_FONTNAME)), sprintf('fontsize="%s"', $this->getStyle('state_node.fontsize', self::STATE_NODE_FONTSIZE)), sprintf('fontcolor="%s"', $this->getStyle('state_node.fontcolor', self::STATE_NODE_FONTCOLOR)), sprintf('color="%s"', $this->getStyle('state_node.color', self::STATE_NODE_COLOR))]; if ($state->isFinal()) { $attributes[] = 'style="bold"'; } if (!$state_machine->isEventState($state_name) && !$state->isFinal()) { $attributes[] = 'shape="parallelogram"'; } return sprintf('%s [%s]', $this->node_id_map[$state_name], implode(' ', $attributes)); }
/** * Sets the current state name. * Is called when the state machine enters a new state * * @param StateInterface $state */ public function onStateEntry(StateInterface $state) { $this->current_state_name = $state->getName(); }