Skip to content

Commit

Permalink
Add accessor for reading individual files.
Browse files Browse the repository at this point in the history
This method is not part of the PSR7 interfaces, but I think it is
a useful addition as it makes reading files out of the request much
simpler.
  • Loading branch information
markstory committed Sep 5, 2016
1 parent 7f43e3d commit 96630e2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Network/Request.php
Expand Up @@ -1691,6 +1691,22 @@ public function getAttributes()
return $this->attributes + $emulated;
}

/**
* Get the uploaded file from a dotted path.
*
* @param string $path The dot separated path to the file you want.
* @return null|Psr\Http\Message\UploadedFileInterface
*/
public function getUploadedFile($path)
{
$file = Hash::get($this->uploadedFiles, $path);
if (!$file instanceof UploadedFile) {
return null;
}

return $file;
}

/**
* Get the array of uploaded files from the request.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -550,6 +550,38 @@ public function testWithUploadedFiles()
$this->assertSame(['picture' => $file], $new->getUploadedFiles());
}

/**
* Test getting a single file
*
* @return void
*/
public function testGetUploadedFile()
{
$file = new UploadedFile(
__FILE__,
123,
UPLOAD_ERR_OK,
'test.php',
'text/plain'
);
$request = new Request();
$new = $request->withUploadedFiles(['picture' => $file]);
$this->assertNull($new->getUploadedFile(''));
$this->assertSame($file, $new->getUploadedFile('picture'));

$new = $request->withUploadedFiles([
'pictures' => [
[
'image' => $file
]
]
]);
$this->assertNull($new->getUploadedFile('pictures'));
$this->assertNull($new->getUploadedFile('pictures.0'));
$this->assertNull($new->getUploadedFile('pictures.1'));
$this->assertSame($file, $new->getUploadedFile('pictures.0.image'));
}

/**
* Test replacing files with an invalid file
*
Expand Down

0 comments on commit 96630e2

Please sign in to comment.