Skip to content

Commit

Permalink
Add support for default value to InstanceConfigTrait::getConfig().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 5, 2017
1 parent b096744 commit 47f2a3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Core/InstanceConfigTrait.php
Expand Up @@ -103,17 +103,26 @@ public function setConfig($key, $value = null, $merge = true)
* $this->getConfig('some.nested.key');
* ```
*
* Reading with default value:
*
* ```
* $this->getConfig('some-key', 'default-value');
* ```
*
* @param string|null $key The key to get or null for the whole config.
* @param mixed $default The return value when the key does not exist.
* @return mixed Config value being read.
*/
public function getConfig($key = null)
public function getConfig($key = null, $default = null)
{
if (!$this->_configInitialized) {
$this->_config = $this->_defaultConfig;
$this->_configInitialized = true;
}

return $this->_configRead($key);
$return = $this->_configRead($key);

return $return === null ? $default : $return;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Core/InstanceConfigTraitTest.php
Expand Up @@ -150,6 +150,24 @@ public function testGetDot()
);
}

/**
* testGetDefault
*
* @return void
*/
public function testGetDefault()
{
$this->assertSame(
'default',
$this->object->getConfig('nonexistent', 'default')
);

$this->assertSame(
'my-default',
$this->object->getConfig('nested.nonexistent', 'my-default')
);
}

/**
* testSetSimple
*
Expand Down

0 comments on commit 47f2a3c

Please sign in to comment.