File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
7
7
## [ Unreleased]
8
8
9
+ ### Added
10
+
11
+ - Added new method ` Field::asLineMarc() ` to return a line mode Marc string
12
+ representation of the field.
13
+
9
14
### Fixed
10
15
11
16
- Fixed the ` Subject::getParts() ` method.
Original file line number Diff line number Diff line change @@ -98,6 +98,36 @@ protected function toString($codes, $options = [])
98
98
return $ this ->clean (implode ($ glue , $ this ->getSubfieldValues ($ codes )), $ options );
99
99
}
100
100
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
+
101
131
/**
102
132
* Return the data value of the *first* subfield with a given code.
103
133
*
Original file line number Diff line number Diff line change @@ -84,4 +84,27 @@ public function testId()
84
84
json_encode (['id ' => $ record ->id ])
85
85
);
86
86
}
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
+ }
87
110
}
You can’t perform that action at this time.
0 commit comments