Skip to content
Permalink
Browse files

Add support for default value to InstanceConfigTrait::getConfig().

  • Loading branch information...
ADmad committed Aug 5, 2017
1 parent b096744 commit 47f2a3cff90ef9f92dcc4a6c7b5286fd40f4f7c8
Showing with 29 additions and 2 deletions.
  1. +11 −2 src/Core/InstanceConfigTrait.php
  2. +18 −0 tests/TestCase/Core/InstanceConfigTraitTest.php
@@ -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;
}
/**
@@ -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
*

0 comments on commit 47f2a3c

Please sign in to comment.
You can’t perform that action at this time.