Skip to content

Commit

Permalink
gets fields generated at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Mashape committed Feb 2, 2011
1 parent 1c93aba commit 6ab00a8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions mashape/exceptions/exceptionMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

// http://api.mashape.com Error codes

define("EXCEPTION_FIELD_NOTFOUND", "Can't find the property \"%s\"");
define("EXCEPTION_INSTANCE_NULL", "Please verify the class you're initializing with 'MashapeHandler::handleApi(..)' exists");
define("EXCEPTION_EMPTY_REQUEST", "A request attempt was made to Mashape, but the response was empty. The firewall may be blocking outbound HTTP requests");
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON from Mashape. The json_decode function is missing on server");
Expand Down
7 changes: 5 additions & 2 deletions mashape/mashape.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ protected function __construct($dirPath) {
$this->dirPath = $dirPath;
}

protected static function addError($code, $message) {
protected static function addError($code, $message, $statusCode = null) {
if (empty(self::$errors)) {
self::$errors = array();
}
$e = new MashapeAPIError($code, $message);
array_push(self::$errors, $e);
if (!empty($statusCode)) {
self::setHTTPStatusCode($statusCode);
}
}

public static function setHttpStatusCode($statusCode) {
public static function setHTTPStatusCode($statusCode) {
if (!empty($statusCode)) {
header("HTTP/1.0 " . $statusCode);
}
Expand Down
12 changes: 0 additions & 12 deletions mashape/methods/call/helpers/serializeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,12 @@ function serializeArray($result, $instance, $isSimpleResult, $serverKey) {
if (ArrayUtils::isAssociative($result)) {
$json .= "{";
foreach ($result as $key => $value) {
echo "Associative key \"" . $key . "\"\n";
$json .= '"' . $key . '":';
if (is_object($value)) {
$json .= serializeObject($value, $instance, false, $serverKey);
} else {
if (is_array($value)) {
// if (ArrayUtils::isAssociative($result)) {
//
// } else {
// $json .= "[";
// }
$json .= serializeArray($value, $instance, $isSimpleResult, $serverKey);
// if (ArrayUtils::isAssociative($result)) {
//
// } else {
// $json .= "]";
// }
// $json .= serializeArray($value, $instance, $isSimpleResult, $serverKey);
} else {
$json .= serializeObject($value, $instance, !is_object($value), $serverKey);
}
Expand Down
19 changes: 13 additions & 6 deletions mashape/methods/call/helpers/serializeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function serializeObject($result, $instance, $isSimpleResult, $serverKey) {
} else {
// It's a custom object, let's serialize recursively every field
$className = get_class($result);

$reflectedClass = new ReflectionClass($className);

$xmlObject = RESTConfigurationLoader::getObject($className, $serverKey);
Expand All @@ -59,12 +59,19 @@ function serializeObject($result, $instance, $isSimpleResult, $serverKey) {
$fieldMethod = $field->getMethod();
$fieldValue = null;
if (empty($fieldMethod)) {
$reflectedProperty = $reflectedClass->getProperty($fieldName);
if ($reflectedProperty->isPublic()) {
$fieldValue = $reflectedClass->getProperty($fieldName)->getValue($result);
if ($reflectedClass->hasProperty($fieldName)) {
$reflectedProperty = $reflectedClass->getProperty($fieldName);
if ($reflectedProperty->isPublic()) {
$fieldValue = $reflectedProperty->getValue($result);
} else {
// Try using the __get magic method
$fieldValue = $reflectedClass->getMethod("__get")->invokeArgs($result, array($fieldName));
}
} else if (ArrayUtils::existKey($fieldName, get_object_vars($result))) {
$fieldValue = $result->$fieldName;

} else {
// Try using the __get magic method
$fieldValue = $reflectedClass->getMethod("__get")->invokeArgs($result, array($fieldName));
throw new MashapeException(sprintf(EXCEPTION_FIELD_NOTFOUND, $fieldName), EXCEPTION_GENERIC_LIBRARY_ERROR_CODE);
}
} else {
$fieldValue = $reflectedClass->getMethod($fieldMethod)->invoke($result);
Expand Down

0 comments on commit 6ab00a8

Please sign in to comment.