-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[OptionsResolver] Add support for array<>
syntax to validate keys
#59442
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
Conversation
array<>
syntax to validate keys.array<>
syntax to validate keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase needed after the whitespace PR
src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
Outdated
Show resolved
Hide resolved
74dfd0a
to
f971bc0
Compare
Rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great to have! However, I'm not sure adding support for anything other than array<int|string, X>
is a good idea, as a shorter and simpler alternative is already supported
public function testNestedArrays2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<array<int>>'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as int[][]
(shorter and simpler to write/read)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. It's just to prove the second syntax works. (Same for others)
public function testResolveTypedArrayWithUnion2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string|int>'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as (string|int)[]
(more less the same complexity)
public function testResolveTypedArray2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string>'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as string[]
(shorter and simpler to write/read)
public function testResolveTypedWithUnionOfArray2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string|int>|array<bool|int>'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as (string|int)[] | (bool|int)[]
public function testNestedArraysWithUnions2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<int|float|array<int|float>>'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as (int|float|(int|float)[])[]
(more less same complexity)
I don't think it will be necessary, as nested option definitions can currently handle that |
It's debatable: First Secondly, people might prefer one syntax over another, especially when dealing with multiple options with arrays:
Also, if one day we plan to introduce more "types" like non-empty-string or positive-integer, Or do you mean you're ok with keeping the current implementation but removing some tests @yceruto ? |
This PR is mark as "Need works". Friendly ping @yceruto just to be sure you saw my answer I'm unsure about the "work needed" atm since I'd like to support both |
I wonder if at some point it'd be easier to leverage the |
Indeed that would be great. I didn't know such component already existed. |
Only strings or integers are supported anyway, so I'd throw an exception if anything other than I'm okay with full |
It's not only string or integer. You can also allow numeric. So adding a check would add some complexity with no real benefit, because if someone use
for instance, there will be already an exception since keys are not bool. "Expected bool, got int". In the same way that The configuration validation as only interested if the validation was made on the I can add a test with |
@VincentLanglet PHP arrays don't support anything else than integers or strings as array keys. |
The option resolver will be validating existing array-key. |
Makes sense to me. Should we stop building another type parser in this component to look into this instead? |
Maybe we can:
WDYT? |
I agree. Should I close this PR or |
I would suggest reworking this PR according to the proposed plan :) |
I agree too 👍 |
In PHPdoc there is three way to describe an generic array:
string[]
array<string>
The second syntax has the benefit to allows describing the keys (for instance
array<string, string>
) so I'd like to add the support for this syntax in the OptionResolver.Require #59441 first.
The next step would be the support for the
array{foo: string, bar?: int}
syntax, but this might be a little bit more complicated and should be done in another PR anyways.