Skip to content

Commit

Permalink
determine read/write from the number of func arguments
Browse files Browse the repository at this point in the history
rather than whether the value is null. Prevents problems with for
example:

	$inst->config('setthis', 'some value'); // or the default
	...
    $something = $couldBeNull;
	$inst->config('setthis', $something);

which would be pretty confusing.

Also add the possibility to delete a config key - same use case/example
as above
  • Loading branch information
AD7six committed Mar 25, 2014
1 parent eaf1114 commit 680be10
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/Core/InstanceConfigTrait.php
Expand Up @@ -67,7 +67,7 @@ public function config($key = null, $value = null) {
$this->_config = $this->_defaultConfig;
}

if ($value !== null || is_array($key)) {
if (is_array($key) || func_num_args() === 2) {
return $this->_configWrite($key, $value);
}

Expand Down Expand Up @@ -120,14 +120,19 @@ protected function _configWrite($key, $value = null) {
return;
}

if ($value === null) {
return $this->_configDelete($key);
}

if (strpos($key, '.') === false) {
$this->_config[$key] = $value;
return;
}

$update =& $this->_config;
$stack = explode('.', $key);

foreach (explode('.', $key) as $k) {
foreach ($stack as $k) {
if (!is_array($update)) {
throw new Error\Exception(sprintf('Cannot set %s value', $key));
}
Expand All @@ -142,4 +147,38 @@ protected function _configWrite($key, $value = null) {
$update = $value;
}

/**
* Delete a single config key
*
* @param string $key
* @return void
*/
protected function _configDelete($key) {
if (strpos($key, '.') === false) {
unset($this->_config[$key]);
return;
}

$update =& $this->_config;
$stack = explode('.', $key);
$length = count($stack);

foreach ($stack as $i => $k) {
if (!is_array($update)) {
throw new Error\Exception(sprintf('Cannot unset %s value', $key));
}

if (!isset($update[$k])) {
break;
}

if ($i === $length - 2) {
unset($update[$k]);
break;
}

$update =& $update[$k];
}
}

}
55 changes: 55 additions & 0 deletions tests/TestCase/Core/InstanceConfigTrait.php
Expand Up @@ -285,4 +285,59 @@ public function testReadOnlyConfig() {
$object->config('throw.me', 'an exception');
}

/**
* testDeleteSimple
*
* @return void
*/
public function testDeleteSimple() {
$this->object->config('foo', null);
$this->assertNull(
$this->object->config('foo'),
'setting a new key to null should have no effect'
);

$this->object->config('some', null);
$this->assertNull(
$this->object->config('some'),
'should delete the existing value'
);

$this->assertSame(
[
'a' => ['nested' => 'value'],
],
$this->object->config(),
'deleted keys should not be present'
);

}

/**
* testDeleteNested
*
* @return void
*/
public function testDeleteNested() {
$this->object->config('new.foo', null);
$this->assertNull(
$this->object->config('new.foo'),
'setting a new key to null should have no effect'
);

$this->object->config('a.nested', null);
$this->assertNull(
$this->object->config('a.nested'),
'should delete the existing value'
);

$this->assertSame(
[
'some' => 'string',
],
$this->object->config(),
'deleted keys should not be present'
);
}

}

0 comments on commit 680be10

Please sign in to comment.