Skip to content

Commit d71d164

Browse files
committed
Accept arrays as arguments to collection parameters.
1 parent cafea6f commit d71d164

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ class ObjectSerializer
161161
}
162162
}
163163

164+
/**
165+
* Serialize an array to a string.
166+
*
167+
* @param array $collection collection to serialize to a string
168+
* @param string $collectionFormat the format use for serialization (csv,
169+
* ssv, tsv, pipes, multi)
170+
*
171+
* @return string
172+
*/
173+
public function serializeCollection(array $collection, $collectionFormat, $allowCollectionFormatMulti=false)
174+
{
175+
if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) {
176+
// http_build_query() almost does the job for us. We just
177+
// need to fix the result of multidimensional arrays.
178+
return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
179+
}
180+
switch ($collectionFormat) {
181+
case 'pipes':
182+
return implode('|', $collection);
183+
184+
case 'tsv':
185+
return implode("\t", $collection);
186+
187+
case 'ssv':
188+
return implode(' ', $collection);
189+
190+
case 'csv':
191+
// Deliberate fall through. CSV is default format.
192+
default:
193+
return implode(',', $collection);
194+
}
195+
}
196+
164197
/**
165198
* Deserialize a JSON string into an object
166199
*

modules/swagger-codegen/src/main/resources/php/api.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,29 @@ use \{{invokerPackage}}\ObjectSerializer;
139139
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
140140

141141
{{#queryParams}}// query params
142+
{{#collectionFormat}}
143+
if (is_array(${{paramName}})) {
144+
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}', true);
145+
}
146+
{{/collectionFormat}}
142147
if (${{paramName}} !== null) {
143148
$queryParams['{{baseName}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
144149
}{{/queryParams}}
145150
{{#headerParams}}// header params
151+
{{#collectionFormat}}
152+
if (is_array(${{paramName}})) {
153+
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
154+
}
155+
{{/collectionFormat}}
146156
if (${{paramName}} !== null) {
147157
$headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
148158
}{{/headerParams}}
149159
{{#pathParams}}// path params
160+
{{#collectionFormat}}
161+
if (is_array(${{paramName}})) {
162+
${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
163+
}
164+
{{/collectionFormat}}
150165
if (${{paramName}} !== null) {
151166
$resourcePath = str_replace(
152167
"{" . "{{baseName}}" . "}",

0 commit comments

Comments
 (0)