forked from loafle/openapi-generator-original
Merge pull request #1499 from arnested/collectionFormat
Accept arrays as arguments to collection parameters in PHP client.
This commit is contained in:
@@ -161,6 +161,39 @@ class ObjectSerializer
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize an array to a string.
|
||||
*
|
||||
* @param array $collection collection to serialize to a string
|
||||
* @param string $collectionFormat the format use for serialization (csv,
|
||||
* ssv, tsv, pipes, multi)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function serializeCollection(array $collection, $collectionFormat, $allowCollectionFormatMulti=false)
|
||||
{
|
||||
if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) {
|
||||
// http_build_query() almost does the job for us. We just
|
||||
// need to fix the result of multidimensional arrays.
|
||||
return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
|
||||
}
|
||||
switch ($collectionFormat) {
|
||||
case 'pipes':
|
||||
return implode('|', $collection);
|
||||
|
||||
case 'tsv':
|
||||
return implode("\t", $collection);
|
||||
|
||||
case 'ssv':
|
||||
return implode(' ', $collection);
|
||||
|
||||
case 'csv':
|
||||
// Deliberate fall through. CSV is default format.
|
||||
default:
|
||||
return implode(',', $collection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a JSON string into an object
|
||||
*
|
||||
|
||||
@@ -139,14 +139,29 @@ use \{{invokerPackage}}\ObjectSerializer;
|
||||
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
|
||||
|
||||
{{#queryParams}}// query params
|
||||
{{#collectionFormat}}
|
||||
if (is_array(${{paramName}})) {
|
||||
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}', true);
|
||||
}
|
||||
{{/collectionFormat}}
|
||||
if (${{paramName}} !== null) {
|
||||
$queryParams['{{baseName}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
|
||||
}{{/queryParams}}
|
||||
{{#headerParams}}// header params
|
||||
{{#collectionFormat}}
|
||||
if (is_array(${{paramName}})) {
|
||||
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
|
||||
}
|
||||
{{/collectionFormat}}
|
||||
if (${{paramName}} !== null) {
|
||||
$headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
|
||||
}{{/headerParams}}
|
||||
{{#pathParams}}// path params
|
||||
{{#collectionFormat}}
|
||||
if (is_array(${{paramName}})) {
|
||||
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
|
||||
}
|
||||
{{/collectionFormat}}
|
||||
if (${{paramName}} !== null) {
|
||||
$resourcePath = str_replace(
|
||||
"{" . "{{baseName}}" . "}",
|
||||
|
||||
Reference in New Issue
Block a user