Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Validator] always evaluate binary format when changed
  • Loading branch information
xabbuh committed Jul 1, 2015
1 parent 5e53d6d commit 2ad7e67
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 24 deletions.
66 changes: 46 additions & 20 deletions src/Symfony/Component/Validator/Constraints/File.php
Expand Up @@ -40,7 +40,6 @@ class File extends Constraint
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
);

public $maxSize;
public $binaryFormat;
public $mimeTypes = array();
public $notFoundMessage = 'The file could not be found.';
Expand All @@ -58,29 +57,56 @@ class File extends Constraint
public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
public $uploadErrorMessage = 'The file could not be uploaded.';

protected $maxSize;

public function __construct($options = null)
{
parent::__construct($options);

if ($this->maxSize) {
if (ctype_digit((string) $this->maxSize)) {
$this->maxSize = (int) $this->maxSize;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++Ki$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 10;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 20;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
if (null !== $this->maxSize) {
$this->normalizeBinaryFormat($this->maxSize);
}
}

public function __set($option, $value)
{
if ('maxSize' === $option) {
$this->normalizeBinaryFormat($value);

return;
}

parent::__set($option, $value);
}

public function __get($option)
{
if ('maxSize' === $option) {
return $this->maxSize;
}

return parent::__get($option);
}

private function normalizeBinaryFormat($maxSize)
{
if (ctype_digit((string) $maxSize)) {
$this->maxSize = (int) $maxSize;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $maxSize)) {
$this->maxSize = $maxSize * 1000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $maxSize)) {
$this->maxSize = $maxSize * 1000000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
$this->maxSize = $maxSize << 10;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
$this->maxSize = $maxSize << 20;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
}
}
53 changes: 49 additions & 4 deletions src/Symfony/Component/Validator/Tests/Constraints/FileTest.php
Expand Up @@ -12,13 +12,14 @@
namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

class FileTest extends \PHPUnit_Framework_TestCase
{
/**
* @param mixed $maxSize
* @param int bytes
* @param bool $bytes
* @param int $bytes
* @param bool $binaryFormat
* @dataProvider provideValidSizes
*/
public function testMaxSize($maxSize, $bytes, $binaryFormat)
Expand All @@ -29,11 +30,55 @@ public function testMaxSize($maxSize, $bytes, $binaryFormat)
$this->assertSame($binaryFormat, $file->binaryFormat);
}

/**
* @dataProvider provideValidSizes
*
* @param int|string $maxSize
* @param int $bytes
* @param string $binaryFormat
*/
public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binaryFormat)
{
$file = new File();
$file->maxSize = $maxSize;

$this->assertSame($bytes, $file->maxSize);
$this->assertSame($binaryFormat, $file->binaryFormat);
}

/**
* @dataProvider provideInvalidSizes
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*
* @param int|string $maxSize
*/
public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize)
{
$file = new File(array('maxSize' => 1000));
$file->maxSize = $maxSize;
}

/**
* @dataProvider provideInvalidSizes
*
* @param int|string $maxSize
*/
public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize)
{
$file = new File(array('maxSize' => 1000));

try {
$file->maxSize = $maxSize;
} catch (ConstraintDefinitionException $e) {
}

$this->assertSame(1000, $file->maxSize);
}

/**
* @param mixed $maxSize
* @param int $bytes
* @dataProvider provideInValidSizes
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testInvalideMaxSize($maxSize)
{
Expand Down

0 comments on commit 2ad7e67

Please sign in to comment.