|
19 | 19 |
|
20 | 20 | namespace {{invokerPackage}};
|
21 | 21 |
|
| 22 | +use ArrayAccess; |
22 | 23 | use GuzzleHttp\Psr7\Utils;
|
23 | 24 | use {{modelPackage}}\ModelInterface;
|
24 | 25 |
|
@@ -315,20 +316,31 @@ class ObjectSerializer
|
315 | 316 | }
|
316 | 317 |
|
317 | 318 | /**
|
318 |
| - * Take value and turn it into a string suitable for inclusion in |
| 319 | + * Take value and turn it into an array suitable for inclusion in |
319 | 320 | * the http body (form parameter). If it's a string, pass through unchanged
|
320 | 321 | * If it's a datetime object, format it in ISO8601
|
321 | 322 | *
|
322 |
| - * @param string|\SplFileObject $value the value of the form parameter |
| 323 | + * @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter |
323 | 324 | *
|
324 |
| - * @return string the form string |
| 325 | + * @return array [key => value] of formdata |
325 | 326 | */
|
326 |
| - public static function toFormValue($value) |
| 327 | + public static function toFormValue(string $key, mixed $value) |
327 | 328 | {
|
328 | 329 | if ($value instanceof \SplFileObject) {
|
329 |
| - return $value->getRealPath(); |
| 330 | + return [$key => $value->getRealPath()]; |
| 331 | + } elseif (is_array($value) || $value instanceof ArrayAccess) { |
| 332 | + $flattened = []; |
| 333 | + $result = []; |
| 334 | +
|
| 335 | + self::flattenArray(json_decode(json_encode($value), true), $flattened); |
| 336 | +
|
| 337 | + foreach ($flattened as $k => $v) { |
| 338 | + $result["{$key}{$k}"] = self::toString($v); |
| 339 | + } |
| 340 | +
|
| 341 | + return $result; |
330 | 342 | } else {
|
331 |
| - return self::toString($value); |
| 343 | + return [$key => self::toString($value)]; |
332 | 344 | }
|
333 | 345 | }
|
334 | 346 |
|
@@ -605,4 +617,81 @@ class ObjectSerializer
|
605 | 617 |
|
606 | 618 | return $qs ? (string) substr($qs, 0, -1) : '';
|
607 | 619 | }
|
| 620 | + |
| 621 | + /** |
| 622 | + * Flattens an array of Model object and generates an array compatible |
| 623 | + * with formdata - a single-level array where the keys use bracket |
| 624 | + * notation to signify nested data. |
| 625 | + * |
| 626 | + * @param \ArrayAccess|array $source |
| 627 | + * |
| 628 | + * credit: https://github.com/FranBar1966/FlatPHP |
| 629 | + */ |
| 630 | + private static function flattenArray( |
| 631 | + mixed $source, |
| 632 | + array &$destination, |
| 633 | + string $start = '', |
| 634 | + ) { |
| 635 | + $opt = [ |
| 636 | + 'prefix' => '[', |
| 637 | + 'suffix' => ']', |
| 638 | + 'suffix-end' => true, |
| 639 | + 'prefix-list' => '[', |
| 640 | + 'suffix-list' => ']', |
| 641 | + 'suffix-list-end' => true, |
| 642 | + ]; |
| 643 | +
|
| 644 | + if (!is_array($source)) { |
| 645 | + $source = (array) $source; |
| 646 | + } |
| 647 | + |
| 648 | + /** |
| 649 | + * array_is_list only in PHP >= 8.1 |
| 650 | + * |
| 651 | + * credit: https://www.php.net/manual/en/function.array-is-list.php#127044 |
| 652 | + */ |
| 653 | + if (!function_exists('array_is_list')) { |
| 654 | + function array_is_list(array $array) |
| 655 | + { |
| 656 | + $i = -1; |
| 657 | +
|
| 658 | + foreach ($array as $k => $v) { |
| 659 | + ++$i; |
| 660 | + if ($k !== $i) { |
| 661 | + return false; |
| 662 | + } |
| 663 | + } |
| 664 | + |
| 665 | + return true; |
| 666 | + } |
| 667 | + } |
| 668 | + |
| 669 | + if (array_is_list($source)) { |
| 670 | + $currentPrefix = $opt['prefix-list']; |
| 671 | + $currentSuffix = $opt['suffix-list']; |
| 672 | + $currentSuffixEnd = $opt['suffix-list-end']; |
| 673 | + } else { |
| 674 | + $currentPrefix = $opt['prefix']; |
| 675 | + $currentSuffix = $opt['suffix']; |
| 676 | + $currentSuffixEnd = $opt['suffix-end']; |
| 677 | + } |
| 678 | + |
| 679 | + $currentName = $start; |
| 680 | + |
| 681 | + foreach ($source as $key => $val) { |
| 682 | + $currentName .= $currentPrefix.$key; |
| 683 | +
|
| 684 | + if (is_array($val) && !empty($val)) { |
| 685 | + $currentName .= "{$currentSuffix}"; |
| 686 | + self::flattenArray($val, $destination, $currentName); |
| 687 | + } else { |
| 688 | + if ($currentSuffixEnd) { |
| 689 | + $currentName .= $currentSuffix; |
| 690 | + } |
| 691 | + $destination[$currentName] = self::toString($val); |
| 692 | + } |
| 693 | + |
| 694 | + $currentName = $start; |
| 695 | + } |
| 696 | + } |
608 | 697 | }
|
0 commit comments