Skip to content

Commit

Permalink
Better capability remove() method
Browse files Browse the repository at this point in the history
Need to be able to remove individual extension parameters, since, e.g.,
an end-user may want to disable 1 of many parameters via the
capability_ignore parameter.
  • Loading branch information
slusarz committed Jul 17, 2014
1 parent 96a5068 commit 5cf0c35
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
7 changes: 5 additions & 2 deletions framework/Imap_Client/lib/Horde/Imap/Client/Base.php
Expand Up @@ -368,10 +368,13 @@ public function _setInit($key = null, $val = null)
case 'capability':
if ($ci = $this->getParam('capability_ignore')) {
$ignored = array();

foreach ($ci as $val2) {
if ($val->query($val2)) {
$c = explode('=', $val2);

if ($val->query($c[0], isset($c[1]) ? $c[1] : null)) {
$ignored[] = $val2;
$val->remove($val2);
$val->remove($c[0], isset($c[1]) ? $c[1] : null);
}
}

Expand Down
23 changes: 21 additions & 2 deletions framework/Imap_Client/lib/Horde/Imap/Client/Data/Capability.php
Expand Up @@ -64,10 +64,29 @@ public function add($capability, $params = null)
* Remove a capability.
*
* @param string $capability The capability to remove.
* @param string $params A parameter (or array of parameters) to
* remove from the capability.
*/
public function remove($capability)
public function remove($capability, $params = null)
{
unset($this->_data[strtoupper($capability)]);
$capability = strtoupper($capability);

if (is_null($params)) {
unset($this->_data[$capability]);
} elseif (isset($this->_data[$capability])) {
if (!is_array($params)) {
$params = array($params);
}
$params = array_map('strtoupper', $params);

$this->_data[$capability] = is_array($this->_data[$capability])
? array_diff($this->_data[$capability], $params)
: array();

if (empty($this->_data[$capability])) {
unset($this->_data[$capability]);
}
}
}

/**
Expand Down
Expand Up @@ -99,6 +99,21 @@ public function testRemoval()
$c->remove('FOO');

$this->assertFalse($c->query('FOO'));

$c->add('BAR', array('A', 'B', 'C'));
$c->remove('BAR', array('A', 'C'));

$this->assertTrue($c->query('BAR'));
$this->assertFalse($c->query('BAR', 'A'));
$this->assertTrue($c->query('BAR', 'B'));
$this->assertFalse($c->query('BAR', 'C'));

$c->remove('BAR', 'b');

$this->assertFalse($c->query('BAR'));
$this->assertFalse($c->query('BAR', 'A'));
$this->assertFalse($c->query('BAR', 'B'));
$this->assertFalse($c->query('BAR', 'C'));
}

public function testGetParams()
Expand Down

0 comments on commit 5cf0c35

Please sign in to comment.