|
28 | 28 |
|
29 | 29 | namespace OpenAPI\Client; |
30 | 30 |
|
| 31 | +use ArrayAccess; |
31 | 32 | use DateTimeInterface; |
32 | 33 | use DateTime; |
33 | 34 | use GuzzleHttp\Psr7\Utils; |
@@ -326,20 +327,33 @@ public static function toHeaderValue(string $value): string |
326 | 327 | } |
327 | 328 |
|
328 | 329 | /** |
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 |
330 | 331 | * the http body (form parameter). If it's a string, pass through unchanged |
331 | 332 | * If it's a datetime object, format it in ISO8601 |
332 | 333 | * |
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 |
334 | 335 | * |
335 | | - * @return string the form string |
| 336 | + * @return array [key => value] of formdata |
336 | 337 | */ |
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 { |
339 | 342 | 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; |
341 | 355 | } else { |
342 | | - return self::toString($value); |
| 356 | + return [$key => self::toString($value)]; |
343 | 357 | } |
344 | 358 | } |
345 | 359 |
|
@@ -608,4 +622,58 @@ public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): |
608 | 622 |
|
609 | 623 | return $qs ? (string) substr($qs, 0, -1) : ''; |
610 | 624 | } |
| 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 | + } |
611 | 679 | } |
0 commit comments