Skip to content

Commit ba20a6d

Browse files
committed
Add method Field::asLineMarc()
1 parent 1fe8408 commit ba20a6d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added new method `Field::asLineMarc()` to return a line mode Marc string
12+
representation of the field.
13+
914
### Fixed
1015

1116
- Fixed the `Subject::getParts()` method.

src/Fields/Field.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ protected function toString($codes, $options = [])
9898
return $this->clean(implode($glue, $this->getSubfieldValues($codes)), $options);
9999
}
100100

101+
/**
102+
* Return a line MARC representation of the field. If the field is deleted,
103+
* null is returned.
104+
*
105+
* @param string $sep Subfield separator character, defaults to '$'
106+
* @param string $blank Blank indicator character, defaults to ' '
107+
* @return string|null.
108+
*/
109+
public function asLineMarc($sep = '$', $blank = ' ')
110+
{
111+
if ($this->field->isEmpty()) {
112+
return null;
113+
}
114+
$subfields = [];
115+
foreach ($this->field->getSubfields() as $sf) {
116+
$subfields[] = $sep . $sf->getCode() . ' ' . $sf->getData();
117+
}
118+
$tag = $this->field->getTag();
119+
$ind1 = $this->field->getIndicator(1);
120+
$ind2 = $this->field->getIndicator(2);
121+
if ($ind1 == ' ') {
122+
$ind1 = $blank;
123+
}
124+
if ($ind2 == ' ') {
125+
$ind2 = $blank;
126+
}
127+
128+
return "${tag} ${ind1}${ind2} " . implode(' ', $subfields);
129+
}
130+
101131
/**
102132
* Return the data value of the *first* subfield with a given code.
103133
*

tests/FieldsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,27 @@ public function testId()
8484
json_encode(['id' => $record->id])
8585
);
8686
}
87+
88+
public function testAsLineMarc()
89+
{
90+
$source = '<?xml version="1.0" encoding="UTF-8" ?>
91+
<record xmlns="http://www.loc.gov/MARC21/slim">
92+
<leader>99999cam a2299999 u 4500</leader>
93+
<controlfield tag="001">98218834x</controlfield>
94+
<datafield tag="020" ind1=" " ind2=" ">
95+
<subfield code="q">h.</subfield>
96+
<subfield code="c">Nkr 98.00</subfield>
97+
</datafield>
98+
</record>';
99+
100+
$record = Record::fromString($source);
101+
$field = $record->isbns[0];
102+
103+
$this->assertEquals('020 $q h. $c Nkr 98.00', $field->asLineMarc());
104+
$this->assertEquals('020 $$q h. $$c Nkr 98.00', $field->asLineMarc('$$'));
105+
$this->assertEquals('020 ## $$q h. $$c Nkr 98.00', $field->asLineMarc('$$', '#'));
106+
107+
$field->delete();
108+
$this->assertNull($field->asLineMarc());
109+
}
87110
}

0 commit comments

Comments
 (0)