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

Add Record::getMaximumOccurrenceOf(), return maximum occurrence value

of fields identified by a tag
parent 5a0b68e9
No related merge requests found
......@@ -164,6 +164,28 @@ abstract class Record {
}
}
/**
* Return the maximum occurrence value of a field.
*
* @throws \InvalidArgumentException Invalid field tag
* @param string $tag Field tag
* @return int|null Maximum occurrence of field or NULL if field does not
* exist
*/
public function getMaximumOccurrenceOf ($tag) {
if (!preg_match(Field::TAG_RE, $tag)) {
throw new \InvalidArgumentException("Invalid field tag: {$tag}");
}
return array_reduce($this->getFields($tag),
function ($maxOccurrence, Field $field) {
if ($field->getOccurrence() > $maxOccurrence || $maxOccurrence === null) {
return $field->getOccurrence();
} else {
return $maxOccurrence;
}
}, null);
}
/**
* Return TRUE if the record is empty.
*
......
......@@ -92,6 +92,15 @@ class LocalRecordTest extends \PHPUnit_FrameWork_TestCase {
$this->assertFalse($r->isEmpty());
}
public function testGetMaximumOccurrenceOf () {
$r = new LocalRecord();
$this->assertNull($r->getMaximumOccurrenceOf('144Z'));
$r->append(new Field('144Z', 0));
$this->assertEquals(0, $r->getMaximumOccurrenceOf('144Z'));
$r->append(new Field('144Z', 10));
$this->assertEquals(10, $r->getMaximumOccurrenceOf('144Z'));
}
///
/**
......@@ -129,4 +138,12 @@ class LocalRecordTest extends \PHPUnit_FrameWork_TestCase {
$r = new LocalRecord();
$r->append(new Field('003@', 0));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetMaximumOccurrenceOfThrowsExceptionOnInvalidFieldTag () {
$r = new LocalRecord();
$r->getMaximumOccurrenceOf('@@@@');
}
}
\ No newline at end of file
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