Merge pull request #159 from yanoosh/php-fix-sanitize

PHP - Improved/fixed serialization and deserialize.
This commit is contained in:
Tony Tam 2014-05-01 20:31:13 -07:00
commit b4f3143c8c

View File

@ -44,7 +44,7 @@ class APIClient {
* @param array $queryParams parameters to be place in query URL * @param array $queryParams parameters to be place in query URL
* @param array $postData parameters to be placed in POST body * @param array $postData parameters to be placed in POST body
* @param array $headerParams parameters to be place in request header * @param array $headerParams parameters to be place in request header
* @return unknown * @return mixed
*/ */
public function callAPI($resourcePath, $method, $queryParams, $postData, public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) { $headerParams) {
@ -66,7 +66,7 @@ class APIClient {
} }
if (is_object($postData) or is_array($postData)) { if (is_object($postData) or is_array($postData)) {
$postData = json_encode(self::sanitizeForSerialization($postData)); $postData = json_encode($this->sanitizeForSerialization($postData));
} }
$url = $this->apiServer . $resourcePath; $url = $this->apiServer . $resourcePath;
@ -117,21 +117,35 @@ class APIClient {
$response_info['http_code']); $response_info['http_code']);
} }
return $data; return $data;
} }
/** /**
* Build a JSON POST object * Build a JSON POST object
*/ */
public static function sanitizeForSerialization($postData) { protected function sanitizeForSerialization($data)
foreach ($postData as $key => $value) { {
if (is_a($value, "DateTime")) { if (is_scalar($data) || null === $data) {
$postData->{$key} = $value->format(DateTime::ISO8601); $sanitized = $data;
} } else if ($data instanceof \DateTime) {
} $sanitized = $data->format(\DateTime::ISO8601);
return $postData; } else if (is_array($data)) {
} foreach ($data as $property => $value) {
$data[$property] = $this->sanitizeForSerialization($value);
}
$sanitized = $data;
} else if (is_object($data)) {
$values = array();
foreach (array_keys($data::$swaggerTypes) as $property) {
$values[$property] = $this->sanitizeForSerialization($data->$property);
}
$sanitized = $values;
} else {
$sanitized = (string)$data;
}
return $sanitized;
}
/** /**
* Take value and turn it into a string suitable for inclusion in * Take value and turn it into a string suitable for inclusion in
@ -169,67 +183,42 @@ class APIClient {
return $value; return $value;
} }
/** /**
* Deserialize a JSON string into an object * Deserialize a JSON string into an object
* *
* @param object $object object or primitive to be deserialized * @param object $object object or primitive to be deserialized
* @param string $class class name is passed as a string * @param string $class class name is passed as a string
* @return object an instance of $class * @return object an instance of $class
*/ */
public static function deserialize($object, $class) {
if (gettype($object) == "NULL") { public static function deserialize($data, $class)
return $object; {
} if (null === $data) {
$deserialized = null;
if (substr($class, 0, 6) == 'array[') { } else if (substr($class, 0, 6) == 'array[') {
$sub_class = substr($class, 6, -1); $subClass = substr($class, 6, -1);
$sub_objects = array(); $values = array();
foreach ($object as $sub_object) { foreach ($data as $value) {
$sub_objects[] = self::deserialize($sub_object, $values[] = self::deserialize($value, $subClass);
$sub_class);
} }
return $sub_objects; $deserialized = $values;
} elseif ($class == 'DateTime') { } elseif ($class == 'DateTime') {
return new DateTime($object); $deserialized = new \DateTime($data);
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) { } elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
settype($object, $class); settype($data, $class);
return $object; $deserialized = $data;
} else { } else {
$instance = new $class(); // this instantiates class named $class $instance = new $class();
$classVars = get_class_vars($class); foreach ($instance::$swaggerTypes as $property => $type) {
} if (isset($data->$property)) {
$instance->$property = self::deserialize($data->$property, $type);
}
}
$deserialized = $instance;
}
foreach ($object as $property => $value) { return $deserialized;
}
// Need to handle possible pluralization differences
$true_property = $property;
if (! property_exists($class, $true_property)) {
if (substr($property, -1) == 's') {
$true_property = substr($property, 0, -1);
}
}
if (array_key_exists($true_property, $classVars['swaggerTypes'])) {
$type = $classVars['swaggerTypes'][$true_property];
} else {
$type = 'string';
}
if (in_array($type, array('string', 'int', 'float', 'bool'))) {
settype($value, $type);
$instance->{$true_property} = $value;
} elseif (preg_match("/array<(.*)>/", $type, $matches)) {
$sub_class = $matches[1];
$instance->{$true_property} = array();
foreach ($value as $sub_property => $sub_value) {
$instance->{$true_property}[] = self::deserialize($sub_value,
$sub_class);
}
} else {
$instance->{$true_property} = self::deserialize($value, $type);
}
}
return $instance;
}
} }