Skip to content
Snippets Groups Projects
Commit 816565ac authored by David Maus's avatar David Maus
Browse files

New function: Return true if field has subfield

* src/HAB/Pica/Record/Field.php (hasSubfield): New function. Return true
  if field has subfield.
parent f670b474
No related branches found
No related tags found
No related merge requests found
...@@ -262,6 +262,21 @@ class Field ...@@ -262,6 +262,21 @@ class Field
return null; return null;
} }
/**
* Return true if a subfield with given code exists.
*
* If optional argument $n return true if the field has subfield
* at the specified position.
*
* @param string $code Subfield code
* @param integer $n Optional zero-based subfield index
* @return boolean
*/
public function hasSubfield ($code, $n = 0)
{
return (boolean)$this->getNthSubfield($code, $n);
}
/** /**
* Return all subfields with the specified code. * Return all subfields with the specified code.
* *
......
...@@ -130,6 +130,17 @@ class FieldTest extends PHPUnit_FrameWork_TestCase ...@@ -130,6 +130,17 @@ class FieldTest extends PHPUnit_FrameWork_TestCase
$this->assertNull($s); $this->assertNull($s);
} }
public function testHasSubfield ()
{
$f = new Field('003@', 0, array(new Subfield('a', 'first a'),
new Subfield('b', 'first b'),
new Subfield('a', 'second a')));
$this->assertTrue($f->hasSubfield('a'));
$this->assertTrue($f->hasSubfield('a', 1));
$this->assertFalse($f->hasSubfield('a', 2));
$this->assertFalse($f->hasSubfield('c'));
}
/** /**
* @depends testSetSubfields * @depends testSetSubfields
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment