Skip to content
Permalink
Browse files

Add accessor for reading individual files.

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 96630e240a7b0133c443b3899c7636efb1102855
Showing with 48 additions and 0 deletions.
  1. +16 −0 src/Network/Request.php
  2. +32 −0 tests/TestCase/Network/RequestTest.php
@@ -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.
*
@@ -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
*

0 comments on commit 96630e2

Please sign in to comment.
You can’t perform that action at this time.