Skip to content

Commit

Permalink
Merge pull request #2 from talis/TENT-949-MinorMaterialType
Browse files Browse the repository at this point in the history
90% - TENT-949 minor material type
  • Loading branch information
astilla committed Apr 14, 2015
2 parents eb9ab52 + 8bc945f commit c0245b0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
.idea/
vendor/
atlassian-ide-plugin.xml

composer.lock
composer.phar
39 changes: 39 additions & 0 deletions src/OpenURL/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,44 @@ public function toKev($abbr)
}
return(implode("&", $kevs));
}

/**
* Unsets either a single value or all values from a given key.
*
* @param string $key The key whose value(s) we want to unset.
* @param null $value If specified, only removes the gives value from the key otherwise unsets all values.
*/
public function unsetValue($key, $value=null)
{
if ($value == null)
{
if (isset($this->values[$key]))
{
unset($this->values[$key]);
}
}
else
{
if (isset($this->values[$key]))
{
$values = $this->values[$key];
if (is_array($values))
{
$valueKey = array_search($value, $values);
if ($valueKey !== FALSE)
{
unset($this->values[$key][$valueKey]);

// Reindex the array to remove the empty entry...
$this->values[$key] = array_values($this->values[$key]);
}
}
else
{
unset($this->values[$key]);
}
}
}
}
}
?>
48 changes: 48 additions & 0 deletions test/unit/OpenURLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,52 @@ public function testLoadKev() {
$this->assertEquals("info:sid/ebookco.com:bookreader", $ctx->getReferrer()->getIdentifier());
}

public function testEntityUnsetForAllValues()
{
$entity = new \OpenURL\Entity();
$entity->setValue('key1', 'val1');
$entity->setValue('key2', 'val2');

$entity->unsetValue('key2');

$key2Values = $entity->getValue('key2');
$this->assertNull($key2Values);
$this->assertEquals('val1', $entity->getValue('key1'));
$this->assertEquals(array('key1'=>'val1'), $entity->getValues());
}

public function testEntityUnsetValueForSingleValue()
{
$entity = new \OpenURL\Entity();
$entity->setValue('key1', array('val1a', 'val1b'));
$entity->setValue('key2', 'val2');

$entity->unsetValue('key1', 'val1a');

$this->assertEquals(array('key1'=>array('val1b'), 'key2'=>'val2'), $entity->getValues());
}

public function testEntityUnsetValueWithNonExistentValue()
{
$entity = new \OpenURL\Entity();
$entity->setValue('key1', array('val1a', 'val1b'));
$entity->setValue('key2', 'val2');

$entity->unsetValue('key1', 'foo');

$this->assertEquals(array('key1'=>array('val1a', 'val1b'), 'key2'=>'val2'), $entity->getValues());
}

public function testEntityUnsetValueWithNonExistentKey()
{
$entity = new \OpenURL\Entity();
$entity->setValue('key1', 'val1');
$entity->setValue('key2', 'val2');
$entity->unsetValue('keyFoo');

$this->assertEquals(array('key1'=>'val1', 'key2'=>'val2'), $entity->getValues());
}



}

0 comments on commit c0245b0

Please sign in to comment.