Skip to content

Commit

Permalink
Fix the UploadedFiles handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Jun 5, 2019
1 parent a0cd0b7 commit aff334d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
43 changes: 26 additions & 17 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,11 @@ private function captureRequestBody(ServerRequestInterface $serverRequest)
return null;
}

$requestData = $serverRequest->getParsedBody();
$requestData = \is_array($requestData) ? $requestData : [];

foreach ($serverRequest->getUploadedFiles() as $fieldName => $uploadedFiles) {
if (!\is_array($uploadedFiles)) {
$uploadedFiles = [$uploadedFiles];
}

/** @var UploadedFileInterface $uploadedFile */
foreach ($uploadedFiles as $uploadedFile) {
$requestData[$fieldName][] = [
'client_filename' => $uploadedFile->getClientFilename(),
'client_media_type' => $uploadedFile->getClientMediaType(),
'size' => $uploadedFile->getSize(),
];
}
}
$parsedBody = $serverRequest->getParsedBody();
$requestData = array_merge(
$this->parseUploadedFiles($serverRequest->getUploadedFiles()),
\is_array($parsedBody) ? $parsedBody : []
);

if (!empty($requestData)) {
return $requestData;
Expand All @@ -196,4 +184,25 @@ private function captureRequestBody(ServerRequestInterface $serverRequest)

return $requestBody->getContents();
}

private function parseUploadedFiles(array $uploadedFiles): array
{
$data = [];

foreach ($uploadedFiles as $key => $item) {
if ($item instanceof UploadedFileInterface) {
$data[$key] = [
'client_filename' => $item->getClientFilename(),
'client_media_type' => $item->getClientMediaType(),
'size' => $item->getSize(),
];
} elseif (\is_array($item)) {
$data[$key] = $this->parseUploadedFiles($item);
} else {
throw new \InvalidArgumentException('Unrecognized UploadedFiles item, got ' . \gettype($item));
}
}

return $data;
}
}
8 changes: 3 additions & 5 deletions tests/Integration/RequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ public function applyToEventDataProvider(): \Generator
],
'data' => [
'foo' => [
[
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
],
],
Expand Down

0 comments on commit aff334d

Please sign in to comment.