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

load() public method

{@inheritDoc}
public load ( StateMachine $stateMachine )
$stateMachine izzum\statemachine\StateMachine
    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 fuck 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);
    }

Usage Example

 /**
  * @test
  */
 public function shouldLoadTransitionsFromYAMLString()
 {
     $machine = new StateMachine(new Context(new Identifier('yaml-test', 'yaml-machine')));
     $this->assertCount(0, $machine->getTransitions());
     $yaml = $this->getYAML();
     $loader = new YAML($yaml);
     $this->assertEquals($this->getYAML(), $loader->getYAML());
     $count = $loader->load($machine);
     $this->assertCount(2, $machine->getTransitions());
     $this->assertEquals(2, $count);
     $tbd = $machine->getTransition('b_to_done');
     $b = $tbd->getStateFrom();
     $d = $tbd->getStateTo();
     $tab = $machine->getTransition('a_to_b');
     $a = $tab->getStateFrom();
     $this->assertEquals($b, $tab->getStateTo());
     $this->assertSame($b, $tab->getStateTo());
     $this->assertTrue($a->isInitial());
     $this->assertTrue($b->isNormal());
     $this->assertTrue($d->isFinal());
 }