-
Notifications
You must be signed in to change notification settings - Fork 136
PSR-7 file validation implemented #251
PSR-7 file validation implemented #251
Conversation
@icetee we already have support for validating PSR-7 uploaded files as of version 2.11.0; the support is even documented. |
@weierophinney |
Related to zendframework/zend-form#227 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a number of comments at this point with directions on changes necessary for us to merge.
With regards to the testing: it sounds like perhaps the PSR-7 library is not installed locally. Run composer list | grep http-message
and check to ensure it's present. That method is definitely part of the spec and the interfaces, so that's where I'd start.
src/File/Crc32.php
Outdated
$file = $value; | ||
$filename = basename($file); | ||
} | ||
extract($this->getFileInfo($value, $file)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use extract()
. Assign the return value to a variable, and pull elements out it.
src/File/ExcludeExtension.php
Outdated
$file = $value; | ||
$filename = basename($file); | ||
} | ||
extract($this->getFileInfo($value, $file)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here.
src/File/ExcludeMimeType.php
Outdated
$filename = basename($file); | ||
$filetype = null; | ||
} | ||
extract($this->getFileInfo($value, $file, true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
etc.
Please review all code for elements where you have done this and update.
src/File/ValidationPsr7Trait.php
Outdated
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the compact form of the license, please:
/**
* @see https://github.com/zendframework/zend-validator for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-validator/blob/master/LICENSE.md New BSD License
(note that since this is a new file, the copyright date is only this year.)
*/
src/File/ValidationPsr7Trait.php
Outdated
$fileInfo['file'] = $value; | ||
$fileInfo['filename'] = basename($fileInfo['file']); | ||
$fileInfo['filetype'] = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract the different cases into separate private
methods; doing so will make it easier to read the method and understand where the individual pieces come from. It will also allow you to return early, removing the elseif/else
conditions:
if (is_string($value && is_array($file)) {
return $this->getLegacyFileInfo($file, $hasType);
}
if (is_array($value)) {
return $this->getSapiFileInfo($file, $hasType);
}
if ($value instanceof UploadedFileInterface) {
return $this->getPsr7FileInfo($value, $hasType);
}
return $this->getFileBasedFileInfo($file, $hasType);
These methods can then be re-used in the getFileInfoExists()
method, allowing you to act based on the return values.
src/File/ValidationPsr7Trait.php
Outdated
$fileInfo['file'] = $file['tmp_name']; | ||
$fileInfo['filetype'] = $file['type']; | ||
|
||
$this->setValue($fileInfo['filename']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method call is a problem, because setValue()
does not exists in the current trait and that results in a false dependency.
I pushed the changes. @weierophinney I have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strategy of testing the FileInformationTrait
is great, as then the tests for the individual validators do not need to change, and we can still be assured that they will operate regardless of the file upload SAPI in place. Nice work!
It looks like the various test failures reported by Travis are not your fault; they appear to be timing issues. I'll try rebasing your PR against current develop to see if recent changes make those go away before I merge.
Fatal error: Default value for parameters with a class type hint can only be NULL
Per comments from @webimpress
Incorporates all feedback from @webimpress
Maybe related build failed - #257 |
@icetee Yes, and I just pushed a fix for that to develop. 😄 |
This commit allows validation files with
$request->getUploadedFiles()
.But I got stuck in testing. I want to be able to use a
$upload->getStream()->getMetadata('uri')
and return example/tmp/abcd
.