public function load(StateMachine $stateMachine)
{
//decode the json in a php object structure
$decoded = json_decode($this->getJSON(), false);
if (!$decoded) {
//could not decode (make sure that fully qualified names are escaped with
//2 backslashes: \\izzum\\commands\\NullCommand and that only double quotes are used.
throw new Exception(sprintf('could not decode json data. did you only use double quotes? check the json format against %s', 'http://jsonlint.com/'), Exception::BAD_LOADERDATA);
}
$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 json. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA);
}
//accessing json as an object 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 json 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);
}