diff --git a/src/php/HAB/Pica/Record/Record.php b/src/php/HAB/Pica/Record/Record.php index 31578074946892b8a7ded12cd178ec577033a2aa..fe41422916097ca839b3fecc14c9678b1ff08534 100644 --- a/src/php/HAB/Pica/Record/Record.php +++ b/src/php/HAB/Pica/Record/Record.php @@ -229,6 +229,24 @@ abstract class Record { } } + /** + * Return the first field that matches a selector. + * + * @see \HAB\Pica\Record\getFields() + * + * @param string $selector Body of regular expression + * @return \HAB\Pica\Record\Field|null The first matching field or NULL if + * no match + */ + public function getFirstMatchingField ($selector) { + $fields = $this->getFields($selector); + if (empty($fields)) { + return null; + } else { + return reset($fields); + } + } + /** * Finalize the clone() operation. * diff --git a/src/tests/unit-tests/php/HAB/Pica/Record/RecordTest.php b/src/tests/unit-tests/php/HAB/Pica/Record/RecordTest.php index ab8123c78e6d654b24d82a635f7c4dffe09f1b77..4c7d03842a8c7d7ecba725142ed52114a9300830 100644 --- a/src/tests/unit-tests/php/HAB/Pica/Record/RecordTest.php +++ b/src/tests/unit-tests/php/HAB/Pica/Record/RecordTest.php @@ -38,6 +38,13 @@ class RecordTest extends \PHPUnit_FrameWork_TestCase { $this->assertInstanceOf('HAB\Pica\Record\AuthorityRecord', $record); } + public function testGetFirstMatchingField () { + $record = new AuthorityRecord(array(new Field('001@', 0), + new Field('001@', 1))); + $this->assertNull($record->getFirstMatchingField('002@/00')); + $this->assertInstanceOf('HAB\Pica\Record\Field', $record->getFirstMatchingField('001@')); + } + /// /**