Skip to content

Commit 882871e

Browse files
committed
Add a mechanism to retrieve values several levels deep.. Resolves #7
1 parent 4ac7223 commit 882871e

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [0.1.4] - 2016-02-11
66
### Added
7-
- getAll() and getConfigArray() with corresponding unit tests.
7+
- `AbstractConfig:getAll()` and `ConfigTrait::getConfigArray()`.
8+
- `hasKey()` and `getKey()` now support a list of strings to fetch a value from several levels deep.
9+
- Tests for the above.
10+
11+
### Fixed
12+
- PHP requirement for unit tests was lowered from 5.6 to 5.4.
813

914
## [0.1.3] - 2016-02-10
1015
### Added

src/AbstractConfig.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,64 @@ public function __construct(array $config)
4343
/**
4444
* Check whether the Config has a specific key.
4545
*
46+
* To check a value several levels deep, add the keys for each level as a comma-separated list.
47+
*
4648
* @since 0.1.0
49+
* @since 0.1.4 Accepts list of keys.
4750
*
48-
* @param string $key The key to check the existence for.
51+
* @param string ... List of keys.
4952
* @return bool
5053
*/
51-
public function hasKey($key)
54+
public function hasKey()
5255
{
53-
return array_key_exists($key, (array)$this);
56+
$keys = array_reverse(func_get_args());
57+
58+
$array = $this->getArrayCopy();
59+
while (count($keys) > 0) {
60+
$key = array_pop($keys);
61+
if ( ! array_key_exists($key, $array)) {
62+
return false;
63+
}
64+
$array = $array[$key];
65+
}
66+
67+
return true;
5468
}
5569

5670
/**
5771
* Get the value of a specific key.
5872
*
73+
* To get a value several levels deep, add the keys for each level as a comma-separated list.
74+
*
5975
* @since 0.1.0
76+
* @since 0.1.4 Accepts list of keys.
6077
*
61-
* @param string $key The key to get the value for.
78+
* @param string ... List of keys.
6279
* @return mixed
80+
* @throws BadMethodCallException If no argument was provided.
6381
* @throws OutOfRangeException If an unknown key is requested.
6482
*/
65-
public function getKey($key)
83+
public function getKey()
6684
{
67-
if ( ! $this->hasKey($key)) {
85+
if (func_num_args() < 1) {
86+
throw new BadMethodCallException(_('No configuration was provided to getKey().'));
87+
}
88+
89+
$keys = func_get_args();
90+
91+
if ( ! call_user_func_array([$this, 'hasKey'], $keys)) {
6892
throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'),
69-
(string)$key));
93+
implode('->', $keys)));
94+
}
95+
96+
$keys = array_reverse($keys);
97+
$array = $this->getArrayCopy();
98+
while (count($keys) > 0) {
99+
$key = array_pop($keys);
100+
$array = $array[$key];
70101
}
71102

72-
return $this[$key];
103+
return $array;
73104
}
74105

75106
/**
@@ -81,7 +112,7 @@ public function getKey($key)
81112
*/
82113
public function getAll()
83114
{
84-
return (array)$this;
115+
return $this->getArrayCopy();
85116
}
86117

87118
/**

src/ConfigInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,29 @@ public function getArrayCopy();
4343
/**
4444
* Check whether the Config has a specific key.
4545
*
46-
* @since 0.1.0
46+
* To check a value several levels deep, add the keys for each level as a comma-separated list.
4747
*
48-
* @param string $key The key to check the existence for.
48+
* @since 0.1.0
49+
* @since 0.1.4 Accepts list of keys.
4950
*
51+
* @param string ... List of keys.
5052
* @return bool
5153
*/
52-
public function hasKey($key);
54+
public function hasKey();
5355

5456
/**
5557
* Get the value of a specific key.
5658
*
59+
* To get a value several levels deep, add the keys for each level as a comma-separated list.
60+
*
5761
* @since 0.1.0
62+
* @since 0.1.4 Accepts list of keys.
5863
*
59-
* @param string $key The key to get the value for.
64+
* @param string ... List of keys.
6065
*
6166
* @return mixed
6267
*/
63-
public function getKey($key);
68+
public function getKey();
6469

6570
/**
6671
* Get a (multi-dimensional) array of all the configuration settings.

tests/ConfigTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
3939
'negative_boolean' => false,
4040
];
4141

42+
protected static $test_multi_array = [
43+
'level1' => [
44+
'level2' => [
45+
'level3' => [
46+
'level4_key' => 'level4_value',
47+
],
48+
],
49+
],
50+
];
51+
4252
/**
4353
* Test creation and value retrieval.
4454
*
@@ -153,6 +163,22 @@ public function testHasKey()
153163
$this->assertFalse($config->hasKey('some_other_key'));
154164
}
155165

166+
/**
167+
* @covers BrightNucleus\Config\AbstractConfig::hasKey
168+
*/
169+
public function testHasKeyWithMultipleLevels()
170+
{
171+
$config = new Config(ConfigTest::$test_multi_array);
172+
$this->assertTrue($config->hasKey('level1'));
173+
$this->assertTrue($config->hasKey('level1', 'level2'));
174+
$this->assertTrue($config->hasKey('level1', 'level2', 'level3'));
175+
$this->assertTrue($config->hasKey('level1', 'level2', 'level3', 'level4_key'));
176+
$this->assertFalse($config->hasKey('level2'));
177+
$this->assertFalse($config->hasKey('level1', 'level3'));
178+
$this->assertFalse($config->hasKey('level1', 'level2', 'level4_key'));
179+
$this->assertFalse($config->hasKey('level0', 'level1', 'level2', 'level3', 'level4_key'));
180+
}
181+
156182
/**
157183
* @covers BrightNucleus\Config\AbstractConfig::getKeys
158184
*/
@@ -176,7 +202,19 @@ public function testGetKey()
176202
$this->assertFalse($config->getKey('negative_boolean'));
177203
$this->setExpectedException('OutOfRangeException',
178204
'The configuration key some_other_key does not exist.');
179-
$this->assertFalse($config->getKey('some_other_key'));
205+
$config->getKey('some_other_key');
206+
}
207+
208+
/**
209+
* @covers BrightNucleus\Config\AbstractConfig::getKey
210+
*/
211+
public function testGetKeyWithMultipleLevels()
212+
{
213+
$config = new Config(ConfigTest::$test_multi_array);
214+
$this->assertEquals('level4_value', $config->getKey('level1', 'level2', 'level3', 'level4_key'));
215+
$this->setExpectedException('OutOfRangeException',
216+
'The configuration key level1->level2->level4_key does not exist.');
217+
$config->getKey('level1', 'level2', 'level4_key');
180218
}
181219

182220
/**

0 commit comments

Comments
 (0)