separate PHP query, header and path param serialization and encoding

This commit is contained in:
Russell Horton 2013-02-10 13:24:01 -08:00
parent 62352a126e
commit 00ac23c462
2 changed files with 28 additions and 6 deletions

View File

@ -137,18 +137,39 @@ class APIClient {
/** /**
* Take value and turn it into a string suitable for inclusion in * Take value and turn it into a string suitable for inclusion in
* the path or the header * the path, by url-encoding.
* @param string $value a string which will be part of the path
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode($value);
}
/**
* Take value and turn it into a string suitable for inclusion in
* the query, by imploding comma-separated if it's an object.
* If it's a string, pass through unchanged. It will be url-encoded
* later.
* @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 toPathValue($object) { public static function toQueryValue($object) {
if (is_array($object)) { if (is_array($object)) {
return rawurlencode(implode(',', $object)); return implode(',', $object);
} else { } else {
return rawurlencode($object); return $object;
} }
} }
/**
* Just pass through the header value for now. Placeholder in case we
* find out we need to do something with header values.
* @param string $value a string which will be part of the header
* @return string the header string
*/
public static function toHeaderValue($value) {
return $value;
}
/** /**
* Deserialize a JSON string into an object * Deserialize a JSON string into an object
@ -218,3 +239,4 @@ class APIClient {
?> ?>

View File

@ -48,13 +48,13 @@ class {{classname}} {
{{#queryParams}} {{#queryParams}}
if(${{paramName}} != null) { if(${{paramName}} != null) {
$queryParams['{{paramName}}'] = $this->apiClient->toPathValue(${{paramName}}); $queryParams['{{paramName}}'] = $this->apiClient->toQueryValue(${{paramName}});
} }
{{/queryParams}} {{/queryParams}}
{{#headerParams}} {{#headerParams}}
if(${{paramName}} != null) { if(${{paramName}} != null) {
$headerParams['{{paramName}}'] = $this->apiClient->toPathValue(${{paramName}}); $headerParams['{{paramName}}'] = $this->apiClient->toHeaderValue(${{paramName}});
} }
{{/headerParams}} {{/headerParams}}