Skip to content

Commit cd93a34

Browse files
committed
Adds explanatino for ::flatten_array(); optimized array_is_list pollyfill
1 parent 81cb186 commit cd93a34

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,16 @@ class ObjectSerializer
622622
}
623623

624624
/**
625+
* Flattens an array of Model object and generates an array compatible
626+
* with formdata - a single-level array where the keys use bracket
627+
* notation to signify nested data.
628+
*
629+
* @param \ArrayAccess|array $source
630+
*
625631
* credit: https://github.com/FranBar1966/FlatPHP
626632
*/
627633
private static function flatten_array(
628-
array $source,
634+
mixed $source,
629635
array &$destination,
630636
string $start = '',
631637
) {
@@ -638,11 +644,28 @@ class ObjectSerializer
638644
'suffix-list-end' => true,
639645
];
640646
641-
// array_is_list only in PHP >= 8.1
647+
if (!is_array($source)) {
648+
$source = (array) $source;
649+
}
650+
651+
/**
652+
* array_is_list only in PHP >= 8.1
653+
*
654+
* credit: https://www.php.net/manual/en/function.array-is-list.php#127044
655+
*/
642656
if (!function_exists('array_is_list')) {
643-
function array_is_list(array $arr)
657+
function array_is_list(array $array)
644658
{
645-
return $arr === [] || (array_keys($arr) === range(0, count($arr) - 1));
659+
$i = -1;
660+
661+
foreach ($array as $k => $v) {
662+
++$i;
663+
if ($k !== $i) {
664+
return false;
665+
}
666+
}
667+
668+
return true;
646669
}
647670
}
648671

0 commit comments

Comments
 (0)