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

Skip to content

Commit dd55fe5

Browse files
committed
add flags for better handling of extra options
1 parent d54ca59 commit dd55fe5

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

src/Symfony/Component/OptionsResolver/Options.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ interface Options extends \ArrayAccess, \Countable
2121
{
2222
const NONE = 0;
2323
const ALL = 1;
24+
const DEFINED = 2;
25+
const NESTED = 3;
26+
const EXTRA = 4;
2427
}

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -691,20 +691,30 @@ public function addAllowedValuesForAll($optionNames, $allowedValues, $replace =
691691
throw new AccessException('Allowed types cannot be added from a lazy option or normalizer.');
692692
}
693693

694-
if (Options::ALL === $optionNames) {
695-
$this->allowedValuesForAll = $replace
696-
? array_merge($this->allowedValuesForAll, $allowedValues) : $allowedValues;
694+
switch ($optionNames) {
695+
case Options::ALL:
696+
$this->allowedValuesForAll = $replace
697+
? array_merge($this->allowedValuesForAll, $allowedValues) : $allowedValues;
697698

698-
if (Options::NONE === $nested) {
699-
return $this;
700-
}
699+
if (Options::NONE === $nested) {
700+
return $this;
701+
}
701702

702-
$optionNames = $this->getNestedOptions();
703+
$optionNames = $this->getNestedOptions();
704+
break;
705+
case Options::NESTED:
706+
$optionNames = $this->getNestedOptions();
707+
break;
708+
case Options::DEFINED:
709+
$optionNames = $this->getDefinedOptions();
710+
break;
711+
default:
712+
$optionNames = (array) $optionNames;
703713
}
704714

705-
foreach ((array) $optionNames as $option) {
706-
if ($this->isNested($option) && Options::ALL === $nested) {
707-
$this->nested[$option]->addAllowedValuesForAll(Options::ALL, $allowedValues, $replace);
715+
foreach ($optionNames as $option) {
716+
if ($this->isNested($option) && Options::NONE !== $nested) {
717+
$this->nested[$option]->addAllowedValuesForAll($nested, $allowedValues, $replace);
708718
} else {
709719
if ($replace) {
710720
$this->setAllowedValues($option, $allowedValues);
@@ -840,20 +850,30 @@ public function addAllowedTypesForAll($optionNames, $allowedTypes, $replace = fa
840850
throw new AccessException('Allowed types cannot be added from a lazy option or normalizer.');
841851
}
842852

843-
if (Options::ALL === $optionNames) {
844-
$this->allowedTypesForAll = $replace
845-
? array_merge($this->allowedTypesForAll, $allowedTypes) : $allowedTypes;
853+
switch ($optionNames) {
854+
case Options::ALL:
855+
$this->allowedTypesForAll = $replace
856+
? array_merge($this->allowedTypesForAll, $allowedTypes) : $allowedTypes;
846857

847-
if (Options::NONE === $nested) {
848-
return $this;
849-
}
858+
if (Options::NONE === $nested) {
859+
return $this;
860+
}
850861

851-
$optionNames = $this->getNestedOptions();
862+
$optionNames = $this->getNestedOptions();
863+
break;
864+
case Options::NESTED:
865+
$optionNames = $this->getNestedOptions();
866+
break;
867+
case Options::DEFINED:
868+
$optionNames = $this->getDefinedOptions();
869+
break;
870+
default:
871+
$optionNames = (array) $optionNames;
852872
}
853873

854-
foreach ((array) $optionNames as $option) {
855-
if ($this->isNested($option) && Options::ALL === $nested) {
856-
$this->nested[$option]->addAllowedTypesForAll(Options::ALL, $allowedTypes, $replace);
874+
foreach ($optionNames as $option) {
875+
if ($this->isNested($option) && Options::NONE !== $nested) {
876+
$this->nested[$option]->addAllowedTypesForAll($nested, $allowedTypes, $replace);
857877
} else {
858878
if ($replace) {
859879
$this->setAllowedTypes($option, $allowedTypes);
@@ -1090,10 +1110,18 @@ public function offsetGet($option)
10901110
}
10911111

10921112
// Validate the type of the resolved option
1093-
if (isset($this->allowedTypes[$option]) || ($this->allowedTypesForAll && false === $this->isNested($option))) {
1113+
if (isset($this->allowedTypes[$option])
1114+
|| ($this->allowedTypesForAll && false === $this->isNested($option))
1115+
|| $extra = (isset($this->allowedTypes[Options::EXTRA]) && false === $this->isDefined($option))
1116+
) {
10941117
$valid = false;
10951118

1096-
$allowedTypes = isset($this->allowedTypes[$option]) ? $this->allowedTypes[$option] : array();
1119+
if (isset($extra) && $extra && $this->allowExtraOptions) {
1120+
$allowedTypes = $this->allowedTypes[Options::EXTRA];
1121+
} else {
1122+
$allowedTypes = isset($this->allowedTypes[$option]) ? $this->allowedTypes[$option] : [];
1123+
}
1124+
10971125
$allowedTypes = array_unique(array_merge($allowedTypes, $this->allowedTypesForAll));
10981126

10991127
foreach ($allowedTypes as $type) {
@@ -1127,11 +1155,19 @@ public function offsetGet($option)
11271155
}
11281156

11291157
// Validate the value of the resolved option
1130-
if (isset($this->allowedValues[$option]) || ($this->allowedValuesForAll && false === $this->isNested($option))) {
1158+
if (isset($this->allowedValues[$option])
1159+
|| ($this->allowedValuesForAll && false === $this->isNested($option))
1160+
|| $extra = (isset($this->allowedValues[Options::EXTRA]) && false === $this->isDefined($option))
1161+
) {
11311162
$success = false;
11321163
$printableAllowedValues = array();
11331164

1134-
$allowedValues = isset($this->allowedValues[$option]) ? $this->allowedValues[$option] : array();
1165+
if (isset($extra) && $extra && $this->allowExtraOptions) {
1166+
$allowedValues = $this->allowedTypes[Options::EXTRA];
1167+
} else {
1168+
$allowedValues = isset($this->allowedValues[$option]) ? $this->allowedValues[$option] : [];
1169+
}
1170+
11351171
$allowedValues = array_merge($allowedValues, $this->allowedValuesForAll);
11361172

11371173
foreach ($allowedValues as $allowedValue) {

0 commit comments

Comments
 (0)