diff --git a/docs/book/file.md b/docs/book/file.md index aa160a3f..25b627d9 100644 --- a/docs/book/file.md +++ b/docs/book/file.md @@ -241,7 +241,7 @@ The following set of options are supported: Required when passing a [PSR-7 UploadedFileInterface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface) to the filter; used to create a new stream representing the renamed file. (Since 2.9.0) -- `upload_file_factory` (`Psr\Http\Message\UploadedFileFactoryInterface`; default: +- `uploaded_file_factory` (`Psr\Http\Message\UploadedFileFactoryInterface`; default: `null`): Required when passing a [PSR-7 UploadedFileInterface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface) to the filter; used to create a new uploaded file representation of the renamed file. (Since 2.9.0) @@ -328,7 +328,7 @@ $filter = new \Zend\Filter\File\RenameUpload([ // @var StreamFactoryInterface $streamFactory 'stream_factory' => $streamFactory, // @var UploadedFileFactoryInterface $uploadedFileFactory - 'upload_file_factory' => $uploadedFileFactory, + 'uploaded_file_factory' => $uploadedFileFactory, ]); // @var ServerRequestInterface $request @@ -356,6 +356,23 @@ foreach ($request->getUploadedFiles() as $uploadedFile) { > provides a PSR-17 implementation, but requires PHP 7.1. If you are still on > PHP 7.0, either upgrade, or find a compatible psr/http-factory-implementation. +If already using `zend-diactoros` that comes with expressive, instances for `$streamFactory` and `$uploadedFileFactory` +can be: +```php +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Zend\Diactoros\StreamFactory; +use Zend\Diactoros\UploadedFileFactory; +use Zend\Filter\File\RenameUpload; + +$filter = new \Zend\Filter\File\RenameUpload([ + 'target' => './data/uploads/', + 'randomize' => true, + 'stream_factory' => new StreamFactory(), + 'uploaded_file_factory' => new UploadedFileFactory(), +]); +``` + ## Uppercase `Zend\Filter\File\Uppercase` can be used to convert all file contents to diff --git a/src/File/RenameUpload.php b/src/File/RenameUpload.php index 6ed19cc4..db2fee18 100644 --- a/src/File/RenameUpload.php +++ b/src/File/RenameUpload.php @@ -26,7 +26,7 @@ class RenameUpload extends AbstractFilter 'overwrite' => false, 'randomize' => false, 'stream_factory' => null, - 'upload_file_factory' => null, + 'uploaded_file_factory' => null, ]; /** @@ -99,18 +99,18 @@ public function getTarget() * filtered PSR-7 UploadedFileInterface instances. * @return self */ - public function setUploadFileFactory(UploadedFileFactoryInterface $factory) + public function setUploadedFileFactory(UploadedFileFactoryInterface $factory) { - $this->options['upload_file_factory'] = $factory; + $this->options['uploaded_file_factory'] = $factory; return $this; } /** * @return null|UploadedFileFactoryInterface */ - public function getUploadFileFactory() + public function getUploadedFileFactory() { - return $this->options['upload_file_factory']; + return $this->options['uploaded_file_factory']; } /** @@ -421,10 +421,10 @@ private function filterPsr7UploadedFile(UploadedFileInterface $uploadedFile) $stream = $streamFactory->createStreamFromFile($targetFile); - $uploadedFileFactory = $this->getUploadFileFactory(); + $uploadedFileFactory = $this->getUploadedFileFactory(); if (! $uploadedFileFactory) { throw new Exception\RuntimeException(sprintf( - 'No PSR-17 %s present; cannot filter file. Please pass the upload_file_factory' + 'No PSR-17 %s present; cannot filter file. Please pass the uploaded_file_factory' . ' option with a %s instance when creating the filter for use with PSR-7.', UploadedFileFactoryInterface::class, UploadedFileFactoryInterface::class diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index a4eb5e13..69c061d2 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -118,6 +118,7 @@ public function testThrowsExceptionWithNonUploadedFile() } /** + * * @requires PHP 7 * @return void */ public function testOptions() @@ -133,11 +134,13 @@ public function testOptions() 'use_upload_name' => true, 'overwrite' => true, 'randomize' => true, + 'uploaded_file_factory' => $this->prophesize(UploadedFileFactoryInterface::class)->reveal() ]); $this->assertEquals($this->sourceFile, $filter->getTarget()); $this->assertTrue($filter->getUseUploadName()); $this->assertTrue($filter->getOverwrite()); $this->assertTrue($filter->getRandomize()); + $this->assertInstanceOf(UploadedFileFactoryInterface::class, $filter->getUploadedFileFactory()); } /** @@ -218,7 +221,7 @@ public function testStringConstructorWithPsrFile() $this->assertEquals($targetFile, $filter->getTarget()); $filter->setStreamFactory($streamFactory->reveal()); - $filter->setUploadFileFactory($fileFactory->reveal()); + $filter->setUploadedFileFactory($fileFactory->reveal()); $moved = $filter($originalFile->reveal());