Skip to content

Commit b3d9b3f

Browse files
committed
Includes php-nextgen; tightens up ::toFormValue()
1 parent 9efbc3e commit b3d9b3f

File tree

14 files changed

+395
-135
lines changed

14 files changed

+395
-135
lines changed

modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace {{invokerPackage}};
2020

21+
use ArrayAccess;
2122
use DateTimeInterface;
2223
use DateTime;
2324
use GuzzleHttp\Psr7\Utils;
@@ -316,20 +317,33 @@ class ObjectSerializer
316317
}
317318
318319
/**
319-
* Take value and turn it into a string suitable for inclusion in
320+
* Take value and turn it into an array suitable for inclusion in
320321
* the http body (form parameter). If it's a string, pass through unchanged
321322
* If it's a datetime object, format it in ISO8601
322323
*
323-
* @param string|\SplFileObject $value the value of the form parameter
324+
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
324325
*
325-
* @return string the form string
326+
* @return array [key => value] of formdata
326327
*/
327-
public static function toFormValue(string|\SplFileObject $value): string
328-
{
328+
public static function toFormValue(
329+
string $key,
330+
string|bool|array|DateTime|ArrayAccess|\SplFileObject $value,
331+
): array {
329332
if ($value instanceof \SplFileObject) {
330-
return $value->getRealPath();
333+
return [$key => $value->getRealPath()];
334+
} elseif (is_array($value) || $value instanceof ArrayAccess) {
335+
$flattened = [];
336+
$result = [];
337+
338+
self::flattenArray(json_decode(json_encode($value), true), $flattened);
339+
340+
foreach ($flattened as $k => $v) {
341+
$result["{$key}{$k}"] = self::toString($v);
342+
}
343+
344+
return $result;
331345
} else {
332-
return self::toString($value);
346+
return [$key => self::toString($value)];
333347
}
334348
}
335349
@@ -598,4 +612,58 @@ class ObjectSerializer
598612

599613
return $qs ? (string) substr($qs, 0, -1) : '';
600614
}
615+
616+
/**
617+
* Flattens an array of Model object and generates an array compatible
618+
* with formdata - a single-level array where the keys use bracket
619+
* notation to signify nested data.
620+
*
621+
* credit: https://github.com/FranBar1966/FlatPHP
622+
*/
623+
private static function flattenArray(
624+
ArrayAccess|array $source,
625+
array &$destination,
626+
string $start = '',
627+
) {
628+
$opt = [
629+
'prefix' => '[',
630+
'suffix' => ']',
631+
'suffix-end' => true,
632+
'prefix-list' => '[',
633+
'suffix-list' => ']',
634+
'suffix-list-end' => true,
635+
];
636+
637+
if (!is_array($source)) {
638+
$source = (array) $source;
639+
}
640+
641+
if (array_is_list($source)) {
642+
$currentPrefix = $opt['prefix-list'];
643+
$currentSuffix = $opt['suffix-list'];
644+
$currentSuffixEnd = $opt['suffix-list-end'];
645+
} else {
646+
$currentPrefix = $opt['prefix'];
647+
$currentSuffix = $opt['suffix'];
648+
$currentSuffixEnd = $opt['suffix-end'];
649+
}
650+
651+
$currentName = $start;
652+
653+
foreach ($source as $key => $val) {
654+
$currentName .= $currentPrefix.$key;
655+
656+
if (is_array($val) && !empty($val)) {
657+
$currentName .= "{$currentSuffix}";
658+
self::flattenArray($val, $destination, $currentName);
659+
} else {
660+
if ($currentSuffixEnd) {
661+
$currentName .= $currentSuffix;
662+
}
663+
$destination[$currentName] = self::toString($val);
664+
}
665+
666+
$currentName = $start;
667+
}
668+
}
601669
}

