8000 Add PSR-7 support for FileValidator by wouterj · Pull Request #15421 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add PSR-7 support for FileValidator #15421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 47 additions & 5 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Psr\Http\Message\UploadedFileInterface;
use Symfony\Component\HttpFoundation\File\File as FileObject;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Expand Down Expand Up @@ -51,10 +52,18 @@ public function validate($value, Constraint $constraint)
return;
}

if ($value instanceof UploadedFile && !$value->isValid()) {
switch ($value->getError()) {
$error = null;
if (
($value instanceof UploadedFile && !$value->isValid())
|| ($value instanceof UploadedFileInterface && UPLOAD_ERR_OK !== $value->getError())
) {
$error = $value->getError();
}

if (null !== $error) {
switch ($error) {
case UPLOAD_ERR_INI_SIZE:
$iniLimitSize = UploadedFile::getMaxFilesize();
$iniLimitSize = self::getMaxFilesize();
if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
$limitInBytes = $constraint->maxSize;
$binaryFormat = $constraint->binaryFormat;
Expand Down Expand Up @@ -154,18 +163,24 @@ public function validate($value, Constraint $constraint)
default:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadErrorMessage)
->setCode($value->getError())
->setCode($error)
->addViolation();
} else {
$this->buildViolation($constraint->uploadErrorMessage)
->setCode($value->getError())
->setCode($error)
->addViolation();
}

return;
}
}

if ($value instanceof UploadedFileInterface) {
// PSR-7 UploadedFileInterface does not represent a file
// in the filesystem.
return;
}

if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
Expand Down Expand Up @@ -292,6 +307,33 @@ private static function moreDecimalsThan($double, $numberOfDecimals)
return strlen((string) $double) > strlen(round($double, $numberOfDecimals));
}

private static function getMaxFilesize()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is equal to HttpFoundation\File\UploadedFile::getMaxFilesize(). It exists here in order to remove the dependency on HttpFoundation for creating the error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we still have a dependency on the component, so what's the advantage?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind - it's a dev dependency. I'm with you :)

{
$iniMax = strtolower(ini_get('upload_max_filesize'));

if ('' === $iniMax) {
return PHP_INT_MAX;
}

$max = ltrim($iniMax, '+');
if (0 === strpos($max, '0x')) {
$max = intval($max, 16);
} elseif (0 === strpos($max, '0')) {
$max = intval($max, 8);
} else {
$max = (int) $max;
}

switch (substr($iniMax, -1)) {
case 't': $max *= 1024;
case 'g': $max *= 1024;
case 'm': $max *= 1024;
case 'k': $max *= 1024;
}

return $max;
}

/**
* Convert the limit to the smallest possible number
* (i.e. try "MB", then "kB", then "bytes").
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\FileValidator;

class FileValidatorPsr7Test extends AbstractConstraintValidatorTest
{
protected function createValidator()
{
return new FileValidator();
}

public function testValidUploadedFile()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not putting them in the FileValidatorTest class directly ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not one FileValidatorTest, there are 2: FileValidatorObjectTest, FileValidatorPathTest, both extending FileValidatorTest. It makes no sense to put it in FileValidatorTest, as it would be run twice with exact the same input/output.

{
$file = $this->getMock('Psr\Http\Message\UploadedFileInterface');
$file->expects($this->any())->method('getError')->willReturn(UPLOAD_ERR_OK);

$this->validator->validate($file, new File());

$this->assertNoViolation();
}

public function testInvalidUploadedFile()
{
$file = $this->getMock('Psr\Http\Message\UploadedFileInterface');
$file->expects($this->any())->method('getError')->willReturn(UPLOAD_ERR_FORM_SIZE);

$constraint = new File(array(
'uploadFormSizeErrorMessage' => 'myMessage',
));

$this->validator->validate($file, $constraint);

$this->buildViolation('myMessage')
->setCode(UPLOAD_ERR_FORM_SIZE)
->assertRaised();
}
}
6 changes: 4 additions & 2 deletions src/Symfony/Component/Validator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"symfony/expression-language": "~2.4|~3.0.0",
"doctrine/annotations": "~1.0",
"doctrine/cache": "~1.0",
"egulias/email-validator": "~1.2,>=1.2.1"
"egulias/email-validator": "~1.2,>=1.2.1",
"psr/http-message": "~1.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this go into suggest too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be here because we reference the Psr UploadedFileInterface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now also added it to the suggests section as well.

},
"suggest": {
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
Expand All @@ -40,7 +41,8 @@
"symfony/config": "",
"egulias/email-validator": "Strict (RFC compliant) email validation",
"symfony/property-access": "For using the 2.4 Validator API",
"symfony/expression-language": "For using the 2.4 Expression validator"
"symfony/expression-language": "For using the 2.4 Expression validator",
"psr/http-message": "For validating PSR-7 UploadedFileInterface implementations"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Validator\\": "" }
Expand Down
0