From 741c147ce55b40231e71d7ab14e96774be98f376 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 18:36:40 +0200 Subject: [PATCH 1/4] [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 a1fd056d12..58296fa424 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 0000000000..0b4ec8f92f --- /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 0000000000..30b6dbcc6b --- /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 b72633eef2..625f86e528 100644 --- a/src/Symfony/Component/Validator/Constraints/Size.php +++ b/src/Symfony/Component/Validator/Constraints/Size.php @@ -20,19 +20,13 @@ use Symfony\Component\Validator\Constraint; * * @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 f26a0ec7d9..605e74b5c1 100644 --- a/src/Symfony/Component/Validator/Constraints/SizeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/SizeValidator.php @@ -21,46 +21,6 @@ use Symfony\Component\Validator\ConstraintValidator; * * @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 5cda166f48..2b202782eb 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 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase $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 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase $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 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase $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 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase public function testMinMessageIsSet() { - $constraint = new Size(array( + $constraint = new Range(array( 'min' => 10, 'max' => 20, 'minMessage' => 'myMessage', @@ -103,7 +103,7 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase public function testMaxMessageIsSet() { - $constraint = new Size(array( + $constraint = new Range(array( 'min' => 10, 'max' => 20, 'maxMessage' => 'myMessage', From 0cdacee5be5e9931fa86d0bb795e0069a6450e56 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 19:08:03 +0200 Subject: [PATCH 2/4] [Validator] Removed MinCount and MaxCount and replaced them by the constraint Count --- src/Symfony/Component/Validator/CHANGELOG.md | 2 +- .../Component/Validator/Constraints/Count.php | 45 ++++ ...xCountValidator.php => CountValidator.php} | 30 ++- .../Validator/Constraints/MaxCount.php | 41 ---- .../Validator/Constraints/MinCount.php | 41 ---- .../Constraints/MinCountValidator.php | 50 ----- .../Resources/translations/validators.en.xlf | 4 + .../Resources/translations/validators.fr.xlf | 4 + .../Resources/translations/validators.it.xlf | 4 + .../Resources/translations/validators.nl.xlf | 4 + .../Resources/translations/validators.pt.xlf | 4 + .../translations/validators.pt_BR.xlf | 4 + .../Resources/translations/validators.ru.xlf | 4 + .../Resources/translations/validators.sl.xlf | 4 + .../translations/validators.sr_Cyrl.xlf | 4 + .../translations/validators.sr_Latn.xlf | 4 + ...ayTest.php => CountValidatorArrayTest.php} | 2 +- ...st.php => CountValidatorCountableTest.php} | 6 +- .../Tests/Constraints/CountValidatorTest.php | 195 ++++++++++++++++++ .../MaxCountValidatorArrayTest.php | 23 --- .../Constraints/MaxCountValidatorTest.php | 113 ---------- .../MinCountValidatorCountableTest.php | 38 ---- .../Constraints/MinCountValidatorTest.php | 114 ---------- 23 files changed, 308 insertions(+), 432 deletions(-) create mode 100644 src/Symfony/Component/Validator/Constraints/Count.php rename src/Symfony/Component/Validator/Constraints/{MaxCountValidator.php => CountValidator.php} (54%) delete mode 100644 src/Symfony/Component/Validator/Constraints/MaxCount.php delete mode 100644 src/Symfony/Component/Validator/Constraints/MinCount.php delete mode 100644 src/Symfony/Component/Validator/Constraints/MinCountValidator.php rename src/Symfony/Component/Validator/Tests/Constraints/{MinCountValidatorArrayTest.php => CountValidatorArrayTest.php} (87%) rename src/Symfony/Component/Validator/Tests/Constraints/{MaxCountValidatorCountableTest.php => CountValidatorCountableTest.php} (75%) create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 58296fa424..f869321899 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -21,5 +21,5 @@ CHANGELOG * [BC BREAK] collections in fields annotated with `Valid` are not traversed recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. - * added MinCount and MaxCount constraint * deprecated the Size constraint and renamed it to Range + * added Count constraint diff --git a/src/Symfony/Component/Validator/Constraints/Count.php b/src/Symfony/Component/Validator/Constraints/Count.php new file mode 100644 index 0000000000..db0661d0d1 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Count.php @@ -0,0 +1,45 @@ + + * + * 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\Exception\MissingOptionsException; + +/** + * @Annotation + * + * @api + */ +class Count extends Constraint +{ + public $minMessage = 'This collection should contain {{ limit }} elements or more.'; + public $maxMessage = 'This collection should contain {{ limit }} elements or less.'; + public $exactMessage = 'This collection should contain exactly {{ limit }} elements.'; + public $min; + public $max; + + public function __construct($options = null) + { + if (null !== $options && !is_array($options)) { + $options = array( + 'min' => $options, + 'max' => $options, + ); + } + + parent::__construct($options); + + if (null === $this->min && null === $this->max) { + throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max')); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php b/src/Symfony/Component/Validator/Constraints/CountValidator.php similarity index 54% rename from src/Symfony/Component/Validator/Constraints/MaxCountValidator.php rename to src/Symfony/Component/Validator/Constraints/CountValidator.php index 50efbb7f47..d4fd3ed6aa 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountValidator.php @@ -18,15 +18,13 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek */ -class MaxCountValidator extends ConstraintValidator +class CountValidator 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 - * - * @api */ public function validate($value, Constraint $constraint) { @@ -40,11 +38,29 @@ class MaxCountValidator extends ConstraintValidator $count = count($value); - if ($count > $constraint->limit) { - $this->context->addViolation($constraint->message, array( + if ($constraint->min == $constraint->max && $count != $constraint->min) { + $this->context->addViolation($constraint->exactMessage, array( '{{ count }}' => $count, - '{{ limit }}' => $constraint->limit, - ), $value, (int) $constraint->limit); + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + + return; + } + + if (null !== $constraint->max && $count > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ count }}' => $count, + '{{ limit }}' => $constraint->max, + ), $value, (int) $constraint->max); + + return; + } + + if (null !== $constraint->min && $count < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ count }}' => $count, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); } } } diff --git a/src/Symfony/Component/Validator/Constraints/MaxCount.php b/src/Symfony/Component/Validator/Constraints/MaxCount.php deleted file mode 100644 index c3286033a0..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MaxCount.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * 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 MaxCount extends Constraint -{ - public $message = 'This collection should contain {{ limit }} elements or less.'; - public $limit; - - /** - * {@inheritDoc} - */ - public function getDefaultOption() - { - return 'limit'; - } - - /** - * {@inheritDoc} - */ - public function getRequiredOptions() - { - return array('limit'); - } -} diff --git a/src/Symfony/Component/Validator/Constraints/MinCount.php b/src/Symfony/Component/Validator/Constraints/MinCount.php deleted file mode 100644 index ebf1ff5d47..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MinCount.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * 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 MinCount extends Constraint -{ - public $message = 'This collection should contain {{ limit }} elements or more.'; - public $limit; - - /** - * {@inheritDoc} - */ - public function getDefaultOption() - { - return 'limit'; - } - - /** - * {@inheritDoc} - */ - public function getRequiredOptions() - { - return array('limit'); - } -} diff --git a/src/Symfony/Component/Validator/Constraints/MinCountValidator.php b/src/Symfony/Component/Validator/Constraints/MinCountValidator.php deleted file mode 100644 index 1620caa4b3..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MinCountValidator.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * 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; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; - -/** - * @author Bernhard Schussek - */ -class MinCountValidator 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 - * - * @throws UnexpectedTypeException If the given value is no array or \Countable. - */ - public function validate($value, Constraint $constraint) - { - if (null === $value) { - return; - } - - if (!is_array($value) && !$value instanceof \Countable) { - throw new UnexpectedTypeException($value, 'array or \Countable'); - } - - $count = count($value); - - if ($count < $constraint->limit) { - $this->context->addViolation($constraint->message, array( - '{{ count }}' => $count, - '{{ limit }}' => $constraint->limit, - ), $value, (int) $constraint->limit); - } - } -} diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index e2e6d24300..b95ec611fe 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less. + + This collection should contain exactly {{ limit }} elements. + This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf index d2e2d78159..1816ebeb24 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins. + + This collection should contain exactly {{ limit }} elements. + Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf index ee373c1958..b23a3cdeb9 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi. + + This collection should contain exactly {{ limit }} elements. + Questa collezione dovrebbe contenere esattamente {{ limit }} elemento.|Questa collezione dovrebbe contenere esattamente {{ limit }} elementi. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf index 864a726d01..90bde0b0d6 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten. + + This collection should contain exactly {{ limit }} elements. + Deze collectie moet exact {{ limit }} element bevatten.|Deze collectie moet exact {{ limit }} elementen bevatten. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf index c44f5b9328..bbe344756f 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos. + + This collection should contain exactly {{ limit }} elements. + Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf index d7822ac789..6195bbbb6e 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos. + + This collection should contain exactly {{ limit }} elements. + Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf index 141a93b65d..351760041a 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше. + + This collection should contain exactly {{ limit }} elements. + Эта коллекция должна содержать ровно {{ limit }} элемент.|Эта коллекция должна содержать ровно {{ limit }} элемента.|Эта коллекция должна содержать ровно {{ limit }} элементов. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf index 959db51fe3..fa37c289f8 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov. + + This collection should contain exactly {{ limit }} elements. + Ta zbirka bi morala vsebovati točno {{ limit }} element.|Ta zbirka bi morala vsebovati točno {{ limit }} elementa.|Ta zbirka bi morala vsebovati točno {{ limit }} elemente.|Ta zbirka bi morala vsebovati točno {{ limit }} elementov. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf index ed1b1041e6..1636b330a9 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената. + + This collection should contain exactly {{ limit }} elements. + Ова колекција треба да садржи тачно {{ limit }} елемент.|Ова колекција треба да садржи тачно {{ limit }} елемента.|Ова колекција треба да садржи тачно {{ limit }} елемената. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf index a956250e44..cf4666de30 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata. + + This collection should contain exactly {{ limit }} elements. + Ova kolekcija treba da sadrži tačno {{ limit }} element.|Ova kolekcija treba da sadrži tačno {{ limit }} elementa.|Ova kolekcija treba da sadrži tačno {{ limit }} elemenata. + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php similarity index 87% rename from src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php index 96eb63a157..5f562e7445 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php @@ -14,7 +14,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; /** * @author Bernhard Schussek */ -class MinCountValidatorArrayTest extends MinCountValidatorTest +class CountValidatorArrayTest extends CountValidatorTest { protected function createCollection(array $content) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php similarity index 75% rename from src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php index c4852e00d3..ec4d8dec71 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; -class MaxCountValidatorCountableTest_Countable implements \Countable +class CountValidatorCountableTest_Countable implements \Countable { private $content; @@ -29,10 +29,10 @@ class MaxCountValidatorCountableTest_Countable implements \Countable /** * @author Bernhard Schussek */ -class MaxCountValidatorCountableTest extends MaxCountValidatorTest +class CountValidatorCountableTest extends CountValidatorTest { protected function createCollection(array $content) { - return new MaxCountValidatorCountableTest_Countable($content); + return new CountValidatorCountableTest_Countable($content); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php new file mode 100644 index 0000000000..c7d29c6d55 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -0,0 +1,195 @@ + + * + * 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\Count; +use Symfony\Component\Validator\Constraints\CountValidator; + +/** + * @author Bernhard Schussek + */ +abstract class CountValidatorTest extends \PHPUnit_Framework_TestCase +{ + protected $context; + protected $validator; + + protected function setUp() + { + $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); + $this->validator = new CountValidator(); + $this->validator->initialize($this->context); + } + + protected function tearDown() + { + $this->context = null; + $this->validator = null; + } + + abstract protected function createCollection(array $content); + + public function testNullIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate(null, new Count(6)); + } + + /** + * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsCountableType() + { + $this->validator->validate(new \stdClass(), new Count(5)); + } + + public function getThreeOrLessElements() + { + return array( + array($this->createCollection(array(1))), + array($this->createCollection(array(1, 2))), + array($this->createCollection(array(1, 2, 3))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), + ); + } + + public function getFourElements() + { + return array( + array($this->createCollection(array(1, 2, 3, 4))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), + ); + } + + public function getNotFourElements() + { + return array_merge( + $this->getThreeOrLessElements(), + $this->getFiveOrMoreElements() + ); + } + + public function getFiveOrMoreElements() + { + return array( + array($this->createCollection(array(1, 2, 3, 4, 5))), + array($this->createCollection(array(1, 2, 3, 4, 5, 6))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5))), + ); + } + + /** + * @dataProvider getThreeOrLessElements + */ + public function testValidValuesMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(array('max' => 3)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreElements + */ + public function testValidValuesMin($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(array('min' => 5)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFourElements + */ + public function testValidValuesExact($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(4); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreElements + */ + public function testInvalidValuesMax($value) + { + $constraint = new Count(array( + 'max' => 4, + 'maxMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessElements + */ + public function testInvalidValuesMin($value) + { + $constraint = new Count(array( + 'min' => 4, + 'minMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getNotFourElements + */ + public function testInvalidValuesExact($value) + { + $constraint = new Count(array( + 'min' => 4, + 'max' => 4, + 'exactMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + public function testDefaultOption() + { + $constraint = new Count(5); + + $this->assertEquals(5, $constraint->min); + $this->assertEquals(5, $constraint->max); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php deleted file mode 100644 index 8453930ae7..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * 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; - -/** - * @author Bernhard Schussek - */ -class MaxCountValidatorArrayTest extends MaxCountValidatorTest -{ - protected function createCollection(array $content) - { - return $content; - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php deleted file mode 100644 index f6ef305ffe..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * 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\MaxCount; -use Symfony\Component\Validator\Constraints\MaxCountValidator; - -/** - * @author Bernhard Schussek - */ -abstract class MaxCountValidatorTest extends \PHPUnit_Framework_TestCase -{ - protected $context; - protected $validator; - - protected function setUp() - { - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - $this->validator = new MaxCountValidator(); - $this->validator->initialize($this->context); - } - - protected function tearDown() - { - $this->context = null; - $this->validator = null; - } - - abstract protected function createCollection(array $content); - - public function testNullIsValid() - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $this->validator->validate(null, new MaxCount(6)); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsCountableType() - { - $this->validator->validate(new \stdClass(), new MaxCount(5)); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new MaxCount(3); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() - { - return array( - array($this->createCollection(array(1))), - array($this->createCollection(array(1, 2))), - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), - ); - } - - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) - { - $constraint = new MaxCount(array( - 'limit' => 3, - 'message' => 'myMessage' - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', $this->identicalTo(array( - '{{ count }}' => count($value), - '{{ limit }}' => 3, - )), $value, 3); - - $this->validator->validate($value, $constraint); - } - - public function getInvalidValues() - { - return array( - array($this->createCollection(array(1, 2, 3, 4))), - array($this->createCollection(array(1, 2, 3, 4, 5))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), - ); - } - - public function testDefaultOption() - { - $constraint = new MaxCount(5); - - $this->assertEquals(5, $constraint->limit); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php deleted file mode 100644 index 48b175a65d..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * 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; - -class MinCountValidatorCountableTest_Countable implements \Countable -{ - private $content; - - public function __construct(array $content) - { - $this->content = $content; - } - - public function count() - { - return count($this->content); - } -} - -/** - * @author Bernhard Schussek - */ -class MinCountValidatorCountableTest extends MinCountValidatorTest -{ - protected function createCollection(array $content) - { - return new MinCountValidatorCountableTest_Countable($content); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php deleted file mode 100644 index ab0cf86eaa..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * 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\MinCount; -use Symfony\Component\Validator\Constraints\MinCountValidator; - -/** - * @author Bernhard Schussek - */ -abstract class MinCountValidatorTest extends \PHPUnit_Framework_TestCase -{ - protected $context; - protected $validator; - - protected function setUp() - { - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - $this->validator = new MinCountValidator(); - $this->validator->initialize($this->context); - } - - protected function tearDown() - { - $this->context = null; - $this->validator = null; - } - - abstract protected function createCollection(array $content); - - public function testNullIsValid() - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $this->validator->validate(null, new MinCount(6)); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsCountableType() - { - $this->validator->validate(new \stdClass(), new MinCount(5)); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new MinCount(3); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() - { - return array( - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array(1, 2, 3, 4))), - array($this->createCollection(array(1, 2, 3, 4, 5))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), - ); - } - - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) - { - $constraint = new MinCount(array( - 'limit' => 4, - 'message' => 'myMessage' - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', $this->identicalTo(array( - '{{ count }}' => count($value), - '{{ limit }}' => 4, - )), $value, 4); - - $this->validator->validate($value, $constraint); - } - - public function getInvalidValues() - { - return array( - array($this->createCollection(array(1))), - array($this->createCollection(array(1, 2))), - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), - ); - } - - public function testDefaultOption() - { - $constraint = new MinCount(5); - - $this->assertEquals(5, $constraint->limit); - } -} From 83a3f75b2dd6619b0ed806404f9164a8c7b7a5f1 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 19:22:49 +0200 Subject: [PATCH 3/4] [Validator] Deprecated the constraints Min and Max in favor of Range --- UPGRADE-2.1.md | 23 ++- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Component/Validator/Constraints/Max.php | 2 + .../Validator/Constraints/MaxValidator.php | 2 + .../Component/Validator/Constraints/Min.php | 2 + .../Validator/Constraints/MinValidator.php | 2 + .../Component/Validator/Constraints/Range.php | 12 +- .../Validator/Constraints/RangeValidator.php | 4 +- .../Tests/Constraints/RangeValidatorTest.php | 152 +++++++++++++++--- 9 files changed, 169 insertions(+), 31 deletions(-) diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index ef8756ab0a..06c8eccfc9 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1136,8 +1136,8 @@ private $recursiveCollection; ``` - * The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should - use the constraints `Min` and `Max` instead. + * The `Size`, `Min` and `Max` constraints were deprecated and will be removed in + Symfony 2.3. You should use the new constraint `Range` instead. Before: @@ -1149,10 +1149,21 @@ After: ``` - /** - * @Assert\Min(2) - * @Assert\Max(16) - */ + /** @Assert\Range(min = 2, max = 16) */ + private $numberOfCpus; + ``` + + Before: + + ``` + /** @Assert\Min(2) */ + private $numberOfCpus; + ``` + + After: + + ``` + /** @Assert\Range(min = 2) */ private $numberOfCpus; ``` diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index f869321899..781e88e0f1 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -22,4 +22,5 @@ CHANGELOG recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. * deprecated the Size constraint and renamed it to Range + * deprecated the Min and Max constraints * added Count constraint diff --git a/src/Symfony/Component/Validator/Constraints/Max.php b/src/Symfony/Component/Validator/Constraints/Max.php index ffb958e85b..814fc1f339 100644 --- a/src/Symfony/Component/Validator/Constraints/Max.php +++ b/src/Symfony/Component/Validator/Constraints/Max.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Max extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MaxValidator.php b/src/Symfony/Component/Validator/Constraints/MaxValidator.php index 18d8e8902e..e3e9f8c462 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxValidator.php @@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Min.php b/src/Symfony/Component/Validator/Constraints/Min.php index 78a4833296..6e8b3649b7 100644 --- a/src/Symfony/Component/Validator/Constraints/Min.php +++ b/src/Symfony/Component/Validator/Constraints/Min.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Min extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MinValidator.php b/src/Symfony/Component/Validator/Constraints/MinValidator.php index a91d873864..c907793c62 100644 --- a/src/Symfony/Component/Validator/Constraints/MinValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinValidator.php @@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php index 0b4ec8f92f..20039f5972 100644 --- a/src/Symfony/Component/Validator/Constraints/Range.php +++ b/src/Symfony/Component/Validator/Constraints/Range.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\MissingOptionsException; /** * @Annotation @@ -26,11 +27,12 @@ class Range extends Constraint public $min; public $max; - /** - * {@inheritDoc} - */ - public function getRequiredOptions() + public function __construct($options = null) { - return array('min', 'max'); + parent::__construct($options); + + if (null === $this->min && null === $this->max) { + throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max')); + } } } diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php index 30b6dbcc6b..4c30388c86 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -41,7 +41,7 @@ class RangeValidator extends ConstraintValidator return; } - if ($value > $constraint->max) { + if (null !== $constraint->max && $value > $constraint->max) { $this->context->addViolation($constraint->maxMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, @@ -50,7 +50,7 @@ class RangeValidator extends ConstraintValidator return; } - if ($value < $constraint->min) { + if (null !== $constraint->min && $value < $constraint->min) { $this->context->addViolation($constraint->minMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->min, diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 2b202782eb..c44b0ea6ce 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -34,19 +34,7 @@ class RangeValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20))); } - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new Range(array('min' => 10, 'max' => 20)); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() + public function getTenToTwenty() { return array( array(10.00001), @@ -60,18 +48,146 @@ class RangeValidatorTest extends \PHPUnit_Framework_TestCase ); } - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) + public function getLessThanTen() { - $this->context->expects($this->once()) + return array( + array(9.99999), + array('9.99999'), + array(5), + array(1.0), + ); + } + + public function getMoreThanTwenty() + { + return array( + array(20.000001), + array('20.000001'), + array(21), + array(30.0), + ); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMin($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('min' => 10)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('max' => 20)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMinMax($value) + { + $this->context->expects($this->never()) ->method('addViolation'); $constraint = new Range(array('min' => 10, 'max' => 20)); $this->validator->validate($value, $constraint); } + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'minMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesMax($value) + { + $constraint = new Range(array( + 'max' => 20, + 'maxMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesCombinedMax($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMaxMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesCombinedMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMinMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + public function getInvalidValues() { return array( From a92f80b815deac74dc4a46efeb915e5333f581f9 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 19:40:29 +0200 Subject: [PATCH 4/4] [Validator] Added Length constraint and deprecated MinLength and MaxLength --- UPGRADE-2.1.md | 17 ++ src/Symfony/Component/Validator/CHANGELOG.md | 4 +- .../Validator/Constraints/Length.php | 46 ++++ .../Validator/Constraints/LengthValidator.php | 74 ++++++ .../Validator/Constraints/MaxLength.php | 2 + .../Constraints/MaxLengthValidator.php | 2 + .../Validator/Constraints/MinLength.php | 2 + .../Constraints/MinLengthValidator.php | 2 + .../Tests/Constraints/LengthValidatorTest.php | 233 ++++++++++++++++++ 9 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Validator/Constraints/Length.php create mode 100644 src/Symfony/Component/Validator/Constraints/LengthValidator.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 06c8eccfc9..3ba03d1ea1 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1167,6 +1167,23 @@ private $numberOfCpus; ``` + * The `MinLength` and `MaxLength` constraints were deprecated and will be + removed in Symfony 2.3. You should use the new constraint `Length` instead. + + Before: + + ``` + /** @Assert\MinLength(8) */ + private $password; + ``` + + After: + + ``` + /** @Assert\Length(min = 8) */ + private $password; + ``` + ### Session * Flash messages now return an array based on their type. The old method is diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 781e88e0f1..dbc3e0b0d7 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -21,6 +21,8 @@ CHANGELOG * [BC BREAK] collections in fields annotated with `Valid` are not traversed recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. + * added Count constraint + * added Length constraint * deprecated the Size constraint and renamed it to Range * deprecated the Min and Max constraints - * added Count constraint + * deprecated the MinLength and MaxLength constraints diff --git a/src/Symfony/Component/Validator/Constraints/Length.php b/src/Symfony/Component/Validator/Constraints/Length.php new file mode 100644 index 0000000000..70554c0686 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Length.php @@ -0,0 +1,46 @@ + + * + * 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\Exception\MissingOptionsException; + +/** + * @Annotation + * + * @api + */ +class Length extends Constraint +{ + public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less.'; + public $minMessage = 'This value is too short. It should have {{ limit }} characters or more.'; + public $exactMessage = 'This value should have exactly {{ limit }} characters.'; + public $max; + public $min; + public $charset = 'UTF-8'; + + public function __construct($options = null) + { + if (null !== $options && !is_array($options)) { + $options = array( + 'min' => $options, + 'max' => $options, + ); + } + + parent::__construct($options); + + if (null === $this->min && null === $this->max) { + throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max')); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php new file mode 100644 index 0000000000..ee89e75477 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -0,0 +1,74 @@ + + * + * 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; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; + +/** + * @author Bernhard Schussek + */ +class LengthValidator 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 + */ + public function validate($value, Constraint $constraint) + { + if (null === $value || '' === $value) { + return; + } + + if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + + $stringValue = (string) $value; + + if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) { + $length = grapheme_strlen($stringValue); + } elseif (function_exists('mb_strlen')) { + $length = mb_strlen($stringValue, $constraint->charset); + } else { + $length = strlen($stringValue); + } + + if ($constraint->min == $constraint->max && $length != $constraint->min) { + $this->context->addViolation($constraint->exactMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + + return; + } + + if (null !== $constraint->max && $length > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->max, + ), $value, (int) $constraint->max); + + return; + } + + if (null !== $constraint->min && $length < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/MaxLength.php b/src/Symfony/Component/Validator/Constraints/MaxLength.php index c928bd5bfd..765d548060 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxLength.php +++ b/src/Symfony/Component/Validator/Constraints/MaxLength.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxLength extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php index e7f8d72758..076734cd40 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php @@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxLengthValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/MinLength.php b/src/Symfony/Component/Validator/Constraints/MinLength.php index 1ee15906a6..4457f6c160 100644 --- a/src/Symfony/Component/Validator/Constraints/MinLength.php +++ b/src/Symfony/Component/Validator/Constraints/MinLength.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinLength extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php index 77d833e998..fc8cef537a 100644 --- a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php @@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinLengthValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php new file mode 100644 index 0000000000..dbcef694e6 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -0,0 +1,233 @@ + + * + * 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\Length; +use Symfony\Component\Validator\Constraints\LengthValidator; + +class LengthValidatorTest extends \PHPUnit_Framework_TestCase +{ + protected $context; + protected $validator; + + protected function setUp() + { + $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); + $this->validator = new LengthValidator(); + $this->validator->initialize($this->context); + } + + protected function tearDown() + { + $this->context = null; + $this->validator = null; + } + + public function testNullIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate(null, new Length(6)); + } + + public function testEmptyStringIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate('', new Length(6)); + } + + /** + * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsStringCompatibleType() + { + $this->validator->validate(new \stdClass(), new Length(5)); + } + + public function getThreeOrLessCharacters() + { + return array( + array(12), + array('12'), + array('üü', true), + array('éé', true), + array(123), + array('123'), + array('üüü', true), + array('ééé', true), + ); + } + + public function getFourCharacters() + { + return array( + array(1234), + array('1234'), + array('üüüü', true), + array('éééé', true), + ); + } + + public function getNotFourCharacters() + { + return array_merge( + $this->getThreeOrLessCharacters(), + $this->getFiveOrMoreCharacters() + ); + } + + public function getFiveOrMoreCharacters() + { + return array( + array(12345), + array('12345'), + array('üüüüü', true), + array('ééééé', true), + array(123456), + array('123456'), + array('üüüüüü', true), + array('éééééé', true), + ); + } + + /** + * @dataProvider getFiveOrMoreCharacters + */ + public function testValidValuesMin($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(array('min' => 5)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessCharacters + */ + public function testValidValuesMax($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(array('max' => 3)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFourCharacters + */ + public function testValidValuesExact($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(4); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessCharacters + */ + public function testInvalidValuesMin($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'min' => 4, + 'minMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreCharacters + */ + public function testInvalidValuesMax($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'max' => 4, + 'maxMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getNotFourCharacters + */ + public function testInvalidValuesExact($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'min' => 4, + 'max' => 4, + 'exactMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + public function testConstraintGetDefaultOption() + { + $constraint = new Length(5); + + $this->assertEquals(5, $constraint->min); + $this->assertEquals(5, $constraint->max); + } +}