forked from loafle/openapi-generator-original
change serializer to be instanced, rather than static methods
This commit is contained in:
parent
2fced0f634
commit
c91a1b963b
@ -25,11 +25,12 @@ class ApiClient {
|
|||||||
public static $PUT = "PUT";
|
public static $PUT = "PUT";
|
||||||
public static $DELETE = "DELETE";
|
public static $DELETE = "DELETE";
|
||||||
|
|
||||||
/**
|
/** @var Configuration */
|
||||||
* @var Configuration
|
|
||||||
*/
|
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
/** @var ObjectSerializer */
|
||||||
|
protected $serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Configuration $config config for this ApiClient
|
* @param Configuration $config config for this ApiClient
|
||||||
*/
|
*/
|
||||||
@ -39,6 +40,7 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
$this->serializer = new ObjectSerializer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,6 +51,14 @@ class ApiClient {
|
|||||||
return $this->config;
|
return $this->config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the serializer
|
||||||
|
* @return ObjectSerializer
|
||||||
|
*/
|
||||||
|
public function getSerializer() {
|
||||||
|
return $this->serializer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get API key (with prefix if set)
|
* Get API key (with prefix if set)
|
||||||
* @param string $apiKey name of apikey
|
* @param string $apiKey name of apikey
|
||||||
@ -96,7 +106,7 @@ class ApiClient {
|
|||||||
$postData = http_build_query($postData);
|
$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
|
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;
|
$url = $this->config->getHost() . $resourcePath;
|
||||||
|
@ -8,14 +8,14 @@ class ObjectSerializer {
|
|||||||
* @param mixed $data the data to serialize
|
* @param mixed $data the data to serialize
|
||||||
* @return string serialized form of $data
|
* @return string serialized form of $data
|
||||||
*/
|
*/
|
||||||
public static function sanitizeForSerialization($data) {
|
public function sanitizeForSerialization($data) {
|
||||||
if (is_scalar($data) || null === $data) {
|
if (is_scalar($data) || null === $data) {
|
||||||
$sanitized = $data;
|
$sanitized = $data;
|
||||||
} else if ($data instanceof \DateTime) {
|
} else if ($data instanceof \DateTime) {
|
||||||
$sanitized = $data->format(\DateTime::ISO8601);
|
$sanitized = $data->format(\DateTime::ISO8601);
|
||||||
} else if (is_array($data)) {
|
} else if (is_array($data)) {
|
||||||
foreach ($data as $property => $value) {
|
foreach ($data as $property => $value) {
|
||||||
$data[$property] = self::sanitizeForSerialization($value);
|
$data[$property] = $this->sanitizeForSerialization($value);
|
||||||
}
|
}
|
||||||
$sanitized = $data;
|
$sanitized = $data;
|
||||||
} else if (is_object($data)) {
|
} else if (is_object($data)) {
|
||||||
@ -23,7 +23,7 @@ class ObjectSerializer {
|
|||||||
foreach (array_keys($data::$swaggerTypes) as $property) {
|
foreach (array_keys($data::$swaggerTypes) as $property) {
|
||||||
$getter = $data::$getters[$property];
|
$getter = $data::$getters[$property];
|
||||||
if ($data->$getter() !== null) {
|
if ($data->$getter() !== null) {
|
||||||
$values[$data::$attributeMap[$property]] = self::sanitizeForSerialization($data->$getter());
|
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$getter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sanitized = $values;
|
$sanitized = $values;
|
||||||
@ -40,8 +40,8 @@ class ObjectSerializer {
|
|||||||
* @param string $value a string which will be part of the path
|
* @param string $value a string which will be part of the path
|
||||||
* @return string the serialized object
|
* @return string the serialized object
|
||||||
*/
|
*/
|
||||||
public static function toPathValue($value) {
|
public function toPathValue($value) {
|
||||||
return rawurlencode(self::toString($value));
|
return rawurlencode($this->toString($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,11 +52,11 @@ class ObjectSerializer {
|
|||||||
* @param object $object an object to be serialized to a string
|
* @param object $object an object to be serialized to a string
|
||||||
* @return string the serialized object
|
* @return string the serialized object
|
||||||
*/
|
*/
|
||||||
public static function toQueryValue($object) {
|
public function toQueryValue($object) {
|
||||||
if (is_array($object)) {
|
if (is_array($object)) {
|
||||||
return implode(',', $object);
|
return implode(',', $object);
|
||||||
} else {
|
} 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
|
* @param string $value a string which will be part of the header
|
||||||
* @return string the header string
|
* @return string the header string
|
||||||
*/
|
*/
|
||||||
public static function toHeaderValue($value) {
|
public function toHeaderValue($value) {
|
||||||
return self::toString($value);
|
return $this->toString($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,8 +78,8 @@ class ObjectSerializer {
|
|||||||
* @param string $value the value of the form parameter
|
* @param string $value the value of the form parameter
|
||||||
* @return string the form string
|
* @return string the form string
|
||||||
*/
|
*/
|
||||||
public static function toFormValue($value) {
|
public function toFormValue($value) {
|
||||||
return self::toString($value);
|
return $this->toString($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,7 +89,7 @@ class ObjectSerializer {
|
|||||||
* @param string $value the value of the parameter
|
* @param string $value the value of the parameter
|
||||||
* @return string the header string
|
* @return string the header string
|
||||||
*/
|
*/
|
||||||
public static function toString($value) {
|
public function toString($value) {
|
||||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||||
return $value->format(\DateTime::ISO8601);
|
return $value->format(\DateTime::ISO8601);
|
||||||
} else {
|
} else {
|
||||||
@ -104,7 +104,7 @@ class ObjectSerializer {
|
|||||||
* @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($data, $class) {
|
public function deserialize($data, $class) {
|
||||||
if (null === $data) {
|
if (null === $data) {
|
||||||
$deserialized = null;
|
$deserialized = null;
|
||||||
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
|
} 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_array = explode(',', $inner, 2);
|
||||||
$subClass = $subClass_array[1];
|
$subClass = $subClass_array[1];
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
$deserialized[$key] = self::deserialize($value, $subClass);
|
$deserialized[$key] = $this->deserialize($value, $subClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (strcasecmp(substr($class, -2),'[]') == 0) {
|
} elseif (strcasecmp(substr($class, -2),'[]') == 0) {
|
||||||
$subClass = substr($class, 0, -2);
|
$subClass = substr($class, 0, -2);
|
||||||
$values = array();
|
$values = array();
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
$values[] = self::deserialize($value, $subClass);
|
$values[] = $this->deserialize($value, $subClass);
|
||||||
}
|
}
|
||||||
$deserialized = $values;
|
$deserialized = $values;
|
||||||
} elseif ($class == 'DateTime') {
|
} elseif ($class == 'DateTime') {
|
||||||
@ -140,7 +140,7 @@ class ObjectSerializer {
|
|||||||
|
|
||||||
$propertyValue = $data->{$instance::$attributeMap[$property]};
|
$propertyValue = $data->{$instance::$attributeMap[$property]};
|
||||||
if (isset($propertyValue)) {
|
if (isset($propertyValue)) {
|
||||||
$instance->$propertySetter(self::deserialize($propertyValue, $type));
|
$instance->$propertySetter($this->deserialize($propertyValue, $type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$deserialized = $instance;
|
$deserialized = $instance;
|
||||||
|
@ -95,21 +95,21 @@ class {{classname}} {
|
|||||||
|
|
||||||
{{#queryParams}}// query params
|
{{#queryParams}}// query params
|
||||||
if(${{paramName}} !== null) {
|
if(${{paramName}} !== null) {
|
||||||
$queryParams['{{baseName}}'] = ObjectSerializer::toQueryValue(${{paramName}});
|
$queryParams['{{baseName}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
|
||||||
}{{/queryParams}}
|
}{{/queryParams}}
|
||||||
{{#headerParams}}// header params
|
{{#headerParams}}// header params
|
||||||
if(${{paramName}} !== null) {
|
if(${{paramName}} !== null) {
|
||||||
$headerParams['{{baseName}}'] = ObjectSerializer::toHeaderValue(${{paramName}});
|
$headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
|
||||||
}{{/headerParams}}
|
}{{/headerParams}}
|
||||||
{{#pathParams}}// path params
|
{{#pathParams}}// path params
|
||||||
if(${{paramName}} !== null) {
|
if(${{paramName}} !== null) {
|
||||||
$resourcePath = str_replace("{" . "{{baseName}}" . "}",
|
$resourcePath = str_replace("{" . "{{baseName}}" . "}",
|
||||||
ObjectSerializer::toPathValue(${{paramName}}),
|
$this->apiClient->getSerializer()->toPathValue(${{paramName}}),
|
||||||
$resourcePath);
|
$resourcePath);
|
||||||
}{{/pathParams}}
|
}{{/pathParams}}
|
||||||
{{#formParams}}// form params
|
{{#formParams}}// form params
|
||||||
if (${{paramName}} !== null) {
|
if (${{paramName}} !== null) {
|
||||||
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}ObjectSerializer::toFormValue(${{paramName}});
|
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->getSerializer()->toFormValue(${{paramName}});
|
||||||
}{{/formParams}}
|
}{{/formParams}}
|
||||||
{{#bodyParams}}// body params
|
{{#bodyParams}}// body params
|
||||||
$_tempBody = null;
|
$_tempBody = null;
|
||||||
@ -140,7 +140,7 @@ class {{classname}} {
|
|||||||
} catch (ApiException $e) {
|
} catch (ApiException $e) {
|
||||||
switch ($e->getCode()) { {{#responses}}{{#dataType}}
|
switch ($e->getCode()) { {{#responses}}{{#dataType}}
|
||||||
case {{code}}:
|
case {{code}}:
|
||||||
$data = ObjectSerializer::deserialize($e->getResponseBody(), '{{dataType}}');
|
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}');
|
||||||
$e->setResponseObject($data);
|
$e->setResponseObject($data);
|
||||||
break;{{/dataType}}{{/responses}}
|
break;{{/dataType}}{{/responses}}
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ class {{classname}} {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$responseObject = ObjectSerializer::deserialize($response,'{{returnType}}');
|
$responseObject = $this->apiClient->getSerializer()->deserialize($response,'{{returnType}}');
|
||||||
return $responseObject;
|
return $responseObject;
|
||||||
{{/returnType}}
|
{{/returnType}}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user