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 @@ -108,11 +108,11 @@ class ObjectSerializer
}
}
} else {
foreach($data as $property => $value) {
foreach ($data as $property => $value) {
$values[$property] = self::sanitizeForSerialization($value);
}
}
return (object)$values;
return (object) $values;
} else {
return (string)$data;
}
Expand Down Expand Up @@ -170,30 +170,30 @@ class ObjectSerializer
*/
private static function isEmptyValue(mixed $value, string $openApiType): bool
{
# If empty() returns false, it is not empty regardless of its type.
// If empty() returns false, it is not empty regardless of its type.
if (!empty($value)) {
return false;
}

# Null is always empty, as we cannot send a real "null" value in a query parameter.
// Null is always empty, as we cannot send a real "null" value in a query parameter.
if ($value === null) {
return true;
}

return match ($openApiType) {
# For numeric values, false and '' are considered empty.
# This comparison is safe for floating point values, since the previous call to empty() will
# filter out values that don't match 0.
// For numeric values, false and '' are considered empty.
// This comparison is safe for floating point values, since the previous call to empty() will
// filter out values that don't match 0.
'int','integer' => $value !== 0,
'number'|'float' => $value !== 0 && $value !== 0.0,

# For boolean values, '' is considered empty
// For boolean values, '' is considered empty
'bool','boolean' => !in_array($value, [false, 0], true),

# For string values, '' is considered empty.
// For string values, '' is considered empty.
'string' => $value === '',

# For all the other types, any value at this point can be considered empty.
// For all the other types, any value at this point can be considered empty.
default => true
};
}
Expand All @@ -220,10 +220,10 @@ class ObjectSerializer
bool $required = true
): array {

# Check if we should omit this parameter from the query. This should only happen when:
# - Parameter is NOT required; AND
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
# example, 0 as "int" or "boolean" is NOT an empty value.
// Check if we should omit this parameter from the query. This should only happen when:
// - Parameter is NOT required; AND
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
// example, 0 as "int" or "boolean" is NOT an empty value.
if (self::isEmptyValue($value, $openApiType)) {
if ($required) {
return ["{$paramName}" => ''];
Expand All @@ -232,8 +232,8 @@ class ObjectSerializer
}
}

# Handle DateTime objects in query
if($openApiType === "\DateTime" && $value instanceof DateTime) {
// Handle DateTime objects in query
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

Expand All @@ -243,7 +243,9 @@ class ObjectSerializer
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
if (!is_array($arr)) return $arr;
if (!is_array($arr)) {
return $arr;
}

foreach ($arr as $k => $v) {
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
Expand Down Expand Up @@ -491,7 +493,7 @@ class ObjectSerializer
$data = is_string($data) ? json_decode($data) : $data;

if (is_array($data)) {
$data = (object)$data;
$data = (object) $data;
}

// If a discriminator is defined and points to a valid subclass, use it.
Expand Down Expand Up @@ -530,17 +532,17 @@ class ObjectSerializer
}

/**
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
{
if (!$params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public static function sanitizeForSerialization(mixed $data, ?string $type = nul
}
}
} else {
foreach($data as $property => $value) {
foreach ($data as $property => $value) {
$values[$property] = self::sanitizeForSerialization($value);
}
}
return (object)$values;
return (object) $values;
} else {
return (string)$data;
}
Expand Down Expand Up @@ -179,30 +179,30 @@ public static function toPathValue(string $value): string
*/
private static function isEmptyValue(mixed $value, string $openApiType): bool
{
# If empty() returns false, it is not empty regardless of its type.
// If empty() returns false, it is not empty regardless of its type.
if (!empty($value)) {
return false;
}

# Null is always empty, as we cannot send a real "null" value in a query parameter.
// Null is always empty, as we cannot send a real "null" value in a query parameter.
if ($value === null) {
return true;
}

return match ($openApiType) {
# For numeric values, false and '' are considered empty.
# This comparison is safe for floating point values, since the previous call to empty() will
# filter out values that don't match 0.
// For numeric values, false and '' are considered empty.
// This comparison is safe for floating point values, since the previous call to empty() will
// filter out values that don't match 0.
'int','integer' => $value !== 0,
'number'|'float' => $value !== 0 && $value !== 0.0,

# For boolean values, '' is considered empty
// For boolean values, '' is considered empty
'bool','boolean' => !in_array($value, [false, 0], true),

# For string values, '' is considered empty.
// For string values, '' is considered empty.
'string' => $value === '',

# For all the other types, any value at this point can be considered empty.
// For all the other types, any value at this point can be considered empty.
default => true
};
}
Expand All @@ -229,10 +229,10 @@ public static function toQueryValue(
bool $required = true
): array {

# Check if we should omit this parameter from the query. This should only happen when:
# - Parameter is NOT required; AND
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
# example, 0 as "int" or "boolean" is NOT an empty value.
// Check if we should omit this parameter from the query. This should only happen when:
// - Parameter is NOT required; AND
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
// example, 0 as "int" or "boolean" is NOT an empty value.
if (self::isEmptyValue($value, $openApiType)) {
if ($required) {
return ["{$paramName}" => ''];
Expand All @@ -241,8 +241,8 @@ public static function toQueryValue(
}
}

# Handle DateTime objects in query
if($openApiType === "\DateTime" && $value instanceof DateTime) {
// Handle DateTime objects in query
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

Expand All @@ -252,7 +252,9 @@ public static function toQueryValue(
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
if (!is_array($arr)) return $arr;
if (!is_array($arr)) {
return $arr;
}

foreach ($arr as $k => $v) {
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
Expand Down Expand Up @@ -500,7 +502,7 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
$data = is_string($data) ? json_decode($data) : $data;

if (is_array($data)) {
$data = (object)$data;
$data = (object) $data;
}

// If a discriminator is defined and points to a valid subclass, use it.
Expand Down Expand Up @@ -539,17 +541,17 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
}

/**
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
{
if (!$params) {
Expand Down
60 changes: 31 additions & 29 deletions samples/client/echo_api/php-nextgen/src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public static function sanitizeForSerialization(mixed $data, ?string $type = nul
}
}
} else {
foreach($data as $property => $value) {
foreach ($data as $property => $value) {
$values[$property] = self::sanitizeForSerialization($value);
}
}
return (object)$values;
return (object) $values;
} else {
return (string)$data;
}
Expand Down Expand Up @@ -179,30 +179,30 @@ public static function toPathValue(string $value): string
*/
private static function isEmptyValue(mixed $value, string $openApiType): bool
{
# If empty() returns false, it is not empty regardless of its type.
// If empty() returns false, it is not empty regardless of its type.
if (!empty($value)) {
return false;
}

# Null is always empty, as we cannot send a real "null" value in a query parameter.
// Null is always empty, as we cannot send a real "null" value in a query parameter.
if ($value === null) {
return true;
}

return match ($openApiType) {
# For numeric values, false and '' are considered empty.
# This comparison is safe for floating point values, since the previous call to empty() will
# filter out values that don't match 0.
// For numeric values, false and '' are considered empty.
// This comparison is safe for floating point values, since the previous call to empty() will
// filter out values that don't match 0.
'int','integer' => $value !== 0,
'number'|'float' => $value !== 0 && $value !== 0.0,

# For boolean values, '' is considered empty
// For boolean values, '' is considered empty
'bool','boolean' => !in_array($value, [false, 0], true),

# For string values, '' is considered empty.
// For string values, '' is considered empty.
'string' => $value === '',

# For all the other types, any value at this point can be considered empty.
// For all the other types, any value at this point can be considered empty.
default => true
};
}
Expand All @@ -229,10 +229,10 @@ public static function toQueryValue(
bool $required = true
): array {

# Check if we should omit this parameter from the query. This should only happen when:
# - Parameter is NOT required; AND
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
# example, 0 as "int" or "boolean" is NOT an empty value.
// Check if we should omit this parameter from the query. This should only happen when:
// - Parameter is NOT required; AND
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
// example, 0 as "int" or "boolean" is NOT an empty value.
if (self::isEmptyValue($value, $openApiType)) {
if ($required) {
return ["{$paramName}" => ''];
Expand All @@ -241,8 +241,8 @@ public static function toQueryValue(
}
}

# Handle DateTime objects in query
if($openApiType === "\DateTime" && $value instanceof DateTime) {
// Handle DateTime objects in query
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

Expand All @@ -252,7 +252,9 @@ public static function toQueryValue(
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
if (!is_array($arr)) return $arr;
if (!is_array($arr)) {
return $arr;
}

foreach ($arr as $k => $v) {
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
Expand Down Expand Up @@ -500,7 +502,7 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
$data = is_string($data) ? json_decode($data) : $data;

if (is_array($data)) {
$data = (object)$data;
$data = (object) $data;
}

// If a discriminator is defined and points to a valid subclass, use it.
Expand Down Expand Up @@ -539,17 +541,17 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
}

/**
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
* Build a query string from an array of key value pairs.
*
* This function can use the return value of `parse()` to build a query
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
*/
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
{
if (!$params) {
Expand Down
Loading
Loading