Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions modules/swagger-codegen/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}" . "}",
Expand Down
15 changes: 15 additions & 0 deletions samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ public function findPetsByStatusWithHttpInfo($status = null)
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array());

// query params

if (is_array($status)) {
$status = $this->apiClient->getSerializer()->serializeCollection($status, 'multi', true);
}

if ($status !== null) {
$queryParams['status'] = $this->apiClient->getSerializer()->toQueryValue($status);
}
Expand Down Expand Up @@ -391,6 +396,11 @@ public function findPetsByTagsWithHttpInfo($tags = null)
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array());

// query params

if (is_array($tags)) {
$tags = $this->apiClient->getSerializer()->serializeCollection($tags, 'multi', true);
}

if ($tags !== null) {
$queryParams['tags'] = $this->apiClient->getSerializer()->toQueryValue($tags);
}
Expand Down Expand Up @@ -487,6 +497,7 @@ public function getPetByIdWithHttpInfo($pet_id)


// path params

if ($pet_id !== null) {
$resourcePath = str_replace(
"{" . "petId" . "}",
Expand Down Expand Up @@ -591,6 +602,7 @@ public function updatePetWithFormWithHttpInfo($pet_id, $name = null, $status = n


// path params

if ($pet_id !== null) {
$resourcePath = str_replace(
"{" . "petId" . "}",
Expand Down Expand Up @@ -694,10 +706,12 @@ public function deletePetWithHttpInfo($pet_id, $api_key = null)


// header params

if ($api_key !== null) {
$headerParams['api_key'] = $this->apiClient->getSerializer()->toHeaderValue($api_key);
}
// path params

if ($pet_id !== null) {
$resourcePath = str_replace(
"{" . "petId" . "}",
Expand Down Expand Up @@ -792,6 +806,7 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi


// path params

if ($pet_id !== null) {
$resourcePath = str_replace(
"{" . "petId" . "}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function getOrderByIdWithHttpInfo($order_id)


// path params

if ($order_id !== null) {
$resourcePath = str_replace(
"{" . "orderId" . "}",
Expand Down Expand Up @@ -407,6 +408,7 @@ public function deleteOrderWithHttpInfo($order_id)


// path params

if ($order_id !== null) {
$resourcePath = str_replace(
"{" . "orderId" . "}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,11 @@ public function loginUserWithHttpInfo($username = null, $password = null)
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array());

// query params

if ($username !== null) {
$queryParams['username'] = $this->apiClient->getSerializer()->toQueryValue($username);
}// query params

if ($password !== null) {
$queryParams['password'] = $this->apiClient->getSerializer()->toQueryValue($password);
}
Expand Down Expand Up @@ -537,6 +539,7 @@ public function getUserByNameWithHttpInfo($username)


// path params

if ($username !== null) {
$resourcePath = str_replace(
"{" . "username" . "}",
Expand Down Expand Up @@ -632,6 +635,7 @@ public function updateUserWithHttpInfo($username, $body = null)


// path params

if ($username !== null) {
$resourcePath = str_replace(
"{" . "username" . "}",
Expand Down Expand Up @@ -721,6 +725,7 @@ public function deleteUserWithHttpInfo($username)


// path params

if ($username !== null) {
$resourcePath = str_replace(
"{" . "username" . "}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public static function toDebugReport()
$report = "PHP SDK (Swagger\Client) Debug Report:\n";
$report .= " OS: ".php_uname()."\n";
$report .= " PHP Version: ".phpversion()."\n";
$report .= " Swagger Spec Version: 1.0.0\n";
$report .= " OpenAPI Spec Version: 1.0.0\n";
$report .= " SDK Package Version: 1.0.0\n";
$report .= " Temp Folder Path: ".self::getDefaultConfiguration()->getTempFolderPath()."\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ public function toString($value)
}
}

/**
* 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
*
Expand Down Expand Up @@ -193,7 +226,7 @@ public function deserialize($data, $class, $httpHeader=null)
$deserialized = $values;
} elseif ($class === '\DateTime') {
$deserialized = new \DateTime($data);
} elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) {
} elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) {
settype($data, $class);
$deserialized = $data;
} elseif ($class === '\SplFileObject') {
Expand Down