8000 Another implementation · symfony/symfony@aca37dc · GitHub
[go: up one dir, main page]

Skip to content

Commit aca37dc

Browse files
Another implementation
1 parent 8f8a568 commit aca37dc

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

+45-22
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,23 @@ public function offsetGet(mixed $option, bool $triggerDeprecation = true): mixed
11411141

11421142
private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes = null, int $level = 0): bool
11431143
{
1144-
if ($this->isEnclosedWithParenthesis($type)) {
1144+
$allowedTypes = $this->splitOutsideParenthesis($type);
1145+
if (\count($allowedTypes) > 1) {
1146+
foreach ($allowedTypes as $allowedType) {
1147+
if ($this->verifyTypes($allowedType, $value)) {
1148+
return true;
1149+
}
1150+
}
1151+
1152+
if (\is_array($invalidTypes) && (!$invalidTypes || $level > 0)) {
1153+
$invalidTypes[get_debug_type($value)] = true;
1154+
}
1155+
1156+
return false;
1157+
}
1158+
1159+
$type = $allowedTypes[0];
1160+
if (str_starts_with($type, '(') && str_ends_with($type, ')')) {
11451161
return $this->verifyTypes(substr($type, 1, -1), $value, $invalidTypes, $level);
11461162
}
11471163

@@ -1158,14 +1174,7 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
11581174
return $valid;
11591175
}
11601176

1161-
if (str_contains($type, '|')) {
1162-
$allowedTypes = preg_split('/\|(?![^\(]*\))/', $type);
1163-
foreach ($allowedTypes as $allowedType) {
1164-
if ($this->verifyTypes($allowedType, $value)) {
1165-
return true;
1166-
}
1167-
}
1168-
} elseif (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) {
1177+
if (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) {
11691178
return true;
11701179
}
11711180

@@ -1176,24 +1185,38 @@ private function verifyTypes(string $type, mixed $value, ?array &$invalidTypes =
11761185
return false;
11771186
}
11781187

1179-
private function isEnclosedWithParenthesis(string $string): bool
1188+
/**
1189+
* @return list<string>
1190+
*/
1191+
private function splitOutsideParenthesis(string $type): array
11801192
{
1181-
if (!str_starts_with($string, '(') || !str_ends_with($string, ')')) {
1182-
return false;
1183-
}
1193+
$parts = [];
1194+
$currentPart = '';
1195+
$parenthesisLevel = 0;
1196+
1197+
$typeLength = strlen($type);
1198+
for ($i = 0; $i < $typeLength; $i++) {
1199+
$char = $type[$i];
1200+
1201+
if ('(' === $char) {
1202+
$parenthesisLevel++;
1203+
} elseif (')' === $char) {
1204+
$parenthesisLevel--;
1205+
}
11841206

1185-
$openedParenthesis = 1;
1186-
$i = 1;
1187-
while (isset($string[$i]) && $openedParenthesis > 0) {
1188-
if ('(' === $string[$i]) {
1189-
$openedParenthesis++;
1190-
} elseif (')' === $string[$i]) {
1191-
$openedParenthesis--;
1207+
if ('|' === $char && 0 === $parenthesisLevel) {
1208+
$parts[] = $currentPart;
1209+
$currentPart = '';
1210+
} else {
1211+
$currentPart .= $char;
11921212
}
1193-
++$i;
11941213
}
11951214

1196-
return $i === \strlen($string);
1215+
if ($currentPart !== '') {
1216+
$parts[] = $currentPart;
1217+
}
1218+
1219+
return $parts;
11971220
}
11981221

11991222
/**

src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ public function testResolveTypedWithUnion()
794794
public function testResolveTypedWithUnionOfArray()
795795
{
796796
$this->resolver->setDefined('foo');
797-
$this->resolver->setAllowedTypes('foo', '((string|int)[])|((bool|int)[])');
797+
$this->resolver->setAllowedTypes('foo', '(string|int)[]|(bool|int)[]');
798798

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

0 commit comments

Comments
 (0)
0