Skip to content

Commit

Permalink
Support indexed keys of uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Dec 9, 2024
1 parent e0dfd99 commit 64b3648
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FileUploadNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
public function normalize(array $files): array
{
$standardizedPerKey = [];
$groups = [];

foreach ($files as $k => $file) {
switch (true) {
Expand All @@ -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;
Expand Down

0 comments on commit 64b3648

Please sign in to comment.