From 64b36489ee17a59d96b80578ebca62b9e2d513d2 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Mon, 9 Dec 2024 14:01:09 +0100 Subject: [PATCH] Support indexed keys of uploads --- src/FileUploadNormalizer.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/FileUploadNormalizer.php b/src/FileUploadNormalizer.php index 9579ef6..6ef0d5f 100644 --- a/src/FileUploadNormalizer.php +++ b/src/FileUploadNormalizer.php @@ -33,6 +33,7 @@ public function __construct( public function normalize(array $files): array { $standardizedPerKey = []; + $groups = []; foreach ($files as $k => $file) { switch (true) { @@ -56,6 +57,25 @@ public function normalize(array $files): array $standardizedPerKey[$k][] = $this->fromFile($filePath); break; } + + if (str_ends_with($k, '_0')) { + $groups[] = substr($k, 0, -2); + } + } + + foreach ($groups as $group) { + if (array_key_exists($group, $standardizedPerKey)) { + continue; + } + + $standardizedPerKey[$group] = []; + + foreach ($standardizedPerKey as $k => $file) { + if (1 === \count($file) && preg_match('/^'.preg_quote($group, '/').'_\d+$/', $k)) { + $standardizedPerKey[$group][] = reset($file); + unset($standardizedPerKey[$k]); + } + } } return $standardizedPerKey;