[OptionsResolver] Made the OptionsResolver clonable

This commit is contained in:
Bernhard Schussek 2012-07-12 18:02:35 +02:00
parent 70307e5648
commit a924dabd57
3 changed files with 31 additions and 2 deletions

View File

@ -67,6 +67,14 @@ class OptionsResolver implements OptionsResolverInterface
$this->defaultOptions = new Options(); $this->defaultOptions = new Options();
} }
/**
* Clones the resolver.
*/
public function __clone()
{
$this->defaultOptions = clone $this->defaultOptions;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -210,7 +218,7 @@ class OptionsResolver implements OptionsResolverInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function resolve(array $options) public function resolve(array $options = array())
{ {
$this->validateOptionsExistence($options); $this->validateOptionsExistence($options);
$this->validateOptionsCompleteness($options); $this->validateOptionsCompleteness($options);

View File

@ -206,5 +206,5 @@ interface OptionsResolverInterface
* @throws Exception\OptionDefinitionException If a cyclic dependency is detected * @throws Exception\OptionDefinitionException If a cyclic dependency is detected
* between two lazy options. * between two lazy options.
*/ */
public function resolve(array $options); public function resolve(array $options = array());
} }

View File

@ -612,4 +612,25 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($options, $this->resolver->resolve($options)); $this->assertEquals($options, $this->resolver->resolve($options));
} }
public function testClone()
{
$this->resolver->setDefaults(array('one' => '1'));
$clone = clone $this->resolver;
// Changes after cloning don't affect each other
$this->resolver->setDefaults(array('two' => '2'));
$clone->setDefaults(array('three' => '3'));
$this->assertEquals(array(
'one' => '1',
'two' => '2',
), $this->resolver->resolve());
$this->assertEquals(array(
'one' => '1',
'three' => '3',
), $clone->resolve());
}
} }