Skip to content

Commit

Permalink
Merge pull request #48 from chadicus/bug-47
Browse files Browse the repository at this point in the history
Bug 47
  • Loading branch information
chadicus authored Aug 29, 2018
2 parents 96c873d + 6292f8e commit 4c2bb3a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/RequestBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Chadicus\Slim\OAuth2\Http;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use OAuth2;

/**
Expand Down Expand Up @@ -69,23 +70,36 @@ private static function cleanupHeaders(array $uncleanHeaders = [])
/**
* Convert a PSR-7 uploaded files structure to a $_FILES structure.
*
* @param \Psr\Http\Message\UploadedFileInterface[] $uploadedFiles Array of file objects.
* @param array $uploadedFiles Array of file objects.
*
* @return array
*/
private static function convertUploadedFiles(array $uploadedFiles)
{
$files = [];
foreach ($uploadedFiles as $name => $upload) {
$files[$name] = [
'name' => $upload->getClientFilename(),
'type' => $upload->getClientMediaType(),
'size' => $upload->getSize(),
'tmp_name' => $upload->getStream()->getMetadata('uri'),
'error' => $upload->getError(),
];
foreach ($uploadedFiles as $name => $uploadedFile) {
if (!is_array($uploadedFile)) {
$files[$name] = self::convertUploadedFile($uploadedFile);
continue;
}

$files[$name] = [];
foreach ($uploadedFile as $file) {
$files[$name][] = self::convertUploadedFile($file);
}
}

return $files;
}

private static function convertUploadedFile(UploadedFileInterface $uploadedFile)
{
return [
'name' => $uploadedFile->getClientFilename(),
'type' => $uploadedFile->getClientMediaType(),
'size' => $uploadedFile->getSize(),
'tmp_name' => $uploadedFile->getStream()->getMetadata('uri'),
'error' => $uploadedFile->getError(),
];
}
}
53 changes: 53 additions & 0 deletions tests/RequestBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,57 @@ public function toOAuth2BodyContentsOfRequestPreserved()
$this->assertSame('foo', $psr7Request->getBody()->getContents());
$this->assertSame('foo', $oauth2Request->getContent());
}

/**
* @test
* @covers ::toOAuth2
*
* @return void
*/
public function toOAuth2WithMultipleFiles()
{
$files = [
'multi' => [
new UploadedFile(
__FILE__,
100,
UPLOAD_ERR_OK,
'foo1.txt',
'text/plain'
),
new UploadedFile(
__FILE__,
100,
UPLOAD_ERR_OK,
'foo2.txt',
'text/plain'
),
],
];

$psr7Request = (new ServerRequest())->withUploadedFiles($files);
$oauth2Request = RequestBridge::toOauth2($psr7Request);

$this->assertSame(
[
'multi' => [
[
'name' => 'foo1.txt',
'type' => 'text/plain',
'size' => 100,
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK,
],
[
'name' => 'foo2.txt',
'type' => 'text/plain',
'size' => 100,
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK,
],
],
],
$oauth2Request->files
);
}
}

0 comments on commit 4c2bb3a

Please sign in to comment.