diff --git a/src/Options.php b/src/Options.php index 4772ea00f..1d9def4fb 100644 --- a/src/Options.php +++ b/src/Options.php @@ -34,31 +34,31 @@ abstract class Options implements ParameterObject { /** - * @param array|Traversable|null $config + * @param array|Traversable|null $config * @return Options * @throws Exception\InvalidArgumentException */ public function __construct($config = null) { - if (!is_null($config)) { - if (is_array($config) || $config instanceof Traversable) { - $this->processArray($config); - } else { - throw new Exception\InvalidArgumentException( - 'Parameter to \Zend\Stdlib\Options\'s ' - . 'constructor must be an array or implement the ' - . 'Traversable interface' - ); - } + if (is_null($config)) { + return; } + $this->processArray($config); } /** - * @param array $config + * @param array|Traversable $config * @return void */ - protected function processArray(array $config) + protected function processArray($config) { + if (!is_array($config) && !$config instanceof Traversable) { + throw new Exception\InvalidArgumentException(sprintf( + 'Parameter provided to %s must be an array or Traversable', + __METHOD__ + )); + } + foreach ($config as $key => $value) { $setter = $this->assembleSetterNameFromConfigKey($key); $this->{$setter}($value); @@ -159,4 +159,4 @@ public function __unset($key) ); } } -} \ No newline at end of file +} diff --git a/test/OptionsTest.php b/test/OptionsTest.php new file mode 100644 index 000000000..2678f24f1 --- /dev/null +++ b/test/OptionsTest.php @@ -0,0 +1,45 @@ + 1)); + + $this->assertEquals(1, $options->test_field); + } + + public function testConstructionWithTraversable() + { + $config = new ArrayObject(array('test_field' => 1)); + $options = new TestOptions($config); + + $this->assertEquals(1, $options->test_field); + } + + public function testConstructionWithNull() + { + try { + $options = new TestOptions(null); + } catch(InvalidArgumentException $e) { + $this->fail("Unexpected InvalidArgumentException raised"); + } + } + + public function testUnsetting() + { + $options = new TestOptions(array('test_field' => 1)); + + $this->assertEquals(true, isset($options->test_field)); + unset($options->testField); + $this->assertEquals(false, isset($options->test_field)); + + } +} diff --git a/test/TestAsset/TestOptions.php b/test/TestAsset/TestOptions.php new file mode 100644 index 000000000..85bcf5f0c --- /dev/null +++ b/test/TestAsset/TestOptions.php @@ -0,0 +1,23 @@ +testField = $value; + } + + public function getTestField() + { + return $this->testField; + } +}