Skip to content

Commit

Permalink
feature #52954 [Validator] Add list and associative_array types t…
Browse files Browse the repository at this point in the history
…o `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
  • Loading branch information
fabpot committed Dec 20, 2023
2 parents 0f46e33 + 5ab4068 commit 6f1f88f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `list` and `associative_array` types to `Type` constraint

7.0
---

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/TypeValidator.php
Expand Up @@ -36,6 +36,8 @@ class TypeValidator extends ConstraintValidator
'string' => 'is_string',
'scalar' => 'is_scalar',
'array' => 'is_array',
'list' => 'is_array && array_is_list',
'associative_array' => 'is_array && !array_is_list',
'iterable' => 'is_iterable',
'countable' => 'is_countable',
'callable' => 'is_callable',
Expand Down Expand Up @@ -73,6 +75,8 @@ public function validate(mixed $value, Constraint $constraint): void
'finite-float' => \is_float($value) && is_finite($value),
'finite-number' => \is_int($value) || \is_float($value) && is_finite($value),
'number' => \is_int($value) || \is_float($value) && !is_nan($value),
'list' => \is_array($value) && array_is_list($value),
'associative_array' => \is_array($value) && !array_is_list($value),
default => self::VALIDATION_FUNCTIONS[$type]($value),
}) {
return;
Expand Down
Expand Up @@ -97,6 +97,10 @@ public static function getValidValues()
[1.5, 'finite-number'],
['12345', 'string'],
[[], 'array'],
[[], 'list'],
[[1, 2, 3], 'list'],
[['abc' => 1], 'associative_array'],
[[1 => 1], 'associative_array'],
[$object, 'object'],
[$object, 'stdClass'],
[$file, 'resource'],
Expand Down Expand Up @@ -166,6 +170,12 @@ public static function getInvalidValues()
[$file, 'float', 'resource'],
[$file, 'string', 'resource'],
[$file, 'object', 'resource'],
[[1 => 1], 'list', 'array'],
[['abc' => 1], 'list', 'array'],
['abcd1', 'list', '"abcd1"'],
[[], 'associative_array', 'array'],
[[1, 2, 3], 'associative_array', 'array'],
['abcd1', 'associative_array', '"abcd1"'],
['12a34', 'digit', '"12a34"'],
['1a#23', 'alnum', '"1a#23"'],
['abcd1', 'alpha', '"abcd1"'],
Expand Down

0 comments on commit 6f1f88f

Please sign in to comment.