Accept arrays as arguments to collection parameters.

This commit is contained in:
Arne Jørgensen
2016-01-03 22:45:07 +01:00
parent cafea6f726
commit d71d164843
2 changed files with 48 additions and 0 deletions
@@ -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
*