diff --git a/src/AbstractOptions.php b/src/AbstractOptions.php index 8099c75ff..0e68c29e1 100644 --- a/src/AbstractOptions.php +++ b/src/AbstractOptions.php @@ -22,6 +22,8 @@ abstract class AbstractOptions implements ParameterObjectInterface protected $__strictMode__ = true; /** + * Constructor + * * @param array|Traversable|null $options */ public function __construct($options = null) @@ -32,22 +34,29 @@ public function __construct($options = null) } /** - * @param array|Traversable $options + * Set one or more configuration properties + * + * @param array|Traversable|AbstractOptions $options * @throws Exception\InvalidArgumentException * @return AbstractOptions Provides fluent interface */ public function setFromArray($options) { + if ($options instanceof self) { + $options = $options->toArray(); + } + if (!is_array($options) && !$options instanceof Traversable) { throw new Exception\InvalidArgumentException(sprintf( - 'Parameter provided to %s must be an array or Traversable', - __METHOD__ + 'Parameter provided to %s must be an %s, %s or %s', + __METHOD__, 'array', 'Traversable', 'Zend\Stdlib\AbstractOptions' )); } foreach ($options as $key => $value) { $this->__set($key, $value); } + return $this; } @@ -72,6 +81,8 @@ public function toArray() } /** + * Set a configuration property + * * @see ParameterObject::__set() * @param string $key * @param mixed $value @@ -94,6 +105,8 @@ public function __set($key, $value) } /** + * Get a configuration property + * * @see ParameterObject::__get() * @param string $key * @throws Exception\BadMethodCallException @@ -114,6 +127,7 @@ public function __get($key) } /** + * Test if a configuration property is null * @see ParameterObject::__isset() * @param string $key * @return bool @@ -124,6 +138,8 @@ public function __isset($key) } /** + * Set a configuration property to NULL + * * @see ParameterObject::__unset() * @param string $key * @throws Exception\InvalidArgumentException diff --git a/test/OptionsTest.php b/test/OptionsTest.php index d073a432b..51a4f429a 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -32,6 +32,13 @@ public function testConstructionWithTraversable() $this->assertEquals(1, $options->test_field); } + public function testConstructionWithOptions() + { + $options = new TestOptions(new TestOptions(array('test_field' => 1))); + + $this->assertEquals(1, $options->test_field); + } + public function testInvalidFieldThrowsException() { $this->setExpectedException('BadMethodCallException');