From 816565ac5d38ab6bd2a3808651bab6e49bdec34a Mon Sep 17 00:00:00 2001 From: David Maus <maus@hab.de> Date: Sat, 23 Jul 2016 11:30:23 +0200 Subject: [PATCH] New function: Return true if field has subfield * src/HAB/Pica/Record/Field.php (hasSubfield): New function. Return true if field has subfield. --- src/HAB/Pica/Record/Field.php | 15 +++++++++++++++ .../unit-tests/src/HAB/Pica/Record/FieldTest.php | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/HAB/Pica/Record/Field.php b/src/HAB/Pica/Record/Field.php index 9f012aa..6cdbd65 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 ae5e5f3..6fc57c7 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 */ -- GitLab