Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/options-process_array'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -159,4 +159,4 @@ public function __unset($key)
);
}
}
}
}
45 changes: 45 additions & 0 deletions test/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ZendTest\Stdlib;

use ArrayObject,
ZendTest\Stdlib\TestAsset\TestOptions,
ZendTest\Stdlib\TestAsset\TestTraversable,
Zend\Stdlib\Exception\InvalidArgumentException;

class OptionsTest extends \PHPUnit_Framework_TestCase
{
public function testConstructionWithArray()
{
$options = new TestOptions(array('test_field' => 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));

}
}
23 changes: 23 additions & 0 deletions test/TestAsset/TestOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ZendTest\Stdlib\TestAsset;

use Zend\Stdlib\Options;

/**
* Dummy TestOptions used to test Stdlib\Options
*/
class TestOptions extends Options
{
protected $testField;

public function setTestField($value)
{
$this->testField = $value;
}

public function getTestField()
{
return $this->testField;
}
}

0 comments on commit 4c554ee

Please sign in to comment.