function loadMethodsFromXML($xmlParser) { $methods = array(); $xmlMethods = XmlParserUtils::getChildren($xmlParser->document, XML_METHOD); foreach ($xmlMethods as $xmlMethod) { $name = XmlParserUtils::existAttribute($xmlMethod, XML_METHOD_NAME) ? $xmlMethod->tagAttrs[XML_METHOD_NAME] : null; if (empty($name)) { throw new MashapeException(EXCEPTION_METHOD_EMPTY_NAME, EXCEPTION_XML_CODE); } else { if (existMethod($methods, $name)) { throw new MashapeException(sprintf(EXCEPTION_METHOD_DUPLICATE_NAME, $name), EXCEPTION_XML_CODE); } } $http = XmlParserUtils::existAttribute($xmlMethod, XML_METHOD_HTTP) ? $xmlMethod->tagAttrs[XML_METHOD_HTTP] : null; if (empty($http)) { throw new MashapeException(EXCEPTION_METHOD_EMPTY_HTTP, EXCEPTION_XML_CODE); } else { $http = strtolower($http); if ($http != "get" && $http != "post" && $http != "put" && $http != "delete") { throw new MashapeException(sprintf(EXCEPTION_METHOD_INVALID_HTTP, $http), EXCEPTION_XML_CODE); } } $route = XmlParserUtils::existAttribute($xmlMethod, XML_METHOD_ROUTE) ? $xmlMethod->tagAttrs[XML_METHOD_ROUTE] : null; if (!empty($route)) { if (!validateRoute($route)) { throw new MashapeException(sprintf(EXCEPTION_METHOD_INVALID_ROUTE, $route), EXCEPTION_XML_CODE); } else { if (existRoute($methods, $route, $http)) { throw new MashapeException(sprintf(EXCEPTION_METHOD_DUPLICATE_ROUTE, $route), EXCEPTION_XML_CODE); } } } // Get the result $resultsNode = XmlParserUtils::getChildren($xmlMethod, "result"); //$xmlMethod->result; $resultNode = null; if (count($resultsNode) > 1) { throw new MashapeException(sprintf(EXCEPTION_RESULT_MULTIPLE, $name), EXCEPTION_XML_CODE); } elseif (count($resultsNode) == 1) { $resultNode = $resultsNode[0]; } // else { // throw new MashapeException(sprintf(EXCEPTION_RESULT_MISSING, $name), EXCEPTION_XML_CODE); // } $object = null; $array = null; $resultName = null; if ($resultNode != null) { $array = XmlParserUtils::existAttribute($resultNode, XML_RESULT_ARRAY) ? $resultNode->tagAttrs[XML_RESULT_ARRAY] : null; if ($array != null && strtolower($array) == "true") { $array = true; } else { $array = false; } $type = XmlParserUtils::existAttribute($resultNode, XML_RESULT_TYPE) ? $resultNode->tagAttrs[XML_RESULT_TYPE] : null; if (strtolower($type == "simple")) { $resultName = XmlParserUtils::existAttribute($resultNode, XML_RESULT_NAME) ? $resultNode->tagAttrs[XML_RESULT_NAME] : null; if (empty($resultName)) { throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_NAME_SIMPLE, $name), EXCEPTION_XML_CODE); } } else { if (strtolower($type == "complex")) { $object = XmlParserUtils::existAttribute($resultNode, XML_RESULT_NAME) ? $resultNode->tagAttrs[XML_RESULT_NAME] : null; if (empty($object)) { throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_NAME_OBJECT, $name), EXCEPTION_XML_CODE); } } else { if (empty($type)) { throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_TYPE, $name), EXCEPTION_XML_CODE); } else { throw new MashapeException(sprintf(EXCEPTION_RESULT_INVALID_TYPE, $type, $name), EXCEPTION_XML_CODE); } } } } $method = new RESTMethod(); $method->setName($name); $method->setObject($object); $method->setResult($resultName); $method->setArray($array); $method->setHttp($http); $method->setRoute($route); //Save method array_push($methods, $method); } return $methods; }
function testComplex() { RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test.xml"); $method = new RESTMethod(); $method->setName("touchComplex"); $method->setObject("ClassOne"); $this->assertEquals('{"field1":"value1","field2":"value2"}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test2.xml"); $method = new RESTMethod(); $method->setName("touchComplex2"); $method->setObject("ClassOne"); $this->assertEquals('{"field2":"value2"}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test3.xml"); $method = new RESTMethod(); $method->setName("touchComplex3"); $method->setObject("ClassOne"); $this->assertEquals('{"field1":"value1","field2":{"childField1":"child value 1","childField2":"child value 2"}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test3.xml"); $method = new RESTMethod(); $method->setName("touchComplex4"); $method->setObject("ClassOne"); $this->assertEquals('{"field1":"value1","field2":null}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test4.xml"); $method = new RESTMethod(); $method->setName("touchComplex5"); $method->setObject("ClassOne"); $this->assertEquals('{"field1":["value1",3,"value3"]}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test5.xml"); $method = new RESTMethod(); $method->setName("touchComplex6"); $method->setObject("ClassOne"); $this->assertEquals('{"field1":[{"childField1":"child value 1","childField2":"child value 2"},{"childField1":"second child value 1","childField2":"second child value 2"}]}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test6.xml"); $method = new RESTMethod(); $method->setName("touchComplex7"); $method->setObject("ClassThree"); $this->assertEquals('{"field1":"this is field1","field2":["this","is","field",2,true],"field4":[{"field1":"child value 1","field2":"child value 2"},{"field1":"second child value 1","field2":"second child value 2"}]}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test.xml"); $method = new RESTMethod(); $method->setName("touchComplex8"); $method->setObject("ClassOne"); $method->setArray(true); $this->assertEquals('[{"field1":"value1","field2":"value2"},{"field1":"second value1","field2":"second value2"},{"field1":"third value1","field2":"third value2"}]', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); $method = new RESTMethod(); $method->setName("touchComplex9"); $method->setArray(true); $method->setResult("val"); $this->assertEquals('{"val":["ciao","marco",false]}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); $method = new RESTMethod(); $method->setName("touchComplex10"); $method->setArray(true); $method->setResult("val"); $this->assertEquals('{"val":{"key1":"value1","key2":"value2","key3":{"nested1":"nv1","nested2":"nv2"}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); $method = new RESTMethod(); $method->setName("touchComplex11"); $method->setArray(true); $method->setResult("val"); $this->assertEquals('{"val":{"key1":"value1","key2":"value2","key3":{"nested1":"nv1","nested2":{"yo1":"vyo1","yo2":[1,2,3]}}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY)); }