Skip to content

Commit 6b9e82b

Browse files
committed
Add variadic support to ConfigTrait methods hasConfigKey() & getConfigKey(). See #7
1 parent 882871e commit 6b9e82b

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.1.5] - 2016-02-11
6+
### Added
7+
- `ConfigTrait::hasConfigKey()` and `ConfigTrait::getConfigKey()` now support a list of strings to fetch a value from several levels deep.
8+
- Tests for the above.
9+
510
## [0.1.4] - 2016-02-11
611
### Added
712
- `AbstractConfig:getAll()` and `ConfigTrait::getConfigArray()`.
@@ -36,6 +41,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3641
### Added
3742
- Initial release to GitHub.
3843

44+
[0.1.5]: https://github.com/brightnucleus/config/compare/v0.1.4...v0.1.5
3945
[0.1.4]: https://github.com/brightnucleus/config/compare/v0.1.3...v0.1.4
4046
[0.1.3]: https://github.com/brightnucleus/config/compare/v0.1.2...v0.1.3
4147
[0.1.2]: https://github.com/brightnucleus/config/compare/v0.1.1...v0.1.2

src/ConfigTrait.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,37 @@ protected function processConfig(ConfigInterface $config)
4040
/**
4141
* Check whether the Config has a specific key.
4242
*
43+
* To get a value several levels deep, add the keys for each level as a comma-separated list.
44+
*
4345
* @since 0.1.2
46+
* @since 0.1.5 Accepts list of keys.
4447
*
45-
* @param string $key The key to check.
48+
* @param string ... List of keys.
4649
* @return bool Whether the key is known.
4750
*/
48-
protected function hasConfigKey($key)
51+
protected function hasConfigKey()
4952
{
50-
return $this->config->hasKey($key);
53+
$keys = func_get_args();
54+
55+
return call_user_func_array([$this->config, 'hasKey'], $keys);
5156
}
5257

5358
/**
5459
* Get the Config value for a specific key.
5560
*
61+
* To get a value several levels deep, add the keys for each level as a comma-separated list.
62+
*
5663
* @since 0.1.2
64+
* @since 0.1.5 Accepts list of keys.
5765
*
58-
* @param string $key The key for which to get the value.
66+
* @param string ... List of keys.
5967
* @return mixed Value of the key.
6068
*/
61-
protected function getConfigKey($key)
69+
protected function getConfigKey()
6270
{
63-
return $this->config->getKey($key);
71+
$keys = func_get_args();
72+
73+
return call_user_func_array([$this->config, 'getKey'], $keys);
6474
}
6575

6676
/**

tests/ConfigTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
5252
/**
5353
* Test creation and value retrieval.
5454
*
55-
* @covers BrightNucleus\Config\AbstractConfig::__construct
56-
* @covers BrightNucleus\Config\Config::__construct
55+
* @covers \BrightNucleus\Config\AbstractConfig::__construct
56+
* @covers \BrightNucleus\Config\Config::__construct
5757
*
5858
* @since 1.0.0
5959
*/
@@ -123,8 +123,8 @@ public function configExceptionsDataProvider()
123123
}
124124

125125
/**
126-
* @covers BrightNucleus\Config\Config::__construct
127-
* @covers BrightNucleus\Config\Config::isValid
126+
* @covers \BrightNucleus\Config\Config::__construct
127+
* @covers \BrightNucleus\Config\Config::isValid
128128
*/
129129
public function testValidation()
130130
{
@@ -150,7 +150,7 @@ public function testValidation()
150150
}
151151

152152
/**
153-
* @covers BrightNucleus\Config\AbstractConfig::hasKey
153+
* @covers \BrightNucleus\Config\AbstractConfig::hasKey
154154
*/
155155
public function testHasKey()
156156
{
@@ -164,7 +164,7 @@ public function testHasKey()
164164
}
165165

166166
/**
167-
* @covers BrightNucleus\Config\AbstractConfig::hasKey
167+
* @covers \BrightNucleus\Config\AbstractConfig::hasKey
168168
*/
169169
public function testHasKeyWithMultipleLevels()
170170
{
@@ -180,7 +180,7 @@ public function testHasKeyWithMultipleLevels()
180180
}
181181

182182
/**
183-
* @covers BrightNucleus\Config\AbstractConfig::getKeys
183+
* @covers \BrightNucleus\Config\AbstractConfig::getKeys
184184
*/
185185
public function testGetKeys()
186186
{
@@ -190,7 +190,7 @@ public function testGetKeys()
190190
}
191191

192192
/**
193-
* @covers BrightNucleus\Config\AbstractConfig::getKey
193+
* @covers \BrightNucleus\Config\AbstractConfig::getKey
194194
*/
195195
public function testGetKey()
196196
{
@@ -206,7 +206,7 @@ public function testGetKey()
206206
}
207207

208208
/**
209-
* @covers BrightNucleus\Config\AbstractConfig::getKey
209+
* @covers \BrightNucleus\Config\AbstractConfig::getKey
210210
*/
211211
public function testGetKeyWithMultipleLevels()
212212
{
@@ -218,7 +218,7 @@ public function testGetKeyWithMultipleLevels()
218218
}
219219

220220
/**
221-
* @covers BrightNucleus\Config\AbstractConfig::getAll
221+
* @covers \BrightNucleus\Config\AbstractConfig::getAll
222222
*/
223223
public function testGetAll()
224224
{
@@ -227,10 +227,10 @@ public function testGetAll()
227227
}
228228

