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

Skip to content
Merged
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
58 changes: 56 additions & 2 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,28 @@ public function offsetGet(mixed $option, bool $triggerDeprecation = true): mixed
return $value;
}

private function verifyTypes(string $type, mixed $value, array &$invalidTypes, int $level = 0): bool
private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes = null, int $level = 0): bool
{
$allowedTypes = $this->splitOutsideParenthesis($type);
if (\count($allowedTypes) > 1) {
foreach ($allowedTypes as $allowedType) {
if ($this->verifyTypes($allowedType, $value)) {
return true;
}
}

if (\is_array($invalidTypes) && (!$invalidTypes || $level > 0)) {
$invalidTypes[get_debug_type($value)] = true;
}

return false;
}

$type = $allowedTypes[0];
if (str_starts_with($type, '(') && str_ends_with($type, ')')) {
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;
Expand All @@ -1158,13 +1178,47 @@ private function verifyTypes(string $type, mixed $value, array &$invalidTypes, i
return true;
}

if (!$invalidTypes || $level > 0) {
if (\is_array($invalidTypes) && (!$invalidTypes || $level > 0)) {
$invalidTypes[get_debug_type($value)] = true;
}

return false;
}

/**
* @return list<string>
*/
private function splitOutsideParenthesis(string $type): array
{
$parts = [];
$currentPart = '';
$parenthesisLevel = 0;

$typeLength = \strlen($type);
for ($i = 0; $i < $typeLength; ++$i) {
$char = $type[$i];

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

if ('|' === $char && 0 === $parenthesisLevel) {
$parts[] = $currentPart;
$currentPart = '';
} else {
$currentPart .= $char;
}
}

if ('' !== $currentPart) {
$parts[] = $currentPart;
}

return $parts;
}

/**
* Returns whether a resolved option with the given name exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,44 @@ public function testSetAllowedTypesFailsIfUnknownOption()
$this->resolver->setAllowedTypes('foo', 'string');
}

public function testResolveTypedWithUnion()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string|int');

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

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

public function testResolveTypedWithUnionOfClasse()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', \DateTime::class.'|'.\DateTimeImmutable::class);

$datetime = new \DateTime();
$options = $this->resolver->resolve(['foo' => $datetime]);
$this->assertSame(['foo' => $datetime], $options);

$datetime = new \DateTimeImmutable();
$options = $this->resolver->resolve(['foo' => $datetime]);
$this->assertSame(['foo' => $datetime], $options);
}

public function testResolveTypedWithUnionOfArray()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', '(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 @@ -787,6 +825,15 @@ public function testResolveTypedArray()
$this->assertSame(['foo' => ['bar', 'baz']], $options);
}

public function testResolveTypedArrayWithUnion()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', '(string|int)[]');
$options = $this->resolver->resolve(['foo' => ['bar', 1]]);

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

public function testFailIfSetAllowedTypesFromLazyOption()
{
$this->expectException(AccessException::class);
Expand Down Expand Up @@ -878,6 +925,7 @@ public static function provideInvalidTypes()
[[null], ['string[]', 'string'], 'The option "option" with value array is expected to be of type "string[]" or "string", but one of the elements is of type "null".'],
[['string', null], ['string[]', 'string'], 'The option "option" with value array is expected to be of type "string[]" or "string", but one of the elements is of type "null".'],
[[\stdClass::class], ['string'], 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
[['foo', 12], '(string|bool)[]', 'The option "option" with value array is expected to be of type "(string|bool)[]", but one of the elements is of type "int".'],
];
}

Expand Down Expand Up @@ -1903,6 +1951,26 @@ public function testNestedArrays()
]));
}

public function testNestedArraysWithUnions()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', '(int|float|(int|float)[])[]');

$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