Skip to content

Commit

Permalink
[OptionsResolver] Implemented fluid interface
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed May 14, 2012
1 parent 95454f5 commit 876fd9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Expand Up @@ -63,13 +63,17 @@ public function __construct()
*
* - function (Options $options)
* - function (Options $options, $previousValue)
*
* @return OptionsResolver The resolver instance.
*/
public function setDefaults(array $defaultValues)
{
foreach ($defaultValues as $option => $value) {
$this->defaultOptions[$option] = $value;
$this->knownOptions[$option] = true;
}

return $this;
}

/**
Expand All @@ -85,6 +89,8 @@ public function setDefaults(array $defaultValues)
* of the following signature:
*
* - function (Options $options)
*
* @return OptionsResolver The resolver instance.
*/
public function replaceDefaults(array $defaultValues)
{
Expand All @@ -93,6 +99,8 @@ public function replaceDefaults(array $defaultValues)
$this->defaultOptions[$option] = $value;
$this->knownOptions[$option] = true;
}

return $this;
}

/**
Expand All @@ -105,6 +113,8 @@ public function replaceDefaults(array $defaultValues)
*
* @param array $optionNames A list of option names.
*
* @return OptionsResolver The resolver instance.
*
* @throws OptionDefinitionException When trying to pass default values.
*/
public function setOptional(array $optionNames)
Expand All @@ -116,6 +126,8 @@ public function setOptional(array $optionNames)

$this->knownOptions[$option] = true;
}

return $this;
}

/**
Expand All @@ -125,6 +137,8 @@ public function setOptional(array $optionNames)
*
* @param array $optionNames A list of option names.
*
* @return OptionsResolver The resolver instance.
*
* @throws OptionDefinitionException When trying to pass default values.
*/
public function setRequired(array $optionNames)
Expand All @@ -137,6 +151,8 @@ public function setRequired(array $optionNames)
$this->knownOptions[$option] = true;
$this->requiredOptions[$option] = true;
}

return $this;
}

/**
Expand All @@ -146,6 +162,8 @@ public function setRequired(array $optionNames)
* with values acceptable for that option as
* values.
*
* @return OptionsResolver The resolver instance.
*
* @throws InvalidOptionsException If an option has not been defined for
* which an allowed value is set.
*/
Expand All @@ -154,6 +172,8 @@ public function setAllowedValues(array $allowedValues)
$this->validateOptionNames(array_keys($allowedValues));

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

return $this;
}

/**
Expand All @@ -165,6 +185,8 @@ public function setAllowedValues(array $allowedValues)
* with values acceptable for that option as
* values.
*
* @return OptionsResolver The resolver instance.
*
* @throws InvalidOptionsException If an option has not been defined for
* which an allowed value is set.
*/
Expand All @@ -173,6 +195,8 @@ public function addAllowedValues(array $allowedValues)
$this->validateOptionNames(array_keys($allowedValues));

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

return $this;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/OptionsResolver/Tests/OptionResolverTest.php
Expand Up @@ -292,4 +292,23 @@ public function testSetOptionalFailsIfDefaultIsPassed()
'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));
}
}

0 comments on commit 876fd9b

Please sign in to comment.