Skip to content

Commit

Permalink
[Validator] Renamed deprecated Size constraint to Range
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Jul 11, 2012
1 parent 83c058f commit 741c147
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Expand Up @@ -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
36 changes: 36 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Range.php
@@ -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');
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Component/Validator/Constraints/RangeValidator.php
@@ -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,
));
}
}
}
12 changes: 3 additions & 9 deletions src/Symfony/Component/Validator/Constraints/Size.php
Expand Up @@ -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';
}
}
42 changes: 1 addition & 41 deletions src/Symfony/Component/Validator/Constraints/SizeValidator.php
Expand Up @@ -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,
));
}
}
}
Expand Up @@ -11,18 +11,18 @@

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;

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

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

/**
Expand All @@ -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);
}

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

Expand All @@ -85,7 +85,7 @@ public function getInvalidValues()

public function testMinMessageIsSet()
{
$constraint = new Size(array(
$constraint = new Range(array(
'min' => 10,
'max' => 20,
'minMessage' => 'myMessage',
Expand All @@ -103,7 +103,7 @@ public function testMinMessageIsSet()

public function testMaxMessageIsSet()
{
$constraint = new Size(array(
$constraint = new Range(array(
'min' => 10,
'max' => 20,
'maxMessage' => 'myMessage',
Expand Down

0 comments on commit 741c147

Please sign in to comment.