Skip to content

Commit 8fb1ef6

Browse files
committed
[Validator] Improve TypeValidator to handle array of types
1 parent 371dfc7 commit 8fb1ef6

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ CHANGELOG
1010
* removed support for using the `Email` constraint without `egulias/email-validator`
1111
* removed support for using the `Expression` constraint without `symfony/expression-language`
1212

13+
4.4.0
14+
-----
15+
16+
* added support for checking an array of types in `TypeValidator`
17+
1318
4.3.0
1419
-----
1520

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@ public function validate($value, Constraint $constraint)
3333
return;
3434
}
3535

36-
$type = strtolower($constraint->type);
37-
$type = 'boolean' == $type ? 'bool' : $constraint->type;
38-
$isFunction = 'is_'.$type;
39-
$ctypeFunction = 'ctype_'.$type;
40-
41-
if (\function_exists($isFunction) && $isFunction($value)) {
42-
return;
43-
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
44-
return;
45-
} elseif ($value instanceof $constraint->type) {
46-
return;
36+
$types = (array) $constraint->type;
37+
38+
foreach ($types as $type) {
39+
$type = strtolower($type);
40+
$type = 'boolean' == $type ? 'bool' : $type;
41+
$isFunction = 'is_'.$type;
42+
$ctypeFunction = 'ctype_'.$type;
43+
if (\function_exists($isFunction) && $isFunction($value)) {
44+
return;
45+
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
46+
return;
47+
} elseif ($value instanceof $type) {
48+
return;
49+
}
4750
}
4851

4952
$this->context->buildViolation($constraint->message)
5053
->setParameter('{{ value }}', $this->formatValue($value))
51-
->setParameter('{{ type }}', $constraint->type)
54+
->setParameter('{{ type }}', implode('|', $types))
5255
->setCode(Type::INVALID_TYPE_ERROR)
5356
->addViolation();
5457
}

src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,52 @@ public function getInvalidValues()
163163
];
164164
}
165165

166+
/**
167+
* @dataProvider getValidValuesMultipleTypes
168+
*/
169+
public function testValidValuesMultipleTypes($value, array $types)
170+
{
171+
$constraint = new Type(['type' => $types]);
172+
173+
$this->validator->validate($value, $constraint);
174+
175+
$this->assertNoViolation();
176+
}
177+
178+
public function getValidValuesMultipleTypes()
179+
{
180+
return [
181+
['12345', ['array', 'string']],
182+
[[], ['array', 'string']],
183+
];
184+
}
185+
186+
/**
187+
* @dataProvider getInvalidValuesMultipleTypes
188+
*/
189+
public function testInvalidValuesMultipleTypes($value, $types, $valueAsString)
190+
{
191+
$constraint = new Type([
192+
'type' => $types,
193+
'message' => 'myMessage',
194+
]);
195+
196+
$this->validator->validate($value, $constraint);
197+
198+
$this->buildViolation('myMessage')
199+
->setParameter('{{ value }}', $valueAsString)
200+
->setParameter('{{ type }}', implode('|', $types))
201+
->setCode(Type::INVALID_TYPE_ERROR)
202+
->assertRaised();
203+
}
204+
205+
public function getInvalidValuesMultipleTypes()
206+
{
207+
return [
208+
['12345', ['boolean', 'array'], '"12345"'],
209+
];
210+
}
211+
166212
protected function createFile()
167213
{
168214
if (!static::$file) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy