diff --git a/CHANGELOG.md b/CHANGELOG.md index ad6913a7..77205957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `Slytherin` will be documented in this file. +## [0.9.1](https://github.com/rougin/slytherin/compare/v0.9.0...v0.9.1) - 2017-07-21 + +### Fixed +- Retrieving a single uploaded file in `ServerRequest::getUploadedFiles` + ## [0.9.0](https://github.com/rougin/slytherin/compare/v0.8.0...v0.9.0) - 2017-07-08 **NOTE**: This release may break your application if upgrading from `v0.8.0` release. diff --git a/src/Http/ServerRequest.php b/src/Http/ServerRequest.php index 6fff522d..9bdc83ac 100644 --- a/src/Http/ServerRequest.php +++ b/src/Http/ServerRequest.php @@ -249,6 +249,25 @@ public function withoutAttribute($name) return $new; } + /** + * Updates the contents into array for single uploaded file. + * + * @param array $file + * @return array + */ + public function arrayify(array $file) + { + if (! is_array($file['name'])) { + $file['tmp_name'] = array($file['tmp_name']); + $file['size'] = array($file['size']); + $file['error'] = array($file['error']); + $file['name'] = array($file['name']); + $file['type'] = array($file['type']); + } + + return $file; + } + /** * Creates a new \Psr\Http\Message\UploadedFile instance. * @@ -277,6 +296,8 @@ protected function uploaded(array $uploaded) $files = array(); foreach ($uploaded as $name => $file) { + $file = $this->arrayify($file); + list($count, $files[$name]) = array(count($file['name']), array()); for ($i = 0; $i < $count; $i++) { diff --git a/tests/Http/ServerRequestTest.php b/tests/Http/ServerRequestTest.php index 4b71e598..8835c9a0 100644 --- a/tests/Http/ServerRequestTest.php +++ b/tests/Http/ServerRequestTest.php @@ -125,6 +125,39 @@ public function testUploadedFiles() $this->assertEquals($expected, $request->getUploadedFiles()); } + /** + * Tests getUploadedFiles() and a single uploaded file. + * + * @return void + */ + public function testGetUploadedFilesWithSingleUploadedFile() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '/'; + $_SERVER['SERVER_NAME'] = 'localhost'; + $_SERVER['SERVER_PORT'] = '8000'; + + $uploaded = array('file' => array()); + + $uploaded['file']['error'] = 0; + $uploaded['file']['name'] = 'test.txt'; + $uploaded['file']['size'] = 617369; + $uploaded['file']['tmp_name'] = '/tmp/test.txt'; + $uploaded['file']['type'] = 'application/pdf'; + + $request = new ServerRequest($_SERVER, array(), array(), $uploaded); + + $error = $uploaded['file']['error']; + $name = $uploaded['file']['name']; + $size = $uploaded['file']['size']; + $file = $uploaded['file']['tmp_name']; + $type = $uploaded['file']['type']; + + $expected = array('file' => array(new UploadedFile($file, $size, $error, $name, $type))); + + $this->assertEquals($expected, $request->getUploadedFiles()); + } + /** * Tests getAttribute(), getAttributes(), withAttribute() and withoutAttribute(). *