[OptionsResolver] Implemented fluid interface

This commit is contained in:
Bernhard Schussek 2012-05-11 09:41:52 +02:00
parent 95454f5f6b
commit 876fd9ba17
2 changed files with 43 additions and 0 deletions

View File

@ -63,6 +63,8 @@ class OptionsResolver
* *
* - function (Options $options) * - function (Options $options)
* - function (Options $options, $previousValue) * - function (Options $options, $previousValue)
*
* @return OptionsResolver The resolver instance.
*/ */
public function setDefaults(array $defaultValues) public function setDefaults(array $defaultValues)
{ {
@ -70,6 +72,8 @@ class OptionsResolver
$this->defaultOptions[$option] = $value; $this->defaultOptions[$option] = $value;
$this->knownOptions[$option] = true; $this->knownOptions[$option] = true;
} }
return $this;
} }
/** /**
@ -85,6 +89,8 @@ class OptionsResolver
* of the following signature: * of the following signature:
* *
* - function (Options $options) * - function (Options $options)
*
* @return OptionsResolver The resolver instance.
*/ */
public function replaceDefaults(array $defaultValues) public function replaceDefaults(array $defaultValues)
{ {
@ -93,6 +99,8 @@ class OptionsResolver
$this->defaultOptions[$option] = $value; $this->defaultOptions[$option] = $value;
$this->knownOptions[$option] = true; $this->knownOptions[$option] = true;
} }
return $this;
} }
/** /**
@ -105,6 +113,8 @@ class OptionsResolver
* *
* @param array $optionNames A list of option names. * @param array $optionNames A list of option names.
* *
* @return OptionsResolver The resolver instance.
*
* @throws OptionDefinitionException When trying to pass default values. * @throws OptionDefinitionException When trying to pass default values.
*/ */
public function setOptional(array $optionNames) public function setOptional(array $optionNames)
@ -116,6 +126,8 @@ class OptionsResolver
$this->knownOptions[$option] = true; $this->knownOptions[$option] = true;
} }
return $this;
} }
/** /**
@ -125,6 +137,8 @@ class OptionsResolver
* *
* @param array $optionNames A list of option names. * @param array $optionNames A list of option names.
* *
* @return OptionsResolver The resolver instance.
*
* @throws OptionDefinitionException When trying to pass default values. * @throws OptionDefinitionException When trying to pass default values.
*/ */
public function setRequired(array $optionNames) public function setRequired(array $optionNames)
@ -137,6 +151,8 @@ class OptionsResolver
$this->knownOptions[$option] = true; $this->knownOptions[$option] = true;
$this->requiredOptions[$option] = true; $this->requiredOptions[$option] = true;
} }
return $this;
} }
/** /**
@ -146,6 +162,8 @@ class OptionsResolver
* with values acceptable for that option as * with values acceptable for that option as
* values. * values.
* *
* @return OptionsResolver The resolver instance.
*
* @throws InvalidOptionsException If an option has not been defined for * @throws InvalidOptionsException If an option has not been defined for
* which an allowed value is set. * which an allowed value is set.
*/ */
@ -154,6 +172,8 @@ class OptionsResolver
$this->validateOptionNames(array_keys($allowedValues)); $this->validateOptionNames(array_keys($allowedValues));
$this->allowedValues = array_replace($this->allowedValues, $allowedValues); $this->allowedValues = array_replace($this->allowedValues, $allowedValues);
return $this;
} }
/** /**
@ -165,6 +185,8 @@ class OptionsResolver
* with values acceptable for that option as * with values acceptable for that option as
* values. * values.
* *
* @return OptionsResolver The resolver instance.
*
* @throws InvalidOptionsException If an option has not been defined for * @throws InvalidOptionsException If an option has not been defined for
* which an allowed value is set. * which an allowed value is set.
*/ */
@ -173,6 +195,8 @@ class OptionsResolver
$this->validateOptionNames(array_keys($allowedValues)); $this->validateOptionNames(array_keys($allowedValues));
$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues); $this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
return $this;
} }
/** /**

View File

@ -292,4 +292,23 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
'one' => '1', 'one' => '1',
)); ));
} }
public function testFluidInterface()
{
$this->resolver->setDefaults(array('one' => '1'))
->replaceDefaults(array('one' => '2'))
->setAllowedValues(array('one' => array('1', '2')))
->addAllowedValues(array('one' => array('3')))
->setRequired(array('two'))
->setOptional(array('three'));
$options = array(
'two' => '2',
);
$this->assertEquals(array(
'one' => '2',
'two' => '2',
), $this->resolver->resolve($options));
}
} }