Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

PSR-7 file upload parameter naming consistency. #82

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions docs/book/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
alextech marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
14 changes: 7 additions & 7 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RenameUpload extends AbstractFilter
'overwrite' => false,
'randomize' => false,
'stream_factory' => null,
'upload_file_factory' => null,
'uploaded_file_factory' => null,
];

/**
Expand Down Expand Up @@ -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;
alextech marked this conversation as resolved.
Show resolved Hide resolved
return $this;
}

/**
* @return null|UploadedFileFactoryInterface
*/
public function getUploadFileFactory()
public function getUploadedFileFactory()
{
return $this->options['upload_file_factory'];
return $this->options['uploaded_file_factory'];
}

/**
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function testThrowsExceptionWithNonUploadedFile()
}

/**
* * @requires PHP 7
* @return void
*/
public function testOptions()
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());

Expand Down