10000 [Validator] Renamed deprecated Size constraint to Range · symfony/symfony@741c147 · GitHub
[go: up one dir, main page]

Skip to content

Commit 741c147

Browse files
committed
[Validator] Renamed deprecated Size constraint to Range
1 parent 83c058f commit 741c147

File tree

6 files changed

+110
-60
lines changed

6 files changed

+110
-60
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ CHANGELOG
2222
recursively anymore by default. `Valid` contains a new property `deep`
2323
which enables the BC behavior.
2424
* added MinCount and MaxCount constraint
25-
* deprecated the Size constraint
25+
* deprecated the Size constraint and renamed it to Range
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
*
19+
* @api
20+
*/
21+
class Range extends Constraint
22+
{
23+
public $minMessage = 'This value should be {{ limit }} or more';
24+
public $maxMessage = 'This value should be {{ limit }} or less';
25+
public $invalidMessage = 'This value should be a valid number';
26+
public $min;
27+
public $max;
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function getRequiredOptions()
33+
{
34+
return array('min', 'max');
35+
}
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
17+
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*/
20+
class RangeValidator extends ConstraintValidator
21+
{
22+
/**
23+
* Checks if the passed value is valid.
24+
*
25+
* @param mixed $value The value that should be validated
26+
* @param Constraint $constraint The constraint for the validation
27+
*
28+
* @return Boolean Whether or not the value is valid
29+
*/
30+
public function validate($value, Constraint $constraint)
31+
{
32+
if (null === $value) {
33+
return;
34+
}
35+
36+
if (!is_numeric($value)) {
37+
$this->context->addViolation($constraint->invalidMessage, array(
38+
'{{ value }}' => $value,
39+
));
40+
41+
return;
42+
}
43+
44+
if ($value > $constraint->max) {
45+
$this->context->addViolation($constraint->maxMessage, array(
46+
'{{ value }}' => $value,
47+
'{{ limit }}' => $constraint->max,
48+
));
49+
50+
return;
51+
}
52+
53+
if ($value < $constraint->min) {
54+
$this->context->addViolation($constraint->minMessage, array(
55+
'{{ value }}' => $value,
56+
'{{ limit }}' => $constraint->min,
57+
));
58+
}
59+
}
60+
}

src/Symfony/Component/Validator/Constraints/Size.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@
2020
*
2121
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2222
*/
23-
class Size extends Constraint
23+
class Size extends Range
2424
{
25-
public $minMessage = 'This value should be {{ limit }} or more';
26-
public $maxMessage = 'This value should be {{ limit }} or less';
27-
public $invalidMessage = 'This value should be a valid number';
28-
public $min;
29-
public $max;
30-
3125
/**
3226
* {@inheritDoc}
3327
*/
34-
public function getRequiredOptions()
28+
public function validatedBy()
3529
{
36-
return array('min', 'max');
30+
return get_parent_class($this).'Validator';
3731
}
3832
}

src/Symfony/Component/Validator/Constraints/SizeValidator.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,6 @@
2121
*
2222
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2323
*/
24-
class SizeValidator extends ConstraintValidator
24+
class SizeValidator extends RangeValidator
2525
{
26-
/**
27-
* Checks if the passed value is valid.
28-
*
29-
* @param mixed $value The value that should be validated
30-
* @param Constraint $constraint The constraint for the validation
31-
*
32-
* @return Boolean Whether or not the value is valid
33-
*
34-
* @api
35-
*/
36-
public function validate($value, Constraint $constraint)
37-
{
38-
if (null === $value) {
39-
return;
40-
}
41-
42-
if (!is_numeric($value)) {
43-
$this->context->addViolation($constraint->invalidMessage, array(
44-
'{{ value }}' => $value,
45-
));
46-
47-
return;
48-
}
49-
50-
if ($value > $constraint->max) {
51-
$this->context->addViolation($constraint->maxMessage, array(
52-
'{{ value }}' => $value,
53-
'{{ limit }}' => $constraint->max,
54-
));
55-
56-
return;
57-
}
58-
59-
if ($value < $constraint->min) {
60-
$this->context->addViolation($constraint->minMessage, array(
61-
'{{ value }}' => $value,
62-
'{{ limit }}' => $constraint->min,
63-
));
64-
}
65-
}
6626
}

src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php renamed to src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14-
use Symfony\Component\Validator\Constraints\Size;
15-
use Symfony\Component\Validator\Constraints\SizeValidator;
14+
use Symfony\Component\Validator\Constraints\Range;
15+
use Symfony\Component\Validator\Constraints\RangeValidator;
1616

17-
class SizeValidatorTest extends \PHPUnit_Framework_TestCase
17+
class RangeValidatorTest extends \PHPUnit_Framework_TestCase
1818
{
1919
protected $context;
2020
protected $validator;
2121

2222
protected function setUp()
2323
{
2424
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
25-
$this->validator = new SizeValidator();
25+
$this->validator = new RangeValidator();
2626
$this->validator->initialize($this->context);
2727
}
2828

@@ -31,7 +31,7 @@ public function testNullIsValid()
3131
$this->context->expects($this->never())
3232
->method('addViolation');
3333

34-
$this->validator->validate(null, new Size(array('min' => 10, 'max' => 20)));
34+
$this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
3535
}
3636

3737
/**
@@ -42,7 +42,7 @@ public function testValidValues($value)
4242
$this->context->expects($this->never())
4343
->method('addViolation');
4444

45-
$constraint = new Size(array('min' => 10, 'max' => 20));
45+
$constraint = new Range(array('min' => 10, 'max' => 20));
4646
$this->validator->validate($value, $constraint);
4747
}
4848

@@ -68,7 +68,7 @@ public function testInvalidValues($value)
6868
$this->context->expects($this->once())
6969
->method('addViolation');
7070

71-
$constraint = new Size(array('min' => 10, 'max' => 20));
71+
$constraint = new Range(array('min' => 10, 'max' => 20));
7272
$this->validator->validate($value, $constraint);
7373
}
7474

@@ -85,7 +85,7 @@ public function getInvalidValues()
8585

8686
public function testMinMessageIsSet()
8787
{
88-
$constraint = new Size(array(
88+
$constraint = new Range(array(
8989
'min' => 10,
9090
'max' => 20,
9191
'minMessage' => 'myMessage',
@@ -103,7 +103,7 @@ public function testMinMessageIsSet()
103103

104104
public function testMaxMessageIsSet()
105105
{
106-
$constraint = new Size(array(
106+
$constraint = new Range(array(
107107
'min' => 10,
108108
'max' => 20,
109109
'maxMessage' => 'myMessage',

0 commit comments

Comments
 (0)
0