modules/openapi-generator/src/main/resources/php-nextgen/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,13 @@ use {{invokerPackage}}\ObjectSerializer;
759759
$formParams['{{baseName}}'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
760760
? $paramFile
761761
: \GuzzleHttp\Psr7\Utils::tryFopen(
762-
ObjectSerializer::toFormValue($paramFile),
762+
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
763763
'rb'
764764
);
765765
}
766766
{{/isFile}}
767767
{{^isFile}}
768-
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
768+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
769769
{{/isFile}}
770770
}
771771
{{/formParams}}

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -315,34 +315,32 @@ class ObjectSerializer
315315
}
316316
317317
/**
318-
* Take value and turn it into a string suitable for inclusion in
318+
* Take value and turn it into an array suitable for inclusion in
319319
* the http body (form parameter). If it's a string, pass through unchanged
320320
* If it's a datetime object, format it in ISO8601
321321
*
322-
* @param string|\SplFileObject $value the value of the form parameter
322+
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
323323
*
324-
* @return array
324+
* @return array [key => value] of formdata
325325
*/
326326
public static function toFormValue(string $key, mixed $value)
327327
{
328-
$stringable = self::toString($value);
329-
330-
if ($stringable !== null) {
331-
return [$key => $stringable];
332-
} elseif ($value instanceof \SplFileObject) {
328+
if ($value instanceof \SplFileObject) {
333329
return [$key => $value->getRealPath()];
334-
}
330+
} elseif (is_array($value) || $value instanceof ArrayAccess) {
331+
$flattened = [];
332+
$result = [];
335333
336-
$flattened = [];
337-
$result = [];
334+
self::flattenArray(json_decode(json_encode($value), true), $flattened);
338335
339-
self::flattenArray(json_decode(json_encode($value), true), $flattened);
336+
foreach ($flattened as $k => $v) {
337+
$result["{$key}{$k}"] = self::toString($v);
338+
}
340339
341-
foreach ($flattened as $k => $v) {
342-
$result["{$key}{$k}"] = self::toString($v);
340+
return $result;
341+
} else {
342+
return [$key => self::toString($value)];
343343
}
344-
345-
return $result;
346344
}
347345
348346
/**
@@ -353,14 +351,12 @@ class ObjectSerializer
353351
*
354352
* @param float|int|bool|\DateTime $value the value of the parameter
355353
*
356-
* @return string|null the header string
354+
* @return string the header string
357355
*/
358356
public static function toString($value)
359357
{
360358
if ($value instanceof \DateTime) { // datetime in ISO8601 format
361359
return $value->format(self::$dateTimeFormat);
362-
} elseif (!is_scalar($value) && $value !== null) {
363-
return null;
364360
} elseif (is_bool($value)) {
365361
return $value ? 'true' : 'false';
366362
} else {

samples/client/echo_api/php-nextgen-streaming/src/Api/BodyApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ public function testBodyMultipartFormdataArrayOfBinaryRequest(
10341034
$formParams['files'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
10351035
? $paramFile
10361036
: \GuzzleHttp\Psr7\Utils::tryFopen(
1037-
ObjectSerializer::toFormValue($paramFile),
1037+
ObjectSerializer::toFormValue('files', $paramFile)['files'],
10381038
'rb'
10391039
);
10401040
}
@@ -1357,7 +1357,7 @@ public function testBodyMultipartFormdataSingleBinaryRequest(
13571357
$formParams['my-file'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
13581358
? $paramFile
13591359
: \GuzzleHttp\Psr7\Utils::tryFopen(
1360-
ObjectSerializer::toFormValue($paramFile),
1360+
ObjectSerializer::toFormValue('my-file', $paramFile)['my-file'],
13611361
'rb'
13621362
);
13631363
}

samples/client/echo_api/php-nextgen-streaming/src/Api/FormApi.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,15 @@ public function testFormIntegerBooleanStringRequest(
408408

409409
// form params
410410
if ($integer_form !== null) {
411-
$formParams['integer_form'] = ObjectSerializer::toFormValue($integer_form);
411+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('integer_form', $integer_form));
412412
}
413413
// form params
414414
if ($boolean_form !== null) {
415-
$formParams['boolean_form'] = ObjectSerializer::toFormValue($boolean_form);
415+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('boolean_form', $boolean_form));
416416
}
417417
// form params
418418
if ($string_form !== null) {
419-
$formParams['string_form'] = ObjectSerializer::toFormValue($string_form);
419+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('string_form', $string_form));
420420
}
421421

422422
$headers = $this->headerSelector->selectHeaders(
@@ -735,7 +735,7 @@ public function testFormObjectMultipartRequest(
735735

736736
// form params
737737
if ($marker !== null) {
738-
$formParams['marker'] = ObjectSerializer::toFormValue($marker);
738+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('marker', $marker));
739739
}
740740

741741
$headers = $this->headerSelector->selectHeaders(
@@ -1103,27 +1103,27 @@ public function testFormOneofRequest(
11031103

11041104
// form params
11051105
if ($form1 !== null) {
1106-
$formParams['form1'] = ObjectSerializer::toFormValue($form1);
1106+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('form1', $form1));
11071107
}
11081108
// form params
11091109
if ($form2 !== null) {
1110-
$formParams['form2'] = ObjectSerializer::toFormValue($form2);
1110+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('form2', $form2));
11111111
}
11121112
// form params
11131113
if ($form3 !== null) {
1114-
$formParams['form3'] = ObjectSerializer::toFormValue($form3);
1114+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('form3', $form3));
11151115
}
11161116
// form params
11171117
if ($form4 !== null) {
1118-
$formParams['form4'] = ObjectSerializer::toFormValue($form4);
1118+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('form4', $form4));
11191119
}
11201120
// form params
11211121
if ($id !== null) {
1122-
$formParams['id'] = ObjectSerializer::toFormValue($id);
1122+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('id', $id));
11231123
}
11241124
// form params
11251125
if ($name !== null) {
1126-
$formParams['name'] = ObjectSerializer::toFormValue($name);
1126+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('name', $name));
11271127
}
11281128