229229
/**
230-
* @covers BrightNucleus\Config\Config::__construct
231-
* @covers BrightNucleus\Config\Config::fetchArrayData
232-
* @covers BrightNucleus\Config\Config::resolveOptions
233-
* @covers BrightNucleus\Config\Config::configureOptions
230+
* @covers \BrightNucleus\Config\Config::__construct
231+
* @covers \BrightNucleus\Config\Config::fetchArrayData
232+
* @covers \BrightNucleus\Config\Config::resolveOptions
233+
* @covers \BrightNucleus\Config\Config::configureOptions
234234
*/
235235
public function testConfigFileWithoutDefaults()
236236
{
@@ -252,10 +252,10 @@ public function testConfigFileWithoutDefaults()
252252
}
253253

254254
/**
255-
* @covers BrightNucleus\Config\Config::__construct
256-
* @covers BrightNucleus\Config\Config::fetchArrayData
257-
* @covers BrightNucleus\Config\Config::resolveOptions
258-
* @covers BrightNucleus\Config\Config::configureOptions
255+
* @covers \BrightNucleus\Config\Config::__construct
256+
* @covers \BrightNucleus\Config\Config::fetchArrayData
257+
* @covers \BrightNucleus\Config\Config::resolveOptions
258+
* @covers \BrightNucleus\Config\Config::configureOptions
259259
*/
260260
public function testConfigFileWithMissingKeys()
261261
{
@@ -268,10 +268,10 @@ public function testConfigFileWithMissingKeys()
268268
}
269269

270270
/**
271-
* @covers BrightNucleus\Config\Config::__construct
272-
* @covers BrightNucleus\Config\Config::fetchArrayData
273-
* @covers BrightNucleus\Config\Config::resolveOptions
274-
* @covers BrightNucleus\Config\Config::configureOptions
271+
* @covers \BrightNucleus\Config\Config::__construct
272+
* @covers \BrightNucleus\Config\Config::fetchArrayData
273+
* @covers \BrightNucleus\Config\Config::resolveOptions
274+
* @covers \BrightNucleus\Config\Config::configureOptions
275275
*/
276276
public function testConfigFileWithDefaults()
277277
{

tests/ConfigTraitTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testProcessConfig()
3535
/**
3636
* @covers \BrightNucleus\Config\ConfigTrait::hasConfigKey
3737
*/
38-
public function testHasKey()
38+
public function testHasConfigKey()
3939
{
4040
$this->processConfig(new Config([
4141
'testkey1' => 'testvalue1',
@@ -46,10 +46,28 @@ public function testHasKey()
4646
$this->assertFalse($this->hasConfigKey('testkey3'));
4747
}
4848

49+
/**
50+
* @covers \BrightNucleus\Config\ConfigTrait::hasConfigKey
51+
*/
52+
public function testHasConfigKeyWithMultipleLevels()
53+
{
54+
$this->processConfig(new Config([
55+
'level1' => ['level2' => ['level3' => ['level4_key' => 'level4_value'],],],
56+
]));
57+
$this->assertTrue($this->hasConfigKey('level1'));
58+
$this->assertTrue($this->hasConfigKey('level1', 'level2'));
59+
$this->assertTrue($this->hasConfigKey('level1', 'level2', 'level3'));
60+
$this->assertTrue($this->hasConfigKey('level1', 'level2', 'level3', 'level4_key'));
61+
$this->assertFalse($this->hasConfigKey('level1', 'level2', 'level4_key'));
62+
$this->assertFalse($this->hasConfigKey('level1', 'level3'));
63+
$this->assertFalse($this->hasConfigKey('level2'));
64+
$this->assertFalse($this->hasConfigKey('some_other_key'));
65+
}
66+
4967
/**
5068
* @covers \BrightNucleus\Config\ConfigTrait::getConfigKey
5169
*/
52-
public function testGetKey()
70+
public function testGetConfigKey()
5371
{
5472
$this->processConfig(new Config([
5573
'testkey1' => 'testvalue1',
@@ -61,10 +79,24 @@ public function testGetKey()
6179
$this->getConfigKey('testkey3');
6280
}
6381

82+
/**
83+
* @covers \BrightNucleus\Config\ConfigTrait::getConfigKey
84+
*/
85+
public function testGetConfigKeyWithMultipleLevels()
86+
{
87+
$this->processConfig(new Config([
88+
'level1' => ['level2' => ['level3' => ['level4_key' => 'level4_value'],],],
89+
]));
90+
$this->assertEquals('level4_value', $this->getConfigKey('level1', 'level2', 'level3', 'level4_key'));
91+
$this->setExpectedException('OutOfRangeException',
92+
'The configuration key level1->level2->level4_key does not exist.');
93+
$this->getConfigKey('level1', 'level2', 'level4_key');
94+
}
95+
6496
/**
6597
* @covers \BrightNucleus\Config\ConfigTrait::getConfigArray
6698
*/
67-
public function testGetArray()
99+
public function testGetConfigArray()
68100
{
69101
$this->processConfig(new Config([
70102
'testkey1' => 'testvalue1',
@@ -79,7 +111,7 @@ public function testGetArray()
79111
/**
80112
* @covers \BrightNucleus\Config\ConfigTrait::getConfigKeys
81113
*/
82-
public function testGetKeys()
114+
public function testGetConfigKeys()
83115
{
84116
$this->processConfig(new Config([
85117
'testkey1' => 'testvalue1',

0 commit comments

Comments
 (0)