Skip to content

Commit f413a41

Browse files
committed
Revert "[PHP] - Add FormDataProcessor to handle nested ModelInterface data (OpenAPITools#20990)"
This reverts commit 3462310.
1 parent fffc322 commit f413a41

71 files changed

Lines changed: 1363 additions & 4747 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public void processOpts() {
117117

118118
supportingFiles.add(new SupportingFile("ApiException.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiException.php"));
119119
supportingFiles.add(new SupportingFile("Configuration.mustache", toSrcPath(invokerPackage, srcBasePath), "Configuration.php"));
120-
supportingFiles.add(new SupportingFile("FormDataProcessor.mustache", toSrcPath(invokerPackage, srcBasePath), "FormDataProcessor.php"));
121120
supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toSrcPath(invokerPackage, srcBasePath), "ObjectSerializer.php"));
122121
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toSrcPath(modelPackage, srcBasePath), "ModelInterface.php"));
123122
supportingFiles.add(new SupportingFile("HeaderSelector.mustache", toSrcPath(invokerPackage, srcBasePath), "HeaderSelector.php"));

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public void processOpts() {
121121

122122
supportingFiles.add(new SupportingFile("ApiException.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiException.php"));
123123
supportingFiles.add(new SupportingFile("Configuration.mustache", toSrcPath(invokerPackage, srcBasePath), "Configuration.php"));
124-
supportingFiles.add(new SupportingFile("FormDataProcessor.mustache", toSrcPath(invokerPackage, srcBasePath), "FormDataProcessor.php"));
125124
supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toSrcPath(invokerPackage, srcBasePath), "ObjectSerializer.php"));
126125
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toSrcPath(modelPackage, srcBasePath), "ModelInterface.php"));
127126
supportingFiles.add(new SupportingFile("HeaderSelector.mustache", toSrcPath(invokerPackage, srcBasePath), "HeaderSelector.php"));

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

Lines changed: 0 additions & 227 deletions
This file was deleted.

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

Lines changed: 86 additions & 0 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;
@@ -315,6 +316,37 @@ class ObjectSerializer
315316
return self::toString($value);
316317
}
317318
319+
/**
320+
* Take value and turn it into an array suitable for inclusion in
321+
* the http body (form parameter). If it's a string, pass through unchanged
322+
* If it's a datetime object, format it in ISO8601
323+
*
324+
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
325+
*
326+
* @return array [key => value] of formdata
327+
*/
328+
public static function toFormValue(
329+
string $key,
330+
string|bool|array|DateTime|ArrayAccess|\SplFileObject $value,
331+
): array {
332+
if ($value instanceof \SplFileObject) {
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;
345+
} else {
346+
return [$key => self::toString($value)];
347+
}
348+
}
349+
318350
/**
319351
* Take value and turn it into a string suitable for inclusion in
320352
* the parameter. If it's a string, pass through unchanged
@@ -580,4 +612,58 @@ class ObjectSerializer
580612

581613
return $qs ? (string) substr($qs, 0, -1) : '';
582614
}
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+
}
583669
}

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use Psr\Http\Message\ResponseInterface;
3131
use {{invokerPackage}}\ApiException;
3232
use {{invokerPackage}}\Configuration;
3333
use {{invokerPackage}}\HeaderSelector;
34-
use {{invokerPackage}}\FormDataProcessor;
3534
use {{invokerPackage}}\ObjectSerializer;
3635

3736
/**
@@ -725,19 +724,25 @@ use {{invokerPackage}}\ObjectSerializer;
725724
{{/pathParams}}
726725

727726
{{#formParams}}
728-
{{#-first}}
729727
// form params
730-
$formDataProcessor = new FormDataProcessor();
731-
732-
$formData = $formDataProcessor->prepare([
733-
{{/-first}}
734-
'{{paramName}}' => ${{paramName}},
735-
{{#-last}}
736-
]);
737-
738-
$formParams = $formDataProcessor->flatten($formData);
739-
$multipart = $formDataProcessor->has_file;
740-
{{/-last}}
728+
if (${{paramName}} !== null) {
729+
{{#isFile}}
730+
$multipart = true;
731+
$formParams['{{baseName}}'] = [];
732+
$paramFiles = is_array(${{paramName}}) ? ${{paramName}} : [${{paramName}}];
733+
foreach ($paramFiles as $paramFile) {
734+
$formParams['{{baseName}}'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
735+
? $paramFile
736+
: \GuzzleHttp\Psr7\Utils::tryFopen(
737+
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
738+
'rb'
739+
);
740+
}
741+
{{/isFile}}
742+
{{^isFile}}
743+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
744+
{{/isFile}}
745+
}
741746
{{/formParams}}
742747

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

0 commit comments

Comments
 (0)