8000 [Validator] Reverted the changes done to the Size constraint in 3a5e8… · symfony/symfony@d661837 · GitHub
[go: up one dir, main page]

Skip to content

Commit d661837

Browse files
committed
[Validator] Reverted the changes done to the Size constraint in 3a5e84f
1 parent d84b689 commit d661837

File tree

16 files changed

+97
-510
lines changed

16 files changed

+97
-510
lines changed

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

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,6 @@ 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':
170162
case 'Symfony\Component\Validator\Constraints\Max':
171163
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
@@ -211,12 +203,6 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
211203
case 'Symfony\Component\Validator\Constraints\MaxLength':
212204
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
213205

214-
case 'Symfony\Component\Validator\Constraints\Size':
215-
if ('string' === $constraint->type && null !== $constraint->max) {
216-
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
217-
}
218-
break;
219-
220206
case 'Symfony\Component\Validator\Constraints\Type':
221207
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
222208
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
@@ -225,6 +211,9 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
225211

226212
case 'Symfony\Component\Validator\Constraints\Max':
227213
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
214+
215+
case 'Symfony\Component\Validator\Constraints\Size':
216+
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
228217
}
229218
}
230219

@@ -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,6 +241,9 @@ 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

244+
case 'Symfony\Component\Validator\Constraints\Size':
245+
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
246+
274247
case 'Symfony\Component\Validator\Constraints\Type':
275248
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
276249
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ CHANGELOG
55
-----
66

77
* added support for `ctype_*` assertions in `TypeValidator`
8-
* added a Size validator for string & collections
98
* improved the ImageValidator with min width, max width, min height, and max height constraints
109
* added support for MIME with wildcard in FileValidator
1110
* changed Collection validator to add "missing" and "extra" errors to

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

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,17 @@
2020
*/
2121
class Size extends Constraint
2222
{
23-
const TYPE_STRING = 'string';
24-
const TYPE_COLLECTION = 'collection';
25-
26-
public $minMessage;
27-
public $maxMessage;
28-
public $exactMessage;
29-
public $type;
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';
3026
public $min;
3127
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 F438 }} elements or more.';
39-
private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.';
40-
private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.';
41-
42-
public function getMinMessage($type)
43-
{
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-
}
6328

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)
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function getRequiredOptions()
7533
{
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-
}
34+
return array('min', 'max');
8835
}
8936
}

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

Lines changed: 27 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -13,151 +13,52 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1816

