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

Record: New function getFirstMatchingField

Returns the first of all fields that are matched by a field selector.
parent bb84e9e4
No related branches found
No related tags found
No related merge requests found
...@@ -229,6 +229,24 @@ abstract class Record { ...@@ -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. * Finalize the clone() operation.
* *
......
...@@ -38,6 +38,13 @@ class RecordTest extends \PHPUnit_FrameWork_TestCase { ...@@ -38,6 +38,13 @@ class RecordTest extends \PHPUnit_FrameWork_TestCase {
$this->assertInstanceOf('HAB\Pica\Record\AuthorityRecord', $record); $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@'));
}
/// ///
/** /**
......
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