[Validator] Renamed deprecated Size constraint to Range

This commit is contained in:
Bernhard Schussek 2012-07-11 18:36:40 +02:00
parent 83c058fbf1
commit 741c147ce5
6 changed files with 110 additions and 60 deletions

View File

@ -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

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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');
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bschussek@gmail.com>
*/
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,
));
}
}
}

View File

@ -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';
}
}

View File

@ -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,
));
}
}
}

View File

@ -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',