Skip to content

Commit 87677af

Browse files
committed
Simplified tree building for uploaded files
1 parent c9580e7 commit 87677af

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

src/ServerData.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(array $params = [])
3737
$this->get = $params['get'] ?? [];
3838
$this->post = $params['post'] ?? [];
3939
$this->cookie = $params['cookie'] ?? [];
40-
$this->files = $params['files'] ?? [];
40+
$this->files = array_map(fn ($value) => $this->resolveFileTree($value), $params['files'] ?? []);
4141
}
4242

4343
/**
@@ -82,7 +82,7 @@ public function params(): array
8282
'cookie' => $this->cookie,
8383
'query' => $this->get,
8484
'parsedBody' => $this->post,
85-
'files' => $this->normalizeFiles($this->files),
85+
'files' => $this->uploadedFiles(),
8686
'version' => $this->protocolVersion()
8787
];
8888
}
@@ -92,7 +92,7 @@ public function params(): array
9292
*/
9393
public function uploadedFiles(): array
9494
{
95-
return $this->normalizeFiles($this->files);
95+
return $this->files;
9696
}
9797

9898
private function protocolVersion(): string
@@ -162,20 +162,10 @@ private function authorizationHeader(): ?string
162162
return $headers['Authorization'] ?? $headers['authorization'] ?? null;
163163
}
164164

165-
private function normalizeFiles(array $files): array
166-
{
167-
$normalizedFiles = [];
168-
foreach ($files as $key => $value) {
169-
$normalizedFiles[$key] = ($value instanceof UploadedFileInterface)
170-
? $value
171-
: $this->resolveFileTree($value);
172-
}
173-
174-
return $normalizedFiles;
175-
}
176-
177165
private function resolveFileTree($value)
178166
{
167+
if ($value instanceof UploadedFileInterface) { return $value; }
168+
179169
if (!is_array($value)) {
180170
throw new InvalidArgumentException('Invalid file data structure');
181171
}

tests/ServerDataTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ public function testMixedStructureUploadedFiles()
178178

179179
public function testInvalidFileDataStructure_ThrowsException()
180180
{
181-
$server = $this->serverData(['files' => ['field' => 'filename.txt']]);
182181
$this->expectException(InvalidArgumentException::class);
183-
$server->params();
182+
$this->serverData(['files' => ['field' => 'filename.txt']]);
184183
}
185184

186185
private function serverData(array $data = []): ServerData

0 commit comments

Comments
 (0)