8000 [Validator] Added option for specifying the size format for the File validator by csarrazi · Pull Request #10917 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Added option for specifying the size format for the File validator #10917

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
Added option for specifying the size format for the file validator
This lets someone choose between binary (multiples of 1024) and decimal (multiple of 1000) formats for specifying the size
  • Loading branch information
csarrazi committed May 20, 2014
commit 0e527d56788dc753d10d25240a43415f8b1430d6
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class File extends Constraint
{
public $maxSize = null;
public $mimeTypes = array();
public $sizeFormat = FileValidator::SIZE_FORMAT_DECIMAL;
public $notFoundMessage = 'The file could not be found.';
public $notReadableMessage = 'The file is not readable.';
public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
Expand Down
22 changes: 18 additions & 4 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
*/
class FileValidator extends ConstraintValidator
{
const SIZE_FORMAT_BINARY = 'binary';
const SIZE_FORMAT_DECIMAL = 'decimal';

private $sizeFormats = array(
self::SIZE_FORMAT_BINARY => 1024,
self::SIZE_FORMAT_DECIMAL => 1000,
);

/**
* {@inheritdoc}
*/
Expand All @@ -34,16 +42,22 @@ public function validate($value, Constraint $constraint)
return;
}

if (!array_key_exists($constraint->sizeFormat, $this->sizeFormats)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid size format', $constraint->sizeFormat));
}

$formatSize = $this->sizeFormats[$constraint->sizeFormat];

if ($value instanceof UploadedFile && !$value->isValid()) {
switch ($value->getError()) {
case UPLOAD_ERR_INI_SIZE:
if ($constraint->maxSize) {
if (ctype_digit((string) $constraint->maxSize)) {
$maxSize = (int) $constraint->maxSize;
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1024;
$maxSize = $constraint->maxSize * $formatSize;
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1048576;
$maxSize = $constraint->maxSize * pow($formatSize, 2);
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
Expand Down Expand Up @@ -113,11 +127,11 @@ public function validate($value, Constraint $constraint)
$limit = (int) $constraint->maxSize;
$suffix = 'bytes';
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000, 2);
$size = round(filesize($path) / $formatSize, 2);
$limit = (int) $constraint->maxSize;
$suffix = 'kB';
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000000, 2);
$size = round(filesize($path) / pow($formatSize, 2), 2);
$limit = (int) $constraint->maxSize;
$suffix = 'MB';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ public function testTooLargeBytes()
$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testTooLargeKiloBytes()
/**
* @dataProvider sizeFormatProvider
*/
public function testTooLargeKiloBytes($sizeFormat, $size)
{
fwrite($this->file, str_repeat('0', 1400));
fwrite($this->file, str_repeat('0', 1.4 * $size));

$constraint = new File(array(
'maxSize' => '1k',
'maxSizeMessage' => 'myMessage',
'sizeFormat' => $sizeFormat,
));

$this->context->expects($this->once())
Expand All @@ -128,13 +132,17 @@ public function testTooLargeKiloBytes()
$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testTooLargeMegaBytes()
/**
* @dataProvider sizeFormatProvider
*/
public function testTooLargeMegaBytes($sizeFormat, $size)
{
fwrite($this->file, str_repeat('0', 1400000));
fwrite($this->file, str_repeat('0', 1.4 * pow($size, 2)));

$constraint = new File(array(
'maxSize' => '1M',
'maxSizeMessage' => 'myMessage',
'sizeFormat' => $sizeFormat,
));

$this->context->expects($this->once())
Expand Down Expand Up @@ -305,6 +313,14 @@ public function testUploadedFileError($error, $message, array $params = array(),

}

public function sizeFormatProvider()
{
return array(
array('binary', 1024),
array('decimal', 1000),
);
}

public function uploadedFileErrorProvider()
{
$tests = array(
Expand Down
0