[OptionsResolver] fixed bugs concerning required options
This commit is contained in:
parent
1bfcff4fab
commit
104dcf251d
@ -19,6 +19,7 @@ use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
|||||||
* Helper for merging default and concrete option values.
|
* Helper for merging default and concrete option values.
|
||||||
*
|
*
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
* @author Tobias Schultze <http://tobion.de>
|
||||||
*/
|
*/
|
||||||
class OptionsResolver
|
class OptionsResolver
|
||||||
{
|
{
|
||||||
@ -35,7 +36,7 @@ class OptionsResolver
|
|||||||
private $knownOptions = array();
|
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
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $requiredOptions = array();
|
private $requiredOptions = array();
|
||||||
@ -71,6 +72,7 @@ class OptionsResolver
|
|||||||
foreach ($defaultValues as $option => $value) {
|
foreach ($defaultValues as $option => $value) {
|
||||||
$this->defaultOptions->overload($option, $value);
|
$this->defaultOptions->overload($option, $value);
|
||||||
$this->knownOptions[$option] = true;
|
$this->knownOptions[$option] = true;
|
||||||
|
unset($this->requiredOptions[$option]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -97,6 +99,7 @@ class OptionsResolver
|
|||||||
foreach ($defaultValues as $option => $value) {
|
foreach ($defaultValues as $option => $value) {
|
||||||
$this->defaultOptions->set($option, $value);
|
$this->defaultOptions->set($option, $value);
|
||||||
$this->knownOptions[$option] = true;
|
$this->knownOptions[$option] = true;
|
||||||
|
unset($this->requiredOptions[$option]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -105,10 +108,12 @@ class OptionsResolver
|
|||||||
/**
|
/**
|
||||||
* Sets optional options.
|
* Sets optional options.
|
||||||
*
|
*
|
||||||
* This method is identical to `setDefaults`, only that no default values
|
* This method declares a valid option names without setting default values for
|
||||||
* are configured for the options. If these options are not passed to
|
* them. If these options are not passed to {@link resolve()} and no default has
|
||||||
* resolve(), they will be missing in the final options array. This can be
|
* been set for them, they will be missing in the final options array. This can
|
||||||
* helpful if you want to determine whether an option has been set or not.
|
* 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.
|
* @param array $optionNames A list of option names.
|
||||||
*
|
*
|
||||||
@ -132,7 +137,8 @@ class OptionsResolver
|
|||||||
/**
|
/**
|
||||||
* Sets required options.
|
* 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.
|
* @param array $optionNames A list of option names.
|
||||||
*
|
*
|
||||||
@ -148,7 +154,10 @@ class OptionsResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->knownOptions[$option] = true;
|
$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;
|
return $this;
|
||||||
@ -163,12 +172,13 @@ class OptionsResolver
|
|||||||
*
|
*
|
||||||
* @return OptionsResolver The resolver instance.
|
* @return OptionsResolver The resolver instance.
|
||||||
*
|
*
|
||||||
* @throws InvalidOptionsException If an option has not been defined for
|
* @throws InvalidOptionsException If an option has not been defined
|
||||||
* which an allowed value is set.
|
* (see {@link isKnown()}) for which
|
||||||
|
* an allowed value is set.
|
||||||
*/
|
*/
|
||||||
public function setAllowedValues(array $allowedValues)
|
public function setAllowedValues(array $allowedValues)
|
||||||
{
|
{
|
||||||
$this->validateOptionNames($allowedValues);
|
$this->validateOptionsExistence($allowedValues);
|
||||||
|
|
||||||
$this->allowedValues = array_replace($this->allowedValues, $allowedValues);
|
$this->allowedValues = array_replace($this->allowedValues, $allowedValues);
|
||||||
|
|
||||||
@ -191,7 +201,7 @@ class OptionsResolver
|
|||||||
*/
|
*/
|
||||||
public function addAllowedValues(array $allowedValues)
|
public function addAllowedValues(array $allowedValues)
|
||||||
{
|
{
|
||||||
$this->validateOptionNames($allowedValues);
|
$this->validateOptionsExistence($allowedValues);
|
||||||
|
|
||||||
$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
|
$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
|
||||||
|
|
||||||
@ -224,7 +234,7 @@ class OptionsResolver
|
|||||||
*/
|
*/
|
||||||
public function isRequired($option)
|
public function isRequired($option)
|
||||||
{
|
{
|
||||||
return isset($this->requiredOptions[$option]) && !isset($this->defaultOptions[$option]);
|
return isset($this->requiredOptions[$option]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -243,7 +253,8 @@ class OptionsResolver
|
|||||||
*/
|
*/
|
||||||
public function resolve(array $options)
|
public function resolve(array $options)
|
||||||
{
|
{
|
||||||
$this->validateOptionNames($options);
|
$this->validateOptionsExistence($options);
|
||||||
|
$this->validateOptionsCompleteness($options);
|
||||||
|
|
||||||
// Make sure this method can be called multiple times
|
// Make sure this method can be called multiple times
|
||||||
$combinedOptions = clone $this->defaultOptions;
|
$combinedOptions = clone $this->defaultOptions;
|
||||||
@ -266,15 +277,13 @@ class OptionsResolver
|
|||||||
* Validates that the given option names exist and throws an exception
|
* Validates that the given option names exist and throws an exception
|
||||||
* otherwise.
|
* 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
|
* @throws InvalidOptionsException If any of the options has not been defined.
|
||||||
* defined.
|
|
||||||
* @throws MissingOptionsException If a required option is missing.
|
|
||||||
*/
|
*/
|
||||||
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) {
|
if (count($diff) > 0) {
|
||||||
ksort($this->knownOptions);
|
ksort($this->knownOptions);
|
||||||
@ -286,8 +295,19 @@ class OptionsResolver
|
|||||||
implode('", "', array_keys($this->knownOptions))
|
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) {
|
if (count($diff) > 0) {
|
||||||
ksort($diff);
|
ksort($diff);
|
||||||
@ -305,8 +325,8 @@ class OptionsResolver
|
|||||||
*
|
*
|
||||||
* @param array $options A list of option values.
|
* @param array $options A list of option values.
|
||||||
*
|
*
|
||||||
* @throws InvalidOptionsException If any of the values does not match the
|
* @throws InvalidOptionsException If any of the values does not match the
|
||||||
* allowed values of the option.
|
* allowed values of the option.
|
||||||
*/
|
*/
|
||||||
private function validateOptionValues(array $options)
|
private function validateOptionValues(array $options)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user