izzum\statemachine\loader\LoaderArray::load PHP Method

load() public method

{@inheritDoc}
public load ( StateMachine $stateMachine )
$stateMachine izzum\statemachine\StateMachine
    public function load(StateMachine $stateMachine)
    {
        $count = 0;
        $unsorted = $this->getTransitions();
        $has_regex = array();
        $has_no_regex = array();
        foreach ($unsorted as $transition) {
            $to = $transition->getStateTo();
            $from = $transition->getStateFrom();
            //sort on regexes. they should come last in an automated loader like this
            if ($from->isRegex() || $to->isRegex()) {
                $has_regex[] = $transition;
            } else {
                $has_no_regex[] = $transition;
            }
        }
        $sorted = array_merge($has_no_regex, $has_regex);
        // add the sorted transitions. the transitions added will set the
        // states (from/to) on the statemachine
        foreach ($sorted as $transition) {
            // when using transitions with 'regex' states, the statemachine will handle this for you.
            $count += $stateMachine->addTransition($transition);
        }
        return $count;
    }

Usage Example

Example #1
0
 /**
  * {@inheritDoc}
  */
 public function load(StateMachine $stateMachine)
 {
     //decode the json in a php object structure
     $decoded = yaml_parse($this->getYaml(), false);
     //yaml decoding returns a php array.
     $name = $stateMachine->getContext()->getMachine();
     $found = false;
     $data = null;
     if (is_array(@$decoded['machines'])) {
         foreach ($decoded['machines'] as $data) {
             if ($data['name'] === $name) {
                 $found = true;
                 break;
             }
         }
     }
     if (!$found) {
         //no name match found
         throw new Exception(sprintf('no machine data found for "%s" in yaml. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA);
     }
     //accessing an array with an @ error suppresion operator ('shut the f**k up' operator),
     //allows you to get properties, even if they do not exist, without notices.
     //this lets us be a little lazy in mapping the array properties to the state and transition properties
     $states = array();
     foreach ($data['states'] as $state) {
         $tmp = new State($state['name'], $state['type'], @$state['entry_command'], @$state['exit_command'], @$state['entry_callable'], @$state['exit_callable']);
         $tmp->setDescription(@$state['description']);
         $states[$tmp->getName()] = $tmp;
     }
     $transitions = array();
     foreach ($data['transitions'] as $transition) {
         $tmp = new Transition($states[$transition['state_from']], $states[$transition['state_to']], @$transition['event'], @$transition['rule'], @$transition['command'], @$transition['guard_callable'], @$transition['transition_callable']);
         $tmp->setDescription(@$transition['description']);
         $transitions[] = $tmp;
     }
     //delegate to loader
     $loader = new LoaderArray($transitions);
     return $loader->load($stateMachine);
 }
All Usage Examples Of izzum\statemachine\loader\LoaderArray::load