17+
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*
20+
* @api
21+
*/
1922
class SizeValidator extends ConstraintValidator
2023
{
2124
/**
22-
* {@inheritDoc}
25+
* Checks if the passed value is valid.
26+
*
27+
* @param mixed $value The value that should be validated
28+
* @param Constraint $constraint The constraint for the validation
29+
*
30+
* @return Boolean Whether or not the value is valid
31+
*
32+
* @api
2333
*/
2434
public function validate($value, Constraint $constraint)
25-
{
26-
if (null === $constraint->min && null === $constraint->max) {
27-
throw new ConstraintDefinitionException(
28-
'Either "min" or "max" must be specified on constraint Size'
29-
);
30-
}
31-
32-
if (null === $constraint->type) {
33-
$type = $this->guessType($value);
34-
} else {
35-
$type = $constraint->type;
36-
}
37-
38-
switch ($type) {
39-
case Size::TYPE_STRING:
40-
return $this->validateString($value, $constraint);
41-
case Size::TYPE_COLLECTION:
42-
return $this->validateCollection($value, $constraint);
43-
default:
44-
throw new ConstraintDefinitionException(sprintf(
45-
'The "type" on constraint Size must be either "%s" or "%s", "%s" given.',
46-
Size::TYPE_STRING,
47-
Size::TYPE_COLLECTION,
48-
$type
49-
));
50-
}
51-
52-
}
53-
54-
private function validateString($value, Constraint $constraint)
55-
{
56-
if (null === $value || '' === $value) {
57-
return;
58-
}
59-
60-
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
61-
throw new UnexpectedTypeException($value, 'string, scalar or object with __toString()');
62-
}
63-
< 325D /div>
64-
$value = (string) $value;
65-
66-
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
67-
$length = grapheme_strlen($value);
68-
} elseif (function_exists('mb_strlen')) {
69-
$length = mb_strlen($value, $constraint->charset);
70-
} else {
71-
$length = strlen($value);
72-
}
73-
74-
$this->validateSize(
75-
$constraint,
76-
$length,
77-
Size::TYPE_STRING,
78-
array('{{ value }}' => $value)
79-
);
80-
}
81-
82-
private function validateCollection($value, Constraint $constraint)
8335
{
8436
if (null === $value) {
8537
return;
8638
}
8739

88-
if (!is_array($value) && !$value instanceof \Countable) {
89-
throw new UnexpectedTypeException($value, 'array or Countable');
90-
}
91-
92-
$count = count($value);
93-
94-
$this->validateSize(
95-
$constraint,
96-
$count,
97-
Size::TYPE_COLLECTION,
98-
array('{{ count }}' => $count)
99-
);
100-
}
101-
102-
private function validateSize(Constraint $constraint, $size, $type, array $parameters)
103-
{
104-
if ($constraint->min == $constraint->max && $size != $constraint->max) {
105-
$this->context->addViolation(
106-
$constraint->getExactMessage($type),
107-
array_merge(array('{{ limit }}' => $constraint->max), $parameters),
108-
null,
109-
(int) $constraint->max
110-
);
40+
if (!is_numeric($value)) {
41+
$this->context->addViolation($constraint->invalidMessage, array(
42+
'{{ value }}' => $value,
43+
));
11144

11245
return;
11346
}
11447

115-
if (null !== $constraint->max && $size > $constraint->max) {
116-
$this->context->addViolation(
117-
$constraint->getMaxMessage($type),
118-
array_merge(array('{{ limit }}' => $constraint->max), $parameters),
119-
null,
120-
(int) $constraint->max
121-
);
48+
if ($value > $constraint->max) {
49+
$this->context->addViolation($constraint->maxMessage, array(
50+
'{{ value }}' => $value,
51+
'{{ limit }}' => $constraint->max,
52+
));
12253

12354
return;
12455
}
12556

126-
if (null !== $constraint->min && $size < $constraint->min) {
127-
$this->context->addViolation(
128-
$constraint->getMinMessage($type),
129-
array_merge(array('{{ limit }}' => $constraint->min), $parameters),
130-
null,
131-
(int) $constraint->min
132-
);
57+
if ($value < $constraint->min) {
58+
$this->context->addViolation($constraint->minMessage, array(
59+
'{{ value }}' => $value,
60+
'{{ limit }}' => $constraint->min,
61+
));
13362
}
13463
}
135-
136-
private function guessType($value)
137-
{
138-
if (null === $value || is_scalar($value)) {
139-
return Size::TYPE_STRING;
140-
}
141-
142-
if (is_object($value) && method_exists($value, '__toString')) {
143-
if ($value instanceof \Countable) {
144-
throw new \RuntimeException(
145-
'The "type" must be specified on constraint Size because the '.
146-
'validator is not able to guess it since the value is an object '.
147-
'implementing both the __toString() method and the Countable '.
148-
'interface.'
149-
);
150-
}
151-
152-
return Size::TYPE_STRING;
153-
}
154-
155-
if (is_array($value) || $value instanceof \Countable) {
156-
return Size::TYPE_COLLECTION;
157-
}
158-
159-
throw new UnexpectedTypeException(
160-
$value, 'scalar, string, array, Countable or object with __toString()'
161-
);
162-
}
16364
}

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<source>This collection should contain {{ limit }} elements or less.</source>
211211
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
212212
</trans-unit>
213-
<trans-unit id="56">
214-
<source>This collection should contain exactly {{ limit }} elements.</source>
215-
<target>This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.</target>
216-
</trans-unit>
217213
</body>
218214
</file>
219215
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<source>This collection should contain {{ limit }} elements or less.</source>
211211
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
212212
</trans-unit>
213-
<trans-unit id="56">
214-
<source>This collection should contain exactly {{ limit }} elements.</source>
215-
<target>Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments.</target>
216-
</trans-unit>
217213
</body>
218214
</file>
219215
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.it.xlf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<source>This collection should contain {{ limit }} elements or less.</source>
211211
<target>Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi.</target>
212212
</trans-unit>
213-
<trans-unit id="56">
214-
<source>This collection should contain exactly {{ limit }} elements.</source>
215-
<target>Questa collezione dovrebbe contenere esattamente {{ limit }} elemento.|Questa collezione dovrebbe contenere esattamente {{ limit }} elementi.</target>
216-
</trans-unit>
217213
</body>
218214
</file>
219215
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<source>This collection should contain {{ limit }} elements or less.</source>
211211
<target>Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten.</target>
212212
</trans-unit>
213-
<trans-unit id="56">
214-
<source>This collection should contain exactly {{ limit }} elements.</source>
215-
<target>Deze collectie moet exact {{ limit }} element bevatten.|Deze collectie moet exact {{ limit }} elementen bevatten.</target>
216-
</trans-unit>
217213
</body>
218214
</file>
219215
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@
210210
<source>This collection should contain {{ limit }} elements or less.</source>
211211
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
212212
</trans-unit>
213-
<trans-unit id="56">
214-
<source>This collection should contain exactly {{ limit }} elements.</source>
215-
<target>Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos.</target>
216-
</trans-unit>
217213
</body>
218214
</file>
219-
</xliff>
215+
</xliff>

0 commit comments

Comments
 (0)
0