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

Skip to content

Extracted OptionsResolver component out of Form #3968

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 12 commits into from
May 15, 2012
Merged
Show file tree
Hide file tree
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] Implemented fluid interface
  • Loading branch information
webmozart committed May 14, 2012
commit 876fd9ba1745e5fcb863cea19ffde26502f61df9
24 changes: 24 additions & 0 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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));
}
}