Skip to content
Browse files

serialize associative arrays

  • Loading branch information...
1 parent 5739f94 commit 2fd63cc16c13a3679f70e90d134738ef7b22bd7d Mashape committed Feb 2, 2011
View
2 mashape/exceptions/exceptionMessages.php
@@ -25,7 +25,9 @@
*/
define("EXCEPTION_GENERIC_LIBRARY_ERROR_CODE", 1000);
+define("EXCEPTION_EXPECTED_ARRAY_RESULT_SIMPLE", "The result value it's not an array although it's described it would have been an array, please check your XML file");
define("EXCEPTION_EXPECTED_ARRAY_RESULT", "The result value of field \"%s\" in object \"%s\" it's not an array although it's described it would have been an array, please check your XML file");
+define("EXCEPTION_UNEXPECTED_ARRAY_RESULT_SIMPLE", "The result value it's an array although it was described it wouldn't have been an array, please check your XML file");
define("EXCEPTION_UNEXPECTED_ARRAY_RESULT", "The result value of field \"%s\" in object \"%s\" it's an array although it was described it wouldn't have been an array, please check your XML file");
define("EXCEPTION_UNKNOWN_OBJECT", "The result can't be serialized because it's of an unknown type \"%s\" not described in the XML file");
View
9 mashape/methods/call/call.php
@@ -30,6 +30,7 @@
require_once(dirname(__FILE__) . "/../../configuration/restConfigurationLoader.php");
require_once(dirname(__FILE__) . "/helpers/callHelper.php");
require_once(dirname(__FILE__) . "/helpers/routeHelper.php");
+require_once(dirname(__FILE__) . "/../discover/helpers/updateHtaccess.php");
define("METHOD", "_method");
define("TOKEN", "_token");
@@ -47,7 +48,7 @@ class Call implements IMethodHandler {
public function handle($instance, $serverKey, $parameters, $httpRequestMethod) {
// If the request comes from local, reload the configuration
- $this->reloadConfiguration($serverKey);
+ $this->reloadConfiguration($instance, $serverKey);
$methodName = null;
$method = null;
@@ -94,8 +95,12 @@ private function findMethod(&$parameters, &$methodName, &$method, $serverKey) {
}
}
- private function reloadConfiguration($serverKey) {
+ private function reloadConfiguration($instance, $serverKey) {
if (HttpUtils::isLocal()) {
+ // Update the .htaccess file with the new route settings
+ updateHtaccess($instance);
+
+ // Update the configuration
RESTConfigurationLoader::reloadConfiguration($serverKey);
}
}
View
26 mashape/methods/call/helpers/serializeArray.php
@@ -33,27 +33,45 @@ function serializeArray($result, $instance, $isSimpleResult, $serverKey) {
$json = "";
if (is_array($result)) {
if (ArrayUtils::isAssociative($result)) {
+ $json .= "{";
foreach ($result as $key => $value) {
- $json .= '{"' . $key . '":';
+ echo "Associative key \"" . $key . "\"\n";
+ $json .= '"' . $key . '":';
if (is_object($value)) {
$json .= serializeObject($value, $instance, false, $serverKey);
} else {
if (is_array($value)) {
- $json .= "[";
+// if (ArrayUtils::isAssociative($result)) {
+//
+// } else {
+// $json .= "[";
+// }
$json .= serializeArray($value, $instance, $isSimpleResult, $serverKey);
- $json .= "]";
+// if (ArrayUtils::isAssociative($result)) {
+//
+// } else {
+// $json .= "]";
+// }
+// $json .= serializeArray($value, $instance, $isSimpleResult, $serverKey);
} else {
$json .= serializeObject($value, $instance, !is_object($value), $serverKey);
}
}
- $json .= "},";
+ $json .= ",";
}
} else {
+ $json .= "[";
for ($i=0;$i<count($result);$i++) {
$json .= serializeObject($result[$i], $instance, $isSimpleResult, $serverKey) . ",";
}
}
$json = JsonUtils::removeLastChar($result, $json);
+ if (ArrayUtils::isAssociative($result)) {
+ $json .= "}";
+ } else {
+ $json .= "]";
+ }
+
}
return $json;
}
View
6 mashape/methods/call/helpers/serializeMethodResult.php
@@ -47,18 +47,16 @@ function serializeMethodResult($method, $result, $instance, $serverKey) {
$json .= '{"' . $method->getResult() . '":';
}
if ($method->isArray()) {
- $json .= "[";
if (is_array($result)) {
$json .= serializeArray($result, $instance, $isSimpleResult, $serverKey);
} else {
// The result it's not an array although it was described IT WAS an array
- throw new MashapeException(EXCEPTION_EXPECTED_ARRAY_RESULT, EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
+ throw new MashapeException(EXCEPTION_EXPECTED_ARRAY_RESULT_SIMPLE, EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
}
- $json .= "]";
} else {
if (is_array($result)) {
// The result it's an array although it was described IT WAS NOT an array
- throw new MashapeException(EXCEPTION_UNEXPECTED_ARRAY_RESULT, EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
+ throw new MashapeException(EXCEPTION_UNEXPECTED_ARRAY_RESULT_SIMPLE, EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
} else {
$json .= serializeObject($result, $instance, $isSimpleResult, $serverKey);
}
View
2 mashape/methods/call/helpers/serializeObject.php
@@ -83,14 +83,12 @@ function serializeObject($result, $instance, $isSimpleResult, $serverKey) {
$isSimpleField = isSimpleField($field);
if ($field->isArray()) {
- $json .= "[";
if (is_array($fieldValue)) {
$json .= serializeArray($fieldValue, $instance, isSimpleField($field), $serverKey);
} else {
// The result it's not an array although it was described IT WAS an array
throw new MashapeException(sprintf(EXCEPTION_EXPECTED_ARRAY_RESULT, $fieldName, $className), EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
}
- $json .= "]";
} else {
if (is_array($fieldValue)) {
// The result it's an array although it was described IT WAS NOT an array
View
30 tests/methods/call/helpers/callHelperTest.php
@@ -108,6 +108,24 @@ function testComplex() {
$method->setObject("ClassOne");
$method->setArray(true);
$this->assertEquals('{"errors":[],"result":[{"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('{"errors":[],"result":{"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('{"errors":[],"result":{"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('{"errors":[],"result":{"val":{"key1":"value1","key2":"value2","key3":{"nested1":"nv1","nested2":{"yo1":"vyo1","yo2":[1,2,3]}}}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
}
function testError() {
@@ -270,6 +288,18 @@ public function touchComplex8() {
return $result;
}
+ public function touchComplex9() {
+ return array("ciao", "marco", false);
+ }
+
+ public function touchComplex10() {
+ return array("key1"=>"value1", "key2"=>"value2", "key3"=>array("nested1"=>"nv1", "nested2"=>"nv2"));
+ }
+
+ public function touchComplex11() {
+ return array("key1"=>"value1", "key2"=>"value2", "key3"=>array("nested1"=>"nv1", "nested2"=>array("yo1"=>"vyo1", "yo2"=>array(1,2,3))));
+ }
+
public function touchError() {
parent::addError(1, "custom message");
return null;
View
40 tests/utils/arrayUtilsTest.php
@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * Mashape PHP library.
+ *
+ * Copyright (C) 2011 Mashape, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * The author of this software is Mashape, Inc.
+ * For any question or feedback please contact us at: support@mashape.com
+ *
+ */
+
+require_once(dirname(__FILE__) . "/../../mashape/utils/arrayUtils.php");
+
+class ArrayUtilsTest extends PHPUnit_Framework_TestCase
+{
+ function testIsAssociative() {
+ $this->assertFalse(ArrayUtils::isAssociative(null));
+ $this->assertFalse(ArrayUtils::isAssociative(array()));
+ $this->assertFalse(ArrayUtils::isAssociative(array(1,2,3)));
+ $this->assertFalse(ArrayUtils::isAssociative(array("value",2,false)));
+ $this->assertTrue(ArrayUtils::isAssociative(array("key1"=>"value1", "key2"=>"value2")));
+ }
+}
+
+?>

0 comments on commit 2fd63cc

Please sign in to comment.
Something went wrong with that request. Please try again.