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

Skip to content

[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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ public function offsetGet(mixed $option, bool $triggerDeprecation = true): mixed
private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes = null, int $level = 0): bool
{
$type = trim($type);
$allowedTypes = $this->splitOutsideParenthesis($type);
$allowedTypes = $this->splitTypes($type);
if (\count($allowedTypes) > 1) {
foreach ($allowedTypes as $allowedType) {
if ($this->verifyTypes($allowedType, $value)) {
Expand All @@ -1162,17 +1162,29 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
return $this->verifyTypes(substr($type, 1, -1), $value, $invalidTypes, $level);
}

if (\is_array($value) && str_ends_with($type, '[]')) {
$type = substr($type, 0, -2);
$valid = true;
if (\is_array($value)) {
if (str_starts_with($type, 'array<') && str_ends_with($type, '>')) {
$types = $this->splitTypes(substr($type, 6, -1), ',');
$allowedValueType = $types[1] ?? $types[0];
$allowedKeyType = isset($types[1]) ? $types[0] : null;
} elseif (str_ends_with($type, '[]')) {
$allowedValueType = substr($type, 0, -2);
}

if (isset($allowedValueType)) {
$valid = true;
foreach ($value as $key => $val) {
if (isset($allowedKeyType) && !$this->verifyTypes($allowedKeyType, $key, $invalidTypes, $level + 1)) {
$valid = false;
}

foreach ($value as $val) {
if (!$this->verifyTypes($type, $val, $invalidTypes, $level + 1)) {
$valid = false;
if (!$this->verifyTypes($allowedValueType, $val, $invalidTypes, $level + 1)) {
$valid = false;
}
}
}

return $valid;
return $valid;
}
}

if (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) {
Expand All @@ -1189,27 +1201,31 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
/**
* @return list<string>
*/
private function splitOutsideParenthesis(string $type): array
private function splitTypes(string $type, string $separator = '|'): array
{
$parts = [];
$currentPart = '';
$parenthesisLevel = 0;
$arrayLevel = 0;

$typeLength = \strlen($type);
for ($i = 0; $i < $typeLength; ++$i) {
$char = $type[$i];
if ($separator === $char && 0 === $parenthesisLevel && 0 === $arrayLevel) {
$parts[] = $currentPart;
$currentPart = '';
continue;
}

$currentPart .= $char;
if ('(' === $char) {
++$parenthesisLevel;
} elseif (')' === $char) {
--$parenthesisLevel;
}

if ('|' === $char && 0 === $parenthesisLevel) {
$parts[] = $currentPart;
$currentPart = '';
} else {
$currentPart .= $char;
} elseif ('<' === $char) {
++$arrayLevel;
} elseif ('>' === $char) {
--$arrayLevel;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
Copy link
Member

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)[]


$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');
Expand All @@ -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>');
Copy link
Member

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)

$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>');
$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>');

$this->expectException(InvalidOptionsException::class);
$this->resolver->resolve(['foo' => ['bar', 'baz']]);
}

public function testResolveTypedArrayWithUnion()
{
$this->resolver->setDefined('foo');
Expand All @@ -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>');
Copy link
Member

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)

$options = $this->resolver->resolve(['foo' => ['bar', 1]]);

$this->assertSame(['foo' => ['bar', 1]], $options);
}

public function testFailIfSetAllowedTypesFromLazyOption()
{
$this->expectException(AccessException::class);
Expand Down Expand Up @@ -1963,6 +2020,24 @@ public function testNestedArrays()
]));
}

public function testNestedArrays2()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'array<array<int>>');
Copy link
Member

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)

Copy link
Contributor Author

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)


$this->assertSame([
'foo' => [
[
1, 2,
],
],
], $this->resolver->resolve([
'foo' => [
[1, 2],
],
]));
}

public function testNestedArraysWithUnions()
{
$this->resolver->setDefined('foo');
Expand All @@ -1983,6 +2058,26 @@ public function testNestedArraysWithUnions()
]));
}

public function testNestedArraysWithUnions2()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'array<int|float|array<int|float>>');
Copy link
Member

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)


$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');
Expand Down
Loading