8000 [OptionsResolver] fixed two bugs and applied optimization by Tobion · Pull Request #4388 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[OptionsResolver] fixed two bugs and applied optimization #4388

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

Merged
merged 5 commits into from
May 25, 2012
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[OptionsResolver] fixed bugs concerning required options
  • Loading branch information
Tobion committed May 24, 2012
commit 104dcf251dcfd97a5035b57b9248a6b0c4e258e6
64 changes: 42 additions & 22 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Helper for merging default and concrete option values.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Tobias Schultze <http://tobion.de>
*/
class OptionsResolver
{
Expand All @@ -35,7 +36,7 @@ class OptionsResolver
private $knownOptions = array();

/**
* The options required to be passed to resolve().
* The options without defaults that are required to be passed to resolve().
* @var array
*/
private $requiredOptions = array();
Expand Down Expand Up @@ -71,6 +72,7 @@ public function setDefaults(array $defaultValues)
foreach ($defaultValues as $option => $value) {
$this->defaultOptions->overload($option, $value);
$this->knownOptions[$option] = true;
unset($this->requiredOptions[$option]);
}

return $this;
Expand All @@ -97,6 +99,7 @@ public function replaceDefaults(array $defaultValues)
foreach ($defaultValues as $option => $value) {
$this->defaultOptions->set($option, $value);
$this->knownOptions[$option] = true;
unset($this->requiredOptions[$option]);
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" "> }

return $this;
Expand All @@ -105,10 +108,12 @@ public function replaceDefaults(array $defaultValues)
/**
* Sets optional options.
*
* This method is identical to `setDefaults`, only that no default values
* are configured for the options. If these options are not passed to
* resolve(), they will be missing in the final options array. This can be
* helpful if you want to determine whether an option has been set or not.
* This method declares a valid option names without setting default values for
* them. If these options are not passed to {@link resolve()} and no default has
* been set for them, they will be missing in the final options array. This can
* be helpful if you want to determine whether an option has been set or not
* because otherwise {@link resolve()} would trigger an exception for unknown
* options.
*
* @param array $optionNames A list of option names.
*
Expand All @@ -132,7 +137,8 @@ public function setOptional(array $optionNames)
/**
* Sets required options.
*
* If these options are not passed to resolve(), an exception will be thrown.
* If these options are not passed to resolve() and no default has been set for
* them, an exception will be thrown.
*
* @param array $optionNames A list of option names.
*
Expand All @@ -148,7 +154,10 @@ public function setRequired(array $optionNames)
}

$this->knownOptions[$option] = true;
$this->requiredOptions[$option] = true;
// set as required if no default has been set already
if (!isset($this->defaultOptions[$option])) {
$this->requiredOptions[$option] = true;
}
}

return $this;
Expand All @@ -163,12 +172,13 @@ public function setRequired(array $optionNames)
*
* @return OptionsResolver The resolver instance.
*
* @throws InvalidOptionsException If an option has not been defined for
* which an allowed value is set.
* @throws InvalidOptionsException If an option has not been defined
* (see {@link isKnown()}) for which
* an allowed value is set.
*/
public function setAllowedValues(array $allowedValues)
{
$this->validateOptionNames($allowedValues);
$this->validateOptionsExistence($allowedValues);

$this->allowedValues = array_replace($this->allowedValues, $allowedValues);

Expand All @@ -191,7 +201,7 @@ public function setAllowedValues(array $allowedValues)
*/
public function addAllowedValues(array $allowedValues)
{
$this->validateOptionNames($allowedValues);
$this->validateOptionsExistence($allowedValues);

$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);

Expand Down Expand Up @@ -224,7 +234,7 @@ public function isKnown($option)
*/
public function isRequired($option)
{
return isset($this->requiredOptions[$option]) && !isset($this->defaultOptions[$option]);
return isset($this->requiredOptions[$option]);
}

/**
Expand All @@ -243,7 +253,8 @@ public function isRequired($option)
*/
public function resolve(array $options)
{
$this->validateOptionNames($options);
$this->validateOptionsExistence($options);
$this->validateOptionsCompleteness($options);

// Make sure this method can be called multiple times
$combinedOptions = clone $this->defaultOptions;
Expand All @@ -266,15 +277,13 @@ public function resolve(array $options)
* Validates that the given option names exist and throws an exception
* otherwise.
*
* @param array $optionNames An list of option names as keys.
* @param array $options An list of option names as keys.
*
* @throws InvalidOptionsException If any of the options has not been
* defined.
* @throws MissingOptionsException If a required option is missing.
* @throws InvalidOptionsException If any of the options has not been defined.
*/
private function validateOptionNames(array $optionNames)
private function validateOptionsExistence(array $options)
{
$diff = array_diff_key($optionNames, $this->knownOptions);
$diff = array_diff_key($options, $this->knownOptions);

if (count($diff) > 0) {
ksort($this->knownOptions);
Expand All @@ -286,8 +295,19 @@ private function validateOptionNames(array $optionNames)
implode('", "', array_keys($this->knownOptions))
));
}
}

$diff = array_diff_key($this->requiredOptions, $optionNames);
/**
* Validates that all required options are given and throws an exception
* otherwise.
*
* @param array $options An list of option names as keys.
*
* @throws MissingOptionsException If a required option is missing.
*/
private function validateOptionsCompleteness(array $options)
{
$diff = array_diff_key($this->requiredOptions, $options);

if (count($diff) > 0) {
ksort($diff);
Expand All @@ -305,8 +325,8 @@ private function validateOptionNames(array $optionNames)
*
* @param array $options A list of option values.
*
* @throws InvalidOptionsException If any of the values does not match the
* allowed values of the option.
* @throws InvalidOptionsException If any of the values does not match the
* allowed values of the option.
*/
private function validateOptionValues(array $options)
{
Expand Down
0