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

Commit 4c554ee

Browse files
committed
Merge branch 'hotfix/options-process_array'

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

src/Options.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,31 @@
3434
abstract class Options implements ParameterObject
3535
{
3636
/**
37-
* @param array|Traversable|null $config
37+
* @param array|Traversable|null $config
3838
* @return Options
3939
* @throws Exception\InvalidArgumentException
4040
*/
4141
public function __construct($config = null)
4242
{
43-
if (!is_null($config)) {
44-
if (is_array($config) || $config instanceof Traversable) {
45-
$this->processArray($config);
46-
} else {
47-
throw new Exception\InvalidArgumentException(
48-
'Parameter to \Zend\Stdlib\Options\'s '
49-
. 'constructor must be an array or implement the '
50-
. 'Traversable interface'
51-
);
52-
}
43+
if (is_null($config)) {
44+
return;
5345
}
46+
$this->processArray($config);
5447
}
5548

5649
/**
57-
* @param array $config
50+
* @param array|Traversable $config
5851
* @return void
5952
*/
60-
protected function processArray(array $config)
53+
protected function processArray($config)
6154
{
55+
if (!is_array($config) && !$config instanceof Traversable) {
56+
throw new Exception\InvalidArgumentException(sprintf(
57+
'Parameter provided to %s must be an array or Traversable',
58+
__METHOD__
59+
));
60+
}
61+
6262
foreach ($config as $key => $value) {
6363
$setter = $this->assembleSetterNameFromConfigKey($key);
6464
$this->{$setter}($value);
@@ -159,4 +159,4 @@ public function __unset($key)
159159
);
160160
}
161161
}
162-
}
162+
}

test/OptionsTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ZendTest\Stdlib;
4+
5+
use ArrayObject,
6+
ZendTest\Stdlib\TestAsset\TestOptions,
7+
ZendTest\Stdlib\TestAsset\TestTraversable,
8+
Zend\Stdlib\Exception\InvalidArgumentException;
9+
10+
class OptionsTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testConstructionWithArray()
13+
{
14+
$options = new TestOptions(array('test_field' => 1));
15+
16+
$this->assertEquals(1, $options->test_field);
17+
}
18+
19+
public function testConstructionWithTraversable()
20+
{
21+
$config = new ArrayObject(array('test_field' => 1));
22+
$options = new TestOptions($config);
23+
24+
$this->assertEquals(1, $options->test_field);
25+
}
26+
27+
public function testConstructionWithNull()
28+
{
29+
try {
30+
$options = new TestOptions(null);
31+
} catch(InvalidArgumentException $e) {
32+
$this->fail("Unexpected InvalidArgumentException raised");
33+
}
34+
}
35+
36+
public function testUnsetting()
37+
{
38+
$options = new TestOptions(array('test_field' => 1));
39+
40+
$this->assertEquals(true, isset($options->test_field));
41+
unset($options->testField);
42+
$this->assertEquals(false, isset($options->test_field));
43+
44+
}
45+
}

test/TestAsset/TestOptions.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ZendTest\Stdlib\TestAsset;
4+
5+
use Zend\Stdlib\Options;
6+
7+
/**
8+
* Dummy TestOptions used to test Stdlib\Options
9+
*/
10+
class TestOptions extends Options
11+
{
12+
protected $testField;
13+
14+
public function setTestField($value)
15+
{
16+
$this->testField = $value;
17+
}
18+
19+
public function getTestField()
20+
{
21+
return $this->testField;
22+
}
23+
}

0 commit comments

Comments
 (0)