/** * Converts the JSON file to an array in php format * @param $obj deserialized JSON object from JavaScript * @return array of states in a php array */ function jsonToStateArray($nfaStates) { $array = array(); //loop through all the states foreach ($nfaStates as $state) { //initialize a new state with the same name as the current state in the array $newState = new NFAState($state->name); //loop through the adjacency list and use the key value pair as the transition for the state foreach ($state->adjacencyList as $transition => $toStates) { $newState->setTransition($transition, $toStates); } //add the new state to the array $array[$state->name] = $newState; } return $array; }
public function testJsonToStateArray() { require "app/json.php"; //initialize input $json = '{"stateNames":["A","B","C"],"transitions":["a","b"],"states":[{"name":"A","adjacencyList":{"a":["B","C"],"b":[]}},{"name":"B","adjacencyList":{"a":["A"],"b":["B"]}},{"name":"C","adjacencyList":{"a":[],"b":["A","B"]}}]}'; //decode JSON and extract the necessary data $obj = json_decode($json); $result = jsonToStateArray($obj->states); //initialize states to compare with the output of the function $aState = new NFAState("A"); $aState->setTransition("a", array("B", "C")); $aState->setTransition("b", []); $bState = new NFAState("B"); $bState->setTransition("a", array("A")); $bState->setTransition("b", array("B")); $cState = new NFAState("C"); $cState->setTransition("a", []); $cState->setTransition("b", array("A", "B")); // can't do assert contains on an array of objects apparently $this->assertEquals($aState, $result["A"]); $this->assertEquals($bState, $result["B"]); $this->assertEquals($cState, $result["C"]); }