11291129
$headers = $this->headerSelector->selectHeaders(

samples/client/echo_api/php-nextgen-streaming/src/ObjectSerializer.php

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
namespace OpenAPI\Client;
3030

31+
use ArrayAccess;
3132
use DateTimeInterface;
3233
use DateTime;
3334
use GuzzleHttp\Psr7\Utils;
@@ -326,20 +327,33 @@ public static function toHeaderValue(string $value): string
326327
}
327328

328329
/**
329-
* Take value and turn it into a string suitable for inclusion in
330+
* Take value and turn it into an array suitable for inclusion in
330331
* the http body (form parameter). If it's a string, pass through unchanged
331332
* If it's a datetime object, format it in ISO8601
332333
*
333-
* @param string|\SplFileObject $value the value of the form parameter
334+
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
334335
*
335-
* @return string the form string
336+
* @return array [key => value] of formdata
336337
*/
337-
public static function toFormValue(string|\SplFileObject $value): string
338-
{
338+
public static function toFormValue(
339+
string $key,
340+
string|bool|array|DateTime|ArrayAccess|\SplFileObject $value,
341+
): array {
339342
if ($value instanceof \SplFileObject) {
340-
return $value->getRealPath();
343+
return [$key => $value->getRealPath()];
344+
} elseif (is_array($value) || $value instanceof ArrayAccess) {
345+
$flattened = [];
346+
$result = [];
347+
348+
self::flattenArray(json_decode(json_encode($value), true), $flattened);
349+
350+
foreach ($flattened as $k => $v) {
351+
$result["{$key}{$k}"] = self::toString($v);
352+
}
353+
354+
return $result;
341355
} else {
342-
return self::toString($value);
356+
return [$key => self::toString($value)];
343357
}
344358
}
345359

@@ -608,4 +622,58 @@ public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986):
608622

609623
return $qs ? (string) substr($qs, 0, -1) : '';
610624
}
625+
626+
/**
627+
* Flattens an array of Model object and generates an array compatible
628+
* with formdata - a single-level array where the keys use bracket
629+
* notation to signify nested data.
630+
*
631+
* credit: https://github.com/FranBar1966/FlatPHP
632+
*/
633+
private static function flattenArray(
634+
ArrayAccess|array $source,
635+
array &$destination,
636+
string $start = '',
637+
) {
638+
$opt = [
639+
'prefix' => '[',
640+
'suffix' => ']',
641+
'suffix-end' => true,
642+
'prefix-list' => '[',
643+
'suffix-list' => ']',
644+
'suffix-list-end' => true,
645+
];
646+
647+
if (!is_array($source)) {
648+
$source = (array) $source;
649+
}
650+
651+
if (array_is_list($source)) {
652+
$currentPrefix = $opt['prefix-list'];
653+
$currentSuffix = $opt['suffix-list'];
654+
$currentSuffixEnd = $opt['suffix-list-end'];
655+
} else {
656+
$currentPrefix = $opt['prefix'];
657+
$currentSuffix = $opt['suffix'];
658+
$currentSuffixEnd = $opt['suffix-end'];
659+
}
660+
661+
$currentName = $start;
662+
663+
foreach ($source as $key => $val) {
664+
$currentName .= $currentPrefix.$key;
665+
666+
if (is_array($val) && !empty($val)) {
667+
$currentName .= "{$currentSuffix}";
668+
self::flattenArray($val, $destination, $currentName);
669+
} else {
670+
if ($currentSuffixEnd) {
671+
$currentName .= $currentSuffix;
672+
}
673+
$destination[$currentName] = self::toString($val);
674+
}
675+
676+
$currentName = $start;
677+
}
678+
}
611679
}

samples/client/echo_api/php-nextgen/src/Api/BodyApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ public function testBodyMultipartFormdataArrayOfBinaryRequest(
10341034
$formParams['files'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
10351035
? $paramFile
10361036
: \GuzzleHttp\Psr7\Utils::tryFopen(
1037-
ObjectSerializer::toFormValue($paramFile),
1037+
ObjectSerializer::toFormValue('files', $paramFile)['files'],
10381038
'rb'
10391039
);
10401040
}
@@ -1357,7 +1357,7 @@ public function testBodyMultipartFormdataSingleBinaryRequest(
13571357
$formParams['my-file'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
13581358
? $paramFile
13591359
: \GuzzleHttp\Psr7\Utils::tryFopen(
1360-
ObjectSerializer::toFormValue($paramFile),
1360+
ObjectSerializer::toFormValue('my-file', $paramFile)['my-file'],
13611361
'rb'
13621362
);
13631363
}

0 commit comments

Comments
 (0)