From 2ad7e679cd996b575df67a7635491fbee3188ea6 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 26 Jun 2015 17:54:27 +0200 Subject: [PATCH] [Validator] always evaluate binary format when changed --- .../Component/Validator/Constraints/File.php | 66 +++++++++++++------ .../Validator/Tests/Constraints/FileTest.php | 53 +++++++++++++-- 2 files changed, 95 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/File.php b/src/Symfony/Component/Validator/Constraints/File.php index ae0ad6739559..f35f93cdb5e0 100644 --- a/src/Symfony/Component/Validator/Constraints/File.php +++ b/src/Symfony/Component/Validator/Constraints/File.php @@ -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.'; @@ -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)); } } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php index 2ee46813bf8b..fbd4b074260b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php @@ -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) @@ -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) {