8000 merged branch bschussek/size_deprecation (PR #4851) · symfony/symfony@c3b79f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3b79f1

Browse files
committed
merged branch bschussek/size_deprecation (PR #4851)
Commits ------- 0be602d [Validator] Deprecated the Size constraint d661837 [Validator] Reverted the changes done to the Size constraint in 3a5e84f d84b689 [Validator] Added the constraints MinCount and MaxCount 1a732e4 [Validator] Removed the Range constraint as it duplicates functionality given in Min and Max Discussion ---------- [Validator] Deprecated the Size constraint in favor of MinCount and MaxCount Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - This PR cleans up with the current ambiguity between * Min * Max * MinLength * MaxLength * Range * Size in the following ways: * The Range constraint was removed again as it can be completely replaced by Min and Max. * The Size constraint was reverted to it's 2.0 feature set and deprecated. * The constraints MinCount and MaxCount were added to make up for the functionality that was added to Size.
2 parents d92a90f + 0be602d commit c3b79f1

28 files changed

+585
-666
lines changed

UPGRADE-2.1.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,26 @@
11361136
private $recursiveCollection;
11371137
```
11381138
1139+
* The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should
1140+
use the constraints `Min` and `Max` instead.
1141+
1142+
Before:
1143+
1144+
```
1145+
/** @Assert\Size(min = 2, max = 16) */
1146+
private $numberOfCpus;
1147+
```
1148+
1149+
After:
1150+
1151+
```
1152+
/**
1153+
* @Assert\Min(2)
1154+
* @Assert\Max(16)
1155+
*/
1156+
private $numberOfCpus;
1157+
```
1158+
11391159
### Session
11401160
11411161
* Flash messages now return an array based on their type. The old method is

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,14 @@ public function guessTypeForConstraint(Constraint $constraint)
158158
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
159159

160160
case 'Symfony\Component\Validator\Constraints\Size':
161-
switch ($constraint->type) {
162-
case 'string':
163-
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
164-
case 'collection':
165-
return new TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE);
166-
}
167-
break;
168-
169161
case 'Symfony\Component\Validator\Constraints\Min':
170-
case 'Symfony\Component\Validator\Constraints\Range':
171162
case 'Symfony\Component\Validator\Constraints\Max':
172163
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
173164

165+
case 'Symfony\Component\Validator\Constraints\MinCount':
166+
case 'Symfony\Component\Validator\Constraints\MaxCount':
167+
return new TypeGuess('collection', array(), Guess::LOW_CONFIDENCE);
168+
174169
case 'Symfony\Component\Validator\Constraints\Time':
175170
return new TypeGuess('time', array('input'=>'string'), Guess::HIGH_CONFIDENCE);
176171

@@ -208,12 +203,6 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
208203
case 'Symfony\Component\Validator\Constraints\MaxLength':
209204
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
210205

211-
case 'Symfony\Component\Validator\Constraints\Size':
212-
if ('string' === $constraint->type && null !== $constraint->max) {
213-
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
214-
}
215-
break;
216-
217206
case 'Symfony\Component\Validator\Constraints\Type':
218207
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
219208
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
@@ -223,7 +212,7 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
223212
case 'Symfony\Component\Validator\Constraints\Max':
224213
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
225214

226-
case 'Symfony\Component\Validator\Constraints\Range':
215+
case 'Symfony\Component\Validator\Constraints\Size':
227216
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
228217
}
229218
}
@@ -241,25 +230,6 @@ public function guessPatternForConstraint(Constraint $constraint)
241230
case 'Symfony\Component\Validator\Constraints\MinLength':
242231
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
243232

244-
case 'Symfony\Component\Validator\Constraints\Size':
245-
if ('string' !== $constraint->type) {
246-
return;
247-
}
248-
249-
if ($constraint->min === $constraint->max) {
250-
return new ValueGuess(sprintf('.{%s}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
251-
}
252-
253-
if (null === $constraint->min) {
254-
return new ValueGuess(sprintf('.{0,%s}', (string) $constraint->max), Guess::LOW_CONFIDENCE);
255-
}
256-
257-
if (null === $constraint->max) {
258-
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
259-
}
260-
261-
return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE);
262-
263233
case 'Symfony\Component\Validator\Constraints\Regex':
264234
$htmlPattern = $constraint->getHtmlPattern();
265235

@@ -271,7 +241,7 @@ public function guessPatternForConstraint(Constraint $constraint)
271241
case 'Symfony\Component\Validator\Constraints\Min':
272242
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
273243

274-
case 'Symfony\Component\Validator\Constraints\Range':
244+
case 'Symfony\Component\Validator\Constraints\Size':
275245
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
276246

277247
case 'Symfony\Component\Validator\Constraints\Type':

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ CHANGELOG
55
-----
66

77
* added support for `ctype_*` assertions in `TypeValidator`
8-
* added a Range validator for numeric values
9-
* added a Size validator for string & collections
108
* improved the ImageValidator with min width, max width, min height, and max height constraints
119
* added support for MIME with wildcard in FileValidator
1210
* changed Collection validator to add "missing" and "extra" errors to
@@ -23,3 +21,5 @@ CHANGELOG
2321
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
2422
recursively anymore by default. `Valid` contains a new property `deep`
2523
which enables the BC behavior.
24+
* added MinCount and MaxCount constraint
25+
* deprecated the Size constraint

src/Symfony/Component/Validator/Constraints/Range.php renamed to src/Symfony/Component/Validator/Constraints/MaxCount.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@
1818
*
1919
* @api
2020
*/
21-
class Range extends Constraint
21+
class MaxCount extends Constraint
2222
{
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;
23+
public $message = 'This collection should contain {{ limit }} elements or less.';
24+
public $limit;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function getDefaultOption()
30+
{
31+
return 'limit';
32+
}
2833

2934
/**
3035
* {@inheritDoc}
3136
*/
3237
public function getRequiredOptions()
3338
{
34-
return array('min', 'max');
39+
return array('limit');
3540
}
3641
}

src/Symfony/Component/Validator/Constraints/RangeValidator.php renamed to src/Symfony/Component/Validator/Constraints/MaxCountValidator.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* @author Bernhard Schussek <bschussek@gmail.com>
19-
*
20-
* @api
2120
*/
22-
class RangeValidator extends ConstraintValidator
21+
class MaxCountValidator extends ConstraintValidator
2322
{
2423
/**
2524
* Checks if the passed value is valid.
2625
*
2726
* @param mixed $value The value that should be validated
2827
* @param Constraint $constraint The constraint for the validation
2928
*
30-
* @return Boolean Whether or not the value is valid
31-
*
3229
* @api
3330
*/
3431
public function validate($value, Constraint $constraint)
@@ -37,28 +34,17 @@ public function validate($value, Constraint $constraint)
3734
return;
3835
}
3936

40-
if (!is_numeric($value)) {
41-
$this->context->addViolation($constraint->invalidMessage, array(
42-
'{{ value }}' => $value,
43-
));
44-
45-
return;
37+
if (!is_array($value) && !$value instanceof \Countable) {
38+
throw new UnexpectedTypeException($value, 'array or \Countable');
4639
}
4740

48-
if ($value > $constraint->max) {
49-
$this->context->addViolation($constraint->maxMessage, array(
50-
'{{ value }}' => $value,
51-
'{{ limit }}' => $constraint->max,
52-
));
53-
54-
return;
55-
}
41+
$count = count($value);
5642

57-
if ($value < $constraint->min) {
58-
$this->context->addViolation($constraint->minMessage, array(
59-
'{{ value }}' => $value,
60-
'{{ limit }}' => $constraint->min,
61-
));
43+
if ($count > $constraint->limit) {
44+
$this->context->addViolation($constraint->message, array(
45+
'{{ count }}' => $count,
46+
'{{ limit }}' => $constraint->limit,
47+
), $value, (int) $constraint->limit);
6248
}
6349
}
6450
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 MinCount extends Constraint
22+
{
23+
public $message = 'This collection should contain {{ limit }} elements or more.';
24+
public $limit;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function getDefaultOption()
30+
{
31+
return 'limit';
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getRequiredOptions()
38+
{
39+
return array('limit');
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Bernhard Schussek <bschussek@gmail.com>
20+
*/
21+
class MinCountValidator extends ConstraintValidator
22+
{
23+
/**
24+
* Checks if the passed value is valid.
25+
*
26+
* @param mixed $value The value that should be validated
27+
* @param Constraint $constraint The constraint for the validation
28+
*
29+
* @throws UnexpectedTypeException If the given value is no array or \Countable.
30+
*/
31+
public function validate($value, Constraint $constraint)
32+
{
33+
if (null === $value) {
34+
return;
35+
}
36+
37+
if (!is_array($value) && !$value instanceof \Countable) {
38+
throw new UnexpectedTypeException($value, 'array or \Countable');
39+
}
40+
41+
$count = count($value);
42+
43+
if ($count < $constraint->limit) {
44+
$this->context->addViolation($constraint->message, array(
45+
'{{ count }}' => $count,
46+
'{{ limit }}' => $constraint->limit,
47+
), $value, (int) $constraint->limit);
48+
}
49+
}
50+
}

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

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,22 @@
1717
* @Annotation
1818
*
1919
* @api
20+
*
21+
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
2022
*/
2123
class Size extends Constraint
2224
{
23-
const TYPE_STRING = 'string';
24-
const TYPE_COLLECTION = 'collection';
25-
26-
public $minMessage;
27-
public $maxMessage;
28-
public $exactMessage;
29-
public $type;
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';
3028
public $min;
3129
public $max;
32-
public $charset = 'UTF-8';
33-
34-
private $stringMinMessage = 'This value is too short. It should have {{ limit }} characters or more.';
35-
private $stringMaxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
36-
private $stringExactMessage = 'This value should have exactly {{ limit }} characters.';
37-
38-
private $collectionMinMessage = 'This collection should contain {{ limit }} elements or more.';
39-
private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.';
40-
private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.';
4130

42-
public function getMinMessage($type)
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function getRequiredOptions()
4335
{
44-
if (null !== $this->minMessage) {
45-
return $this->minMessage;
46-
}
47-
48-
switch ($type) {
49-
case static::TYPE_STRING:
50-
return $this->stringMinMessage;
51-
case static::TYPE_COLLECTION:
52-
return $this->collectionMinMessage;
53-
default:
54-
throw new \InvalidArgumentException('Invalid type specified.');
55-
}
56-
}
57-
58-
public function getMaxMessage($type)
59-
{
60-
if (null !== $this->maxMessage) {
61-
return $this->maxMessage;
62-
}
63-
64-
switch ($type) {
65-
case static::TYPE_STRING:
66-
return $this->stringMaxMessage;
67-
case static::TYPE_COLLECTION:
68-
return $this->collectionMaxMessage;
69-
default:
70-
throw new \InvalidArgumentException('Invalid type specified.');
71-
}
72-
}
73-
74-
public function getExactMessage($type)
75-
{
76-
if (null !== $this->exactMessage) {
77-
return $this->exactMessage;
78-
}
79-
80-
switch ($type) {
81-
case static::TYPE_STRING:
82-
return $this->stringExactMessage;
83-
case static::TYPE_COLLECTION:
84-
return $this->collectionExactMessage;
85-
default:
86-
throw new \InvalidArgumentException('Invalid type specified.');
87-
}
36+
return array('min', 'max');
8837
}
8938
}

0 commit comments

Comments
 (0)
0