-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -828,6 +828,18 @@ public function testResolveTypedWithUnionOfArray() | |
$this->assertSame(['foo' => [1, true]], $options); | ||
} | ||
|
||
public function testResolveTypedWithUnionOfArray2() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string|int>|array<bool|int>'); | ||
|
||
$options = $this->resolver->resolve(['foo' => [1, '1']]); | ||
$this->assertSame(['foo' => [1, '1']], $options); | ||
|
||
$options = $this->resolver->resolve(['foo' => [1, true]]); | ||
$this->assertSame(['foo' => [1, true]], $options); | ||
} | ||
|
||
public function testResolveTypedArray() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
|
@@ -837,6 +849,42 @@ public function testResolveTypedArray() | |
$this->assertSame(['foo' => ['bar', 'baz']], $options); | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. same as |
||
$options = $this->resolver->resolve(['foo' => ['bar', 'baz']]); | ||
|
||
$this->assertSame(['foo' => ['bar', 'baz']], $options); | ||
} | ||
|
||
public function testResolveTypedArrayWithKeys() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<int, string>'); | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$options = $this->resolver->resolve(['foo' => ['bar', 'baz']]); | ||
|
||
$this->assertSame(['foo' => ['bar', 'baz']], $options); | ||
} | ||
|
||
public function testResolveTypedArrayWithStringKeys() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string, string>'); | ||
$options = $this->resolver->resolve(['foo' => ['bar' => 'bar', 'baz' => 'baz']]); | ||
|
||
$this->assertSame(['foo' => ['bar' => 'bar', 'baz' => 'baz']], $options); | ||
} | ||
|
||
public function testResolveTypedArrayWithInvalidKeys() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
$this->resolver->setAllowedTypes('foo', 'array<string, string>'); | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$this->expectException(InvalidOptionsException::class); | ||
$this->resolver->resolve(['foo' => ['bar', 'baz']]); | ||
} | ||
|
||
public function testResolveTypedArrayWithUnion() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
|
@@ -846,6 +894,15 @@ public function testResolveTypedArrayWithUnion() | |
$this->assertSame(['foo' => ['bar', 1]], $options); | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. same as |
||
$options = $this->resolver->resolve(['foo' => ['bar', 1]]); | ||
|
||
$this->assertSame(['foo' => ['bar', 1]], $options); | ||
} | ||
|
||
public function testFailIfSetAllowedTypesFromLazyOption() | ||
{ | ||
$this->expectException(AccessException::class); | ||
|
@@ -1963,6 +2020,24 @@ public function testNestedArrays() | |
])); | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. same as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
|
||
$this->assertSame([ | ||
'foo' => [ | ||
[ | ||
1, 2, | ||
], | ||
], | ||
], $this->resolver->resolve([ | ||
'foo' => [ | ||
[1, 2], | ||
], | ||
])); | ||
} | ||
|
||
public function testNestedArraysWithUnions() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
|
@@ -1983,6 +2058,26 @@ public function testNestedArraysWithUnions() | |
])); | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. same as |
||
|
||
$this->assertEquals([ | ||
'foo' => [ | ||
1, | ||
2.0, | ||
[1, 2.0], | ||
], | ||
], $this->resolver->resolve([ | ||
'foo' => [ | ||
1, | ||
2.0, | ||
[1, 2.0], | ||
], | ||
])); | ||
} | ||
|
||
public function testNested2Arrays() | ||
{ | ||
$this->resolver->setDefined('foo'); | ||
|
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)[]