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 2 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
16 changes: 14 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,18 @@ 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 Zend\Diactoros\StreamFactory;
alextech marked this conversation as resolved.
Show resolved Hide resolved
use Zend\Diactoros\UploadedFileFactory;

[
'stream_factory' => new StreamFactory(),
'upload_file_factory' => new UploadedFileFactory(),
alextech marked this conversation as resolved.
Show resolved Hide resolved
]
```

## Uppercase

`Zend\Filter\File\Uppercase` can be used to convert all file contents to
Expand Down
8 changes: 4 additions & 4 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 @@ -101,7 +101,7 @@ public function getTarget()
*/
public function setUploadFileFactory(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;
}

Expand All @@ -110,7 +110,7 @@ public function setUploadFileFactory(UploadedFileFactoryInterface $factory)
*/
public function getUploadFileFactory()
{
return $this->options['upload_file_factory'];
return $this->options['uploaded_file_factory'];
}

/**
Expand Down Expand Up @@ -424,7 +424,7 @@ private function filterPsr7UploadedFile(UploadedFileInterface $uploadedFile)
$uploadedFileFactory = $this->getUploadFileFactory();
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
4 changes: 4 additions & 0 deletions test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ public function testOptions()
'use_upload_name' => true,
'overwrite' => true,
'randomize' => true,
'stream_factory' => $this->prophesize(StreamFactoryInterface::class)->reveal(),
alextech marked this conversation as resolved.
Show resolved Hide resolved
'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(StreamFactoryInterface::class, $filter->getStreamFactory());
$this->assertInstanceOf(UploadedFileFactoryInterface::class, $filter->getUploadFileFactory());
}

/**
Expand Down