From 741c147ce55b40231e71d7ab14e96774be98f376 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 18:36:40 +0200 Subject: [PATCH] [Validator] Renamed deprecated Size constraint to Range --- src/Symfony/Component/Validator/CHANGELOG.md | 2 +- .../Component/Validator/Constraints/Range.php | 36 +++++++++++ .../Validator/Constraints/RangeValidator.php | 60 +++++++++++++++++++ .../Component/Validator/Constraints/Size.php | 12 +--- .../Validator/Constraints/SizeValidator.php | 42 +------------ ...lidatorTest.php => RangeValidatorTest.php} | 18 +++--- 6 files changed, 110 insertions(+), 60 deletions(-) create mode 100644 src/Symfony/Component/Validator/Constraints/Range.php create mode 100644 src/Symfony/Component/Validator/Constraints/RangeValidator.php rename src/Symfony/Component/Validator/Tests/Constraints/{SizeValidatorTest.php => RangeValidatorTest.php} (83%) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index a1fd056d12a6..58296fa42416 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -22,4 +22,4 @@ CHANGELOG recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. * added MinCount and MaxCount constraint - * deprecated the Size constraint + * deprecated the Size constraint and renamed it to Range diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php new file mode 100644 index 000000000000..0b4ec8f92f59 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Range.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @api + */ +class Range extends Constraint +{ + public $minMessage = 'This value should be {{ limit }} or more'; + public $maxMessage = 'This value should be {{ limit }} or less'; + public $invalidMessage = 'This value should be a valid number'; + public $min; + public $max; + + /** + * {@inheritDoc} + */ + public function getRequiredOptions() + { + return array('min', 'max'); + } +} diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php new file mode 100644 index 000000000000..30b6dbcc6bfe --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * @author Bernhard Schussek + */ +class RangeValidator extends ConstraintValidator +{ + /** + * Checks if the passed value is valid. + * + * @param mixed $value The value that should be validated + * @param Constraint $constraint The constraint for the validation + * + * @return Boolean Whether or not the value is valid + */ + public function validate($value, Constraint $constraint) + { + if (null === $value) { + return; + } + + if (!is_numeric($value)) { + $this->context->addViolation($constraint->invalidMessage, array( + '{{ value }}' => $value, + )); + + return; + } + + if ($value > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->max, + )); + + return; + } + + if ($value < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->min, + )); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/Size.php b/src/Symfony/Component/Validator/Constraints/Size.php index b72633eef250..625f86e52889 100644 --- a/src/Symfony/Component/Validator/Constraints/Size.php +++ b/src/Symfony/Component/Validator/Constraints/Size.php @@ -20,19 +20,13 @@ * * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ -class Size extends Constraint +class Size extends Range { - public $minMessage = 'This value should be {{ limit }} or more'; - public $maxMessage = 'This value should be {{ limit }} or less'; - public $invalidMessage = 'This value should be a valid number'; - public $min; - public $max; - /** * {@inheritDoc} */ - public function getRequiredOptions() + public function validatedBy() { - return array('min', 'max'); + return get_parent_class($this).'Validator'; } } diff --git a/src/Symfony/Component/Validator/Constraints/SizeValidator.php b/src/Symfony/Component/Validator/Constraints/SizeValidator.php index f26a0ec7d967..605e74b5c143 100644 --- a/src/Symfony/Component/Validator/Constraints/SizeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/SizeValidator.php @@ -21,46 +21,6 @@ * * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ -class SizeValidator extends ConstraintValidator +class SizeValidator extends RangeValidator { - /** - * Checks if the passed value is valid. - * - * @param mixed $value The value that should be validated - * @param Constraint $constraint The constraint for the validation - * - * @return Boolean Whether or not the value is valid - * - * @api - */ - public function validate($value, Constraint $constraint) - { - if (null === $value) { - return; - } - - if (!is_numeric($value)) { - $this->context->addViolation($constraint->invalidMessage, array( - '{{ value }}' => $value, - )); - - return; - } - - if ($value > $constraint->max) { - $this->context->addViolation($constraint->maxMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->max, - )); - - return; - } - - if ($value < $constraint->min) { - $this->context->addViolation($constraint->minMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->min, - )); - } - } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php similarity index 83% rename from src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 5cda166f4847..2b202782eb3e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -11,10 +11,10 @@ namespace Symfony\Component\Validator\Tests\Constraints; -use Symfony\Component\Validator\Constraints\Size; -use Symfony\Component\Validator\Constraints\SizeValidator; +use Symfony\Component\Validator\Constraints\Range; +use Symfony\Component\Validator\Constraints\RangeValidator; -class SizeValidatorTest extends \PHPUnit_Framework_TestCase +class RangeValidatorTest extends \PHPUnit_Framework_TestCase { protected $context; protected $validator; @@ -22,7 +22,7 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - $this->validator = new SizeValidator(); + $this->validator = new RangeValidator(); $this->validator->initialize($this->context); } @@ -31,7 +31,7 @@ public function testNullIsValid() $this->context->expects($this->never()) ->method('addViolation'); - $this->validator->validate(null, new Size(array('min' => 10, 'max' => 20))); + $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20))); } /** @@ -42,7 +42,7 @@ public function testValidValues($value) $this->context->expects($this->never()) ->method('addViolation'); - $constraint = new Size(array('min' => 10, 'max' => 20)); + $constraint = new Range(array('min' => 10, 'max' => 20)); $this->validator->validate($value, $constraint); } @@ -68,7 +68,7 @@ public function testInvalidValues($value) $this->context->expects($this->once()) ->method('addViolation'); - $constraint = new Size(array('min' => 10, 'max' => 20)); + $constraint = new Range(array('min' => 10, 'max' => 20)); $this->validator->validate($value, $constraint); } @@ -85,7 +85,7 @@ public function getInvalidValues() public function testMinMessageIsSet() { - $constraint = new Size(array( + $constraint = new Range(array( 'min' => 10, 'max' => 20, 'minMessage' => 'myMessage', @@ -103,7 +103,7 @@ public function testMinMessageIsSet() public function testMaxMessageIsSet() { - $constraint = new Size(array( + $constraint = new Range(array( 'min' => 10, 'max' => 20, 'maxMessage' => 'myMessage',