Skip to content

Commit

Permalink
Let ConfigTrait::processConfig() accept an arbitrarily nested key.. R…
Browse files Browse the repository at this point in the history
…esolves #14
  • Loading branch information
schlessera committed Feb 11, 2016
1 parent 0477bc3 commit 8638e99
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.8] - 2016-02-11
### Added
- The `ConfigTrait::processConfig()` method now accepts one or more additional parameters (can be delimited strings) to start with a Config at a specific level. Useful to include different `vendor\package` levels in a single merged Config file.

## [0.1.7] - 2016-02-11
### Fixed
- The `hasKey()` method doesn't throw an exception, returns `false` instead.
Expand Down Expand Up @@ -50,6 +54,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Initial release to GitHub.

[0.1.8]: https://github.com/brightnucleus/config/compare/v0.1.7...v0.1.8
[0.1.7]: https://github.com/brightnucleus/config/compare/v0.1.6...v0.1.7
[0.1.6]: https://github.com/brightnucleus/config/compare/v0.1.5...v0.1.6
[0.1.5]: https://github.com/brightnucleus/config/compare/v0.1.4...v0.1.5
Expand Down
20 changes: 18 additions & 2 deletions src/ConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace BrightNucleus\Config;

use Exception;
use RuntimeException;
use BrightNucleus\Config\ConfigInterface;

trait ConfigTrait
Expand All @@ -31,9 +33,23 @@ trait ConfigTrait
* @since 0.1.2
*
* @param ConfigInterface $config The Config to process.
* @param string ... List of keys.

This comment has been minimized.

Copy link
@GaryJones

GaryJones Feb 11, 2016

Funky alignment ;-)

This comment has been minimized.

Copy link
@schlessera

schlessera Feb 12, 2016

Author Member

PHPStorm seems to insist on this... :)

* @throws RuntimeException If the arguments could not be parsed into a Config.
*/
protected function processConfig(ConfigInterface $config)
{
if (func_num_args() > 1) {
try {
$keys = func_get_args();
array_shift($keys);
$config = new Config($config->getKey($keys));
} catch (Exception $exception) {
throw new RuntimeException(sprintf(
_('Could not process the config with the arguments "%1$s".'),
print_r(func_get_args(), true)
));
}
}
$this->config = $config;
}

Expand All @@ -52,7 +68,7 @@ protected function hasConfigKey()
{
$keys = func_get_args();

return call_user_func_array([$this->config, 'hasKey'], $keys);
return $this->config->hasKey($keys);
}

/**
Expand All @@ -70,7 +86,7 @@ protected function getConfigKey()
{
$keys = func_get_args();

return call_user_func_array([$this->config, 'getKey'], $keys);
return $this->config->getKey($keys);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/ConfigTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,35 @@ public function testGetConfigKeys()
]));
$this->assertEquals(['testkey1', 'testkey2'], $this->getConfigKeys());
}

/**
* @covers \BrightNucleus\Config\ConfigTrait::processConfig
*/
public function testProcessConfigAllowsPreKeying()
{
$this->processConfig(new Config([
'vendor' => [
'package' => [
'testkey1' => 'testvalue1',
'testkey2' => 'testvalue2',
],
],
]), 'vendor\package');
$this->assertEquals(
['testkey1' => 'testvalue1', 'testkey2' => 'testvalue2'],
$this->getConfigArray()
);
}

/**
* @covers \BrightNucleus\Config\ConfigTrait::processConfig
*/
public function testProcessConfigThrowsException()
{
$this->setExpectedException('RuntimeException', 'Could not process the config with the arguments');
$this->processConfig(new Config([
'testkey1' => 'testvalue1',
'testkey2' => 'testvalue2',
]), 'vendor\package');
}
}

0 comments on commit 8638e99

Please sign in to comment.