Skip to content

Commit

Permalink
Add method Field::asLineMarc()
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Jan 15, 2019
1 parent 1fe8408 commit ba20a6d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Added new method `Field::asLineMarc()` to return a line mode Marc string
representation of the field.

### Fixed

- Fixed the `Subject::getParts()` method.
Expand Down
30 changes: 30 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ protected function toString($codes, $options = [])
return $this->clean(implode($glue, $this->getSubfieldValues($codes)), $options);
}

/**
* Return a line MARC representation of the field. If the field is deleted,
* null is returned.
*
* @param string $sep Subfield separator character, defaults to '$'
* @param string $blank Blank indicator character, defaults to ' '
* @return string|null.
*/
public function asLineMarc($sep = '$', $blank = ' ')
{
if ($this->field->isEmpty()) {
return null;
}
$subfields = [];
foreach ($this->field->getSubfields() as $sf) {
$subfields[] = $sep . $sf->getCode() . ' ' . $sf->getData();
}
$tag = $this->field->getTag();
$ind1 = $this->field->getIndicator(1);
$ind2 = $this->field->getIndicator(2);
if ($ind1 == ' ') {
$ind1 = $blank;
}
if ($ind2 == ' ') {
$ind2 = $blank;
}

return "${tag} ${ind1}${ind2} " . implode(' ', $subfields);
}

/**
* Return the data value of the *first* subfield with a given code.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,27 @@ public function testId()
json_encode(['id' => $record->id])
);
}

public function testAsLineMarc()
{
$source = '<?xml version="1.0" encoding="UTF-8" ?>
<record xmlns="http://www.loc.gov/MARC21/slim">
<leader>99999cam a2299999 u 4500</leader>
<controlfield tag="001">98218834x</controlfield>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="q">h.</subfield>
<subfield code="c">Nkr 98.00</subfield>
</datafield>
</record>';

$record = Record::fromString($source);
$field = $record->isbns[0];

$this->assertEquals('020 $q h. $c Nkr 98.00', $field->asLineMarc());
$this->assertEquals('020 $$q h. $$c Nkr 98.00', $field->asLineMarc('$$'));
$this->assertEquals('020 ## $$q h. $$c Nkr 98.00', $field->asLineMarc('$$', '#'));

$field->delete();
$this->assertNull($field->asLineMarc());
}
}

0 comments on commit ba20a6d

Please sign in to comment.