Skip to content

Commit 7a71b3e

Browse files
committed
[Validator] Improve TypeValidator to handle array of types
1 parent 90326e6 commit 7a71b3e

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,42 @@ 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;
36+
$types = [];
4037

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;
38+
if (\is_array($constraint->type)) {
39+
$types = $constraint->type;
40+
} else {
41+
$types[] = $constraint->type;
42+
}
43+
44+
foreach ($types as $type) {
45+
if ($this->isValid($value, $type)) {
46+
return;
47+
}
4748
}
4849

4950
$this->context->buildViolation($constraint->message)
5051
->setParameter('{{ value }}', $this->formatValue($value))
51-
->setParameter('{{ type }}', $constraint->type)
52+
->setParameter('{{ type }}', implode(', ', $types))
5253
->setCode(Type::INVALID_TYPE_ERROR)
5354
->addViolation();
5455
}
56+
57+
private function isValid($value, string $type): bool
58+
{
59+
$type = strtolower($type);
60+
$type = 'boolean' == $type ? 'bool' : $type;
61+
$isFunction = 'is_'.$type;
62+
$ctypeFunction = 'ctype_'.$type;
63+
64+
if (\function_exists($isFunction) && $isFunction($value)) {
65+
return true;
66+
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
67+
return true;
68+
} elseif ($value instanceof $type) {
69+
return true;
70+
}
71+
72+
return false;
73+
}
5574
}

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