mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-20 07:39:08 +00:00
[PHP] - Add FormDataProcessor to handle nested ModelInterface data (#20990)
* [PHP] - Add FormDataProcessor to handle nested ModelInterface data * Generating samples * Updates php-nextgen and psr-18 * Adds tests * Some more tests * One last test * Updating files * Fixing diff * Test fix * Updating samples
This commit is contained in:
@@ -28,7 +28,6 @@
|
||||
|
||||
namespace OpenAPI\Client;
|
||||
|
||||
use ArrayAccess;
|
||||
use DateTimeInterface;
|
||||
use DateTime;
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
@@ -326,37 +325,6 @@ class ObjectSerializer
|
||||
return self::toString($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into an array suitable for inclusion in
|
||||
* the http body (form parameter). If it's a string, pass through unchanged
|
||||
* If it's a datetime object, format it in ISO8601
|
||||
*
|
||||
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
|
||||
*
|
||||
* @return array [key => value] of formdata
|
||||
*/
|
||||
public static function toFormValue(
|
||||
string $key,
|
||||
string|bool|array|DateTime|ArrayAccess|\SplFileObject $value,
|
||||
): array {
|
||||
if ($value instanceof \SplFileObject) {
|
||||
return [$key => $value->getRealPath()];
|
||||
} elseif (is_array($value) || $value instanceof ArrayAccess) {
|
||||
$flattened = [];
|
||||
$result = [];
|
||||
|
||||
self::flattenArray(json_decode(json_encode($value), true), $flattened);
|
||||
|
||||
foreach ($flattened as $k => $v) {
|
||||
$result["{$key}{$k}"] = self::toString($v);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
return [$key => self::toString($value)];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take value and turn it into a string suitable for inclusion in
|
||||
* the parameter. If it's a string, pass through unchanged
|
||||
@@ -622,58 +590,4 @@ class ObjectSerializer
|
||||
|
||||
return $qs ? (string) substr($qs, 0, -1) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Flattens an array of Model object and generates an array compatible
|
||||
* with formdata - a single-level array where the keys use bracket
|
||||
* notation to signify nested data.
|
||||
*
|
||||
* credit: https://github.com/FranBar1966/FlatPHP
|
||||
*/
|
||||
private static function flattenArray(
|
||||
ArrayAccess|array $source,
|
||||
array &$destination,
|
||||
string $start = '',
|
||||
) {
|
||||
$opt = [
|
||||
'prefix' => '[',
|
||||
'suffix' => ']',
|
||||
'suffix-end' => true,
|
||||
'prefix-list' => '[',
|
||||
'suffix-list' => ']',
|
||||
'suffix-list-end' => true,
|
||||
];
|
||||
|
||||
if (!is_array($source)) {
|
||||
$source = (array) $source;
|
||||
}
|
||||
|
||||
if (array_is_list($source)) {
|
||||
$currentPrefix = $opt['prefix-list'];
|
||||
$currentSuffix = $opt['suffix-list'];
|
||||
$currentSuffixEnd = $opt['suffix-list-end'];
|
||||
} else {
|
||||
$currentPrefix = $opt['prefix'];
|
||||
$currentSuffix = $opt['suffix'];
|
||||
$currentSuffixEnd = $opt['suffix-end'];
|
||||
}
|
||||
|
||||
$currentName = $start;
|
||||
|
||||
foreach ($source as $key => $val) {
|
||||
$currentName .= $currentPrefix.$key;
|
||||
|
||||
if (is_array($val) && !empty($val)) {
|
||||
$currentName .= "{$currentSuffix}";
|
||||
self::flattenArray($val, $destination, $currentName);
|
||||
} else {
|
||||
if ($currentSuffixEnd) {
|
||||
$currentName .= $currentSuffix;
|
||||
}
|
||||
$destination[$currentName] = self::toString($val);
|
||||
}
|
||||
|
||||
$currentName = $start;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user