Skip to content

Commit 83cb89e

Browse files
committed
[Validator] Improve TypeValidator to handle array of types
1 parent 79128c3 commit 83cb89e

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added support for checking an array of types in `TypeValidator`
8+
49
4.3.0
510
-----
611

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
}

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)