Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d359b2

Browse files
committed
feature #31351 [Validator] Improve TypeValidator to handle array of types (jschaedl)
This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Improve TypeValidator to handle array of types | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31330 | License | MIT | Doc PR | tbd. The `@Type` constraint is now able to handle multiple types: ```php /** * @var string|array * @Assert\Type(type={"string", "array"}) */ private $name; ``` and will pass when `$name` is either of type `string` or `array`. Commits ------- c8100f3 [Validator] Improve TypeValidator to handle array of types
2 parents 5d261bc + c8100f3 commit 8d359b2

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added the `compared_value_path` parameter in violations when using any
88
comparison constraint with the `propertyPath` option.
9+
* added support for checking an array of types in `TypeValidator`
910

1011
4.3.0
1112
-----

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)