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

Skip to content

[Validator] Add list and associative_array types to Type constraint #52954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2023

Conversation

hermann8u
Copy link
Contributor

@hermann8u hermann8u commented Dec 8, 2023

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 Typeconstraint.

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:

#[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 :

#[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

@fabpot
Copy link
Member

fabpot commented Dec 13, 2023

Let's add associative_array support in the same PR as this is closely related.

@hermann8u hermann8u force-pushed the validator/type/list branch 2 times, most recently from 710571f to ecb57d7 Compare December 13, 2023 10:25
@OskarStark
Copy link
Contributor

Please update the PR title + description to reflect the additional option, thanks

@hermann8u hermann8u changed the title [Validator] Add list type to Type constraint [Validator] Add list and associative_array types to Type constraint Dec 14, 2023
@fabpot fabpot force-pushed the validator/type/list branch from ecb57d7 to 5ab4068 Compare December 20, 2023 07:16
@fabpot
Copy link
Member

fabpot commented Dec 20, 2023

Thank you @hermann8u.

@ro0NL
Copy link
Contributor

ro0NL commented Dec 20, 2023

in php any array is associative ;)

@ro0NL
Copy link
Contributor

ro0NL commented Dec 21, 2023

@hermann8u i fail to see what usecase would allow for [1 => some], but not [0 => some]

@hermann8u
Copy link
Contributor Author

@ro0NL I don't know, maybe when 1 is an id for example? I'm currently working on an other PR that add a constraint to validate array keys that could be used in association with this one.

@hermann8u hermann8u deleted the validator/type/list branch December 21, 2023 16:51
@hermann8u
Copy link
Contributor Author

@ro0NL Ok I got your point finally. What you mean is that list are also associative_array?

@fabpot fabpot mentioned this pull request May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants