diff --git a/src/HAB/Pica/Record/Field.php b/src/HAB/Pica/Record/Field.php index 9f012aa7e81df066a3cc184438ea280cac735eda..6cdbd650af18093fde8b60aa6952c433c2fe2c0f 100644 --- a/src/HAB/Pica/Record/Field.php +++ b/src/HAB/Pica/Record/Field.php @@ -262,6 +262,21 @@ class Field 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. * diff --git a/tests/unit-tests/src/HAB/Pica/Record/FieldTest.php b/tests/unit-tests/src/HAB/Pica/Record/FieldTest.php index ae5e5f3c5b9055c938a5f858cfc11348112c6f26..6fc57c773a19777ff84d6c92ef2d8a582ba89fd2 100644 --- a/tests/unit-tests/src/HAB/Pica/Record/FieldTest.php +++ b/tests/unit-tests/src/HAB/Pica/Record/FieldTest.php @@ -130,6 +130,17 @@ class FieldTest extends PHPUnit_FrameWork_TestCase $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 */