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

Skip to content

Commit 6f1f88f

Browse files
committed
feature #52954 [Validator] Add list and associative_array types to Type constraint (Florian Hermann)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Validator] Add `list` and `associative_array` types to `Type` constraint | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT Hello! This PR aims to add support for `list` type in the `Type`constraint. This is done by using the *new* PHP 8.1 function `array_is_list`, after checking that the value is an array with `is_array` function. Here is an example of use: ```php #[Assert\Type('list')] private $value; ``` I consider doing an other PR to add `associative_array` type aswell. Or if you think it's ok, I can also add it in an other commit on this one. --- UPDATE After fabpot comment, I added an other commit to support `associative_array` type aswell : ```php #[Assert\Type('associative_array')] private $value; ``` An `associative_array` is an array that is not a list. Moreover, this means that an empty array will not be considered as an `associative_array`. Best regards Commits ------- 5ab4068 [Validator] Add `list` and `associative_array` types to `Type` constraint
2 parents 0f46e33 + 5ab4068 commit 6f1f88f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add `list` and `associative_array` types to `Type` constraint
8+
49
7.0
510
---
611

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

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class TypeValidator extends ConstraintValidator
3636
'string' => 'is_string',
3737
'scalar' => 'is_scalar',
3838
'array' => 'is_array',
39+
'list' => 'is_array && array_is_list',
40+
'associative_array' => 'is_array && !array_is_list',
3941
'iterable' => 'is_iterable',
4042
'countable' => 'is_countable',
4143
'callable' => 'is_callable',
@@ -73,6 +75,8 @@ public function validate(mixed $value, Constraint $constraint): void
7375
'finite-float' => \is_float($value) && is_finite($value),
7476
'finite-number' => \is_int($value) || \is_float($value) && is_finite($value),
7577
'number' => \is_int($value) || \is_float($value) && !is_nan($value),
78+
'list' => \is_array($value) && array_is_list($value),
79+
'associative_array' => \is_array($value) && !array_is_list($value),
7680
default => self::VALIDATION_FUNCTIONS[$type]($value),
7781
}) {
7882
return;

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

+10
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public static function getValidValues()
9797
[1.5, 'finite-number'],
9898
['12345', 'string'],
9999
[[], 'array'],
100+
[[], 'list'],
101+
[[1, 2, 3], 'list'],
102+
[['abc' => 1], 'associative_array'],
103+
[[1 => 1], 'associative_array'],
100104
[$object, 'object'],
101105
[$object, 'stdClass'],
102106
[$file, 'resource'],
@@ -166,6 +170,12 @@ public static function getInvalidValues()
166170
[$file, 'float', 'resource'],
167171
[$file, 'string', 'resource'],
168172
[$file, 'object', 'resource'],
173+
[[1 => 1], 'list', 'array'],
174+
[['abc' => 1], 'list', 'array'],
175+
['abcd1', 'list', '"abcd1"'],
176+
[[], 'associative_array', 'array'],
177+
[[1, 2, 3], 'associative_array', 'array'],
178+
['abcd1', 'associative_array', '"abcd1"'],
169179
['12a34', 'digit', '"12a34"'],
170180
['1a#23', 'alnum', '"1a#23"'],
171181
['abcd1', 'alpha', '"abcd1"'],

0 commit comments

Comments
 (0)