change serializer to be instanced, rather than static methods

This commit is contained in:
nmonterroso 2015-06-22 21:33:44 -07:00
parent 2fced0f634
commit c91a1b963b
3 changed files with 36 additions and 26 deletions

View File

@ -25,11 +25,12 @@ class ApiClient {
public static $PUT = "PUT";
public static $DELETE = "DELETE";
/**
* @var Configuration
*/
/** @var Configuration */
protected $config;
/** @var ObjectSerializer */
protected $serializer;
/**
* @param Configuration $config config for this ApiClient
*/
@ -39,6 +40,7 @@ class ApiClient {
}
$this->config = $config;
$this->serializer = new ObjectSerializer();
}
/**
@ -49,6 +51,14 @@ class ApiClient {
return $this->config;
}
/**
* get the serializer
* @return ObjectSerializer
*/
public function getSerializer() {
return $this->serializer;
}
/**
* Get API key (with prefix if set)
* @param string $apiKey name of apikey
@ -96,7 +106,7 @@ class ApiClient {
$postData = http_build_query($postData);
}
else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
$postData = json_encode(ObjectSerializer::sanitizeForSerialization($postData));
$postData = json_encode($this->serializer->sanitizeForSerialization($postData));
}
$url = $this->config->getHost() . $resourcePath;

View File

@ -8,14 +8,14 @@ class ObjectSerializer {
* @param mixed $data the data to serialize
* @return string serialized form of $data
*/
public static function sanitizeForSerialization($data) {
public function sanitizeForSerialization($data) {
if (is_scalar($data) || null === $data) {
$sanitized = $data;
} else if ($data instanceof \DateTime) {
$sanitized = $data->format(\DateTime::ISO8601);
} else if (is_array($data)) {
foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value);
$data[$property] = $this->sanitizeForSerialization($value);
}
$sanitized = $data;
} else if (is_object($data)) {
@ -23,7 +23,7 @@ class ObjectSerializer {
foreach (array_keys($data::$swaggerTypes) as $property) {
$getter = $data::$getters[$property];
if ($data->$getter() !== null) {
$values[$data::$attributeMap[$property]] = self::sanitizeForSerialization($data->$getter());
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$getter());
}
}
$sanitized = $values;
@ -40,8 +40,8 @@ class ObjectSerializer {
* @param string $value a string which will be part of the path
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode(self::toString($value));
public function toPathValue($value) {
return rawurlencode($this->toString($value));
}
/**
@ -52,11 +52,11 @@ class ObjectSerializer {
* @param object $object an object to be serialized to a string
* @return string the serialized object
*/
public static function toQueryValue($object) {
public function toQueryValue($object) {
if (is_array($object)) {
return implode(',', $object);
} else {
return self::toString($object);
return $this->toString($object);
}
}
@ -67,8 +67,8 @@ class ObjectSerializer {
* @param string $value a string which will be part of the header
* @return string the header string
*/
public static function toHeaderValue($value) {
return self::toString($value);
public function toHeaderValue($value) {
return $this->toString($value);
}
/**
@ -78,8 +78,8 @@ class ObjectSerializer {
* @param string $value the value of the form parameter
* @return string the form string
*/
public static function toFormValue($value) {
return self::toString($value);
public function toFormValue($value) {
return $this->toString($value);
}
/**
@ -89,7 +89,7 @@ class ObjectSerializer {
* @param string $value the value of the parameter
* @return string the header string
*/
public static function toString($value) {
public function toString($value) {
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ISO8601);
} else {
@ -104,7 +104,7 @@ class ObjectSerializer {
* @param string $class class name is passed as a string
* @return object an instance of $class
*/
public static function deserialize($data, $class) {
public function deserialize($data, $class) {
if (null === $data) {
$deserialized = null;
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
@ -114,14 +114,14 @@ class ObjectSerializer {
$subClass_array = explode(',', $inner, 2);
$subClass = $subClass_array[1];
foreach ($data as $key => $value) {
$deserialized[$key] = self::deserialize($value, $subClass);
$deserialized[$key] = $this->deserialize($value, $subClass);
}
}
} elseif (strcasecmp(substr($class, -2),'[]') == 0) {
$subClass = substr($class, 0, -2);
$values = array();
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass);
$values[] = $this->deserialize($value, $subClass);
}
$deserialized = $values;
} elseif ($class == 'DateTime') {
@ -140,7 +140,7 @@ class ObjectSerializer {
$propertyValue = $data->{$instance::$attributeMap[$property]};
if (isset($propertyValue)) {
$instance->$propertySetter(self::deserialize($propertyValue, $type));
$instance->$propertySetter($this->deserialize($propertyValue, $type));
}
}
$deserialized = $instance;

View File

@ -95,21 +95,21 @@ class {{classname}} {
{{#queryParams}}// query params
if(${{paramName}} !== null) {
$queryParams['{{baseName}}'] = ObjectSerializer::toQueryValue(${{paramName}});
$queryParams['{{baseName}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
}{{/queryParams}}
{{#headerParams}}// header params
if(${{paramName}} !== null) {
$headerParams['{{baseName}}'] = ObjectSerializer::toHeaderValue(${{paramName}});
$headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
}{{/headerParams}}
{{#pathParams}}// path params
if(${{paramName}} !== null) {
$resourcePath = str_replace("{" . "{{baseName}}" . "}",
ObjectSerializer::toPathValue(${{paramName}}),
$this->apiClient->getSerializer()->toPathValue(${{paramName}}),
$resourcePath);
}{{/pathParams}}
{{#formParams}}// form params
if (${{paramName}} !== null) {
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}ObjectSerializer::toFormValue(${{paramName}});
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->getSerializer()->toFormValue(${{paramName}});
}{{/formParams}}
{{#bodyParams}}// body params
$_tempBody = null;
@ -140,7 +140,7 @@ class {{classname}} {
} catch (ApiException $e) {
switch ($e->getCode()) { {{#responses}}{{#dataType}}
case {{code}}:
$data = ObjectSerializer::deserialize($e->getResponseBody(), '{{dataType}}');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}');
$e->setResponseObject($data);
break;{{/dataType}}{{/responses}}
}
@ -152,7 +152,7 @@ class {{classname}} {
return null;
}
$responseObject = ObjectSerializer::deserialize($response,'{{returnType}}');
$responseObject = $this->apiClient->getSerializer()->deserialize($response,'{{returnType}}');
return $responseObject;
{{/returnType}}
}