|
1 | 1 | # PHP MARCspec parser
|
2 | 2 |
|
3 |
| -PHP based *MARCspec* parser and validator. For currently supported version of **MARCspec - A common MARC record path language** see http://cklee.github.io/marc-spec/marc-spec-4482fbb.html . |
| 3 | +PHP based *MARCspec* parser and validator. |
4 | 4 |
|
| 5 | +For currently supported version of **MARCspec - A common MARC record path language** see http://cklee.github.io/marc-spec/marc-spec-e66931e.html . |
| 6 | + |
| 7 | +# Installation |
| 8 | + |
| 9 | +Installation can be done by using composer or download the [ZIP file](https://github.com/cKlee/php-marc-spec/archive/master.zip). |
5 | 10 | # Usage
|
6 | 11 |
|
7 | 12 | ```php
|
8 | 13 | <?php
|
9 | 14 |
|
10 |
| -require_once "vendor/autoload.php"; |
11 |
| - |
12 |
| -use CK\MARCspec\MARCspec; |
13 |
| - |
14 |
| -// parse MARCspec |
15 |
| -$marcSpec = new MARCspec('020$a{$q[0]~\pbk}{$c/0=\€|$c/0=\$}'); |
16 |
| - |
17 |
| -// accessing properties |
18 |
| -echo $marcSpec['field']['tag']; // '020' |
19 |
| -echo $marcSpec['subfields'][0]['tag']; // 'a' |
20 |
| -echo $firstSubSpec = $marcSpec['subfields'][0]['subSpecs'][0]; // '020$q[0]~\pbk' |
21 |
| -echo $firstSubSpec['rightSubTerm']['comparable'] // 'pbk' |
22 |
| -echo $secondSubSpec = $marcSpec['a']['']['comparable'] // 'pbk' |
23 |
| -print |
24 |
| -$subfields = $marcSpec->getSubfields(); // ['a'=>['tag'=>'a','start'=>0],'b'=>['tag'=>'b','start'=>0],'c'=>['tag'=>'c','start'=>0]] |
25 |
| -$indicator1 = $marcSpec->getIndicator1(); // '1' |
26 |
| -$indicator2 = $marcSpec->getIndicator2(); // '0' |
27 |
| - |
28 |
| -// parse Marc spec |
29 |
| -$marcSpec = new MARCspec("LDR/0-4"); |
30 |
| - |
31 |
| -// get parsed elements |
32 |
| -$fieldTag = $marcSpec->getFieldTag(); // 'LDR' |
33 |
| -$charStart = $marcSpec->getCharStart(); // 0 |
34 |
| -$charEnd = $marcSpec->getCharEnd(); // 4 |
35 |
| -$charLength = $marcSpec->getCharLength(); // 5 |
36 |
| - |
37 |
| -// initialize empty instance |
38 |
| -$marcSpec = new MARCspec; |
39 |
| - |
40 |
| -$marcSpec->setFieldTag('245'); |
41 |
| -$marcSpec->addSubfields('$a$b$e'); |
42 |
| -$marcSpec->setIndicator1('1'); |
43 |
| -$marcSpec->setIndicator2('0'); |
44 |
| - |
45 |
| -$enc = $marcSpec->encode(); // '245$a$b$e_10' |
46 |
| - |
47 |
| -// initialize empty instance |
48 |
| -$marcSpec = new MARCspec; |
49 |
| - |
50 |
| -$marcSpec->setFieldTag('007'); |
51 |
| -$marcSpec->setCharStart(0); |
52 |
| -$marcSpec->setLength(5); |
53 |
| - |
54 |
| -$enc = $marcSpec->encode(); // '007/0-4' |
55 |
| -$enc = $marcSpec->encode('json'); // { "marcspec": { "fieldTag": "007", "charStart": 0, "charEnd": 4, "charLength": 5 } } |
56 |
| - |
57 |
| -// initialize empty instance |
58 |
| -$marcSpec = new MARCspec; |
59 |
| - |
60 |
| -marcSpec->validate('245$a_1'); // true |
61 |
| -marcSpec->validate('004$a/1'); // InvalidArgumentException |
| 15 | +namespace CK\MARCspec; |
| 16 | +require("vendor/autoload.php"); |
| 17 | + |
| 18 | +// parse and access MARCspec like an array |
| 19 | +$fixed = new MARCspec('007[0]/1-8{/0=\a}'); |
| 20 | +echo $fixed['field']['tag']."\n"; // '007' |
| 21 | +echo $fixed['field']['charStart']."\n"; // 1 |
| 22 | +echo $fixed['field']['charEnd']."\n"; // 8 |
| 23 | +echo $fixed['field']['charLength']."\n"; // 8 |
| 24 | +echo $fixed['field']['subSpecs'][0]['leftSubTerm']."\n"; // '007[0]/0' |
| 25 | +echo $fixed['field']['subSpecs'][0]['operator']."\n"; // '=' |
| 26 | +echo $fixed['field']['subSpecs'][0]['rightSubTerm']."\n"; // '\a' |
| 27 | +echo $fixed['field']['subSpecs'][0]['rightSubTerm']['comparable']."\n"; // 'a' |
| 28 | + |
| 29 | +echo $fixed."\n"; // '007[0]/1-8{007[0]/0=\a}' |
| 30 | + |
| 31 | +$variable = new MARCspec('245_10$a'); |
| 32 | +echo $variable['field']['tag']."\n"; // '245' |
| 33 | +echo $variable['field']['indicator1']."\n"; // '1' |
| 34 | +echo $variable['field']['indicator2']."\n"; // '0' |
| 35 | +echo $variable['subfields'][0]['tag']."\n"; // 'a' |
| 36 | +echo $variable['a'][0]['tag']."\n"; // 'a' |
| 37 | + |
| 38 | +echo $variable."\n"; // '245_10$a' |
| 39 | + |
| 40 | +$complex = new MARCspec('020$a{$q[0]~\pbk}{$c/0=\€|$c/0=\$}'); |
| 41 | +echo $complex['field']['tag']."\n"; // '020' |
| 42 | +echo $complex['subfields'][0]['tag']."\n"; // 'a' |
| 43 | + |
| 44 | +echo $complex['a'][0]['subSpecs'][0]['leftSubTerm']."\n"; // '020$q[0]' |
| 45 | +echo $complex['a'][0]['subSpecs'][0]['operator']."\n"; // '~' |
| 46 | +echo $complex['a'][0]['subSpecs'][0]['rightSubTerm']['comparable']."\n"; // 'pbk' |
| 47 | + |
| 48 | +echo $complex['a'][0]['subSpecs'][1][0]['leftSubTerm']."\n"; // '020$c/0' |
| 49 | +echo $complex['a'][0]['subSpecs'][1][0]['leftSubTerm']['c'][0]['charStart']."\n"; // 0 |
| 50 | +echo $complex['a'][0]['subSpecs'][1][0]['leftSubTerm']['c'][0]['charEnd']."\n"; // null |
| 51 | +echo $complex['a'][0]['subSpecs'][1][0]['leftSubTerm']['c'][0]['charLength']."\n"; // 1 |
| 52 | +echo $complex['a'][0]['subSpecs'][1][0]['operator']."\n"; // '=' |
| 53 | +echo $complex['a'][0]['subSpecs'][1][0]['rightSubTerm']['comparable']."\n"; // '€' |
| 54 | + |
| 55 | +echo $complex['a'][0]['subSpecs'][1][1]['leftSubTerm']."\n"; // '020$c/0' |
| 56 | +echo $complex['a'][0]['subSpecs'][1][1]['leftSubTerm']['c'][0]['charStart']."\n"; // 0 |
| 57 | +echo $complex['a'][0]['subSpecs'][1][1]['leftSubTerm']['c'][0]['charEnd']."\n"; // null |
| 58 | +echo $complex['a'][0]['subSpecs'][1][1]['leftSubTerm']['c'][0]['charLength']."\n"; // 1 |
| 59 | +echo $complex['a'][0]['subSpecs'][1][1]['operator']."\n"; // '=' |
| 60 | +echo $complex['a'][0]['subSpecs'][1][1]['rightSubTerm']['comparable']."\n"; // '$' |
| 61 | + |
| 62 | +// creating MARCspec |
| 63 | + |
| 64 | +// creating a new Field |
| 65 | +$Field = new Field('...'); |
| 66 | +$Field['indicator2'] = '1'; // or $Field->setIndicator2('1'); |
| 67 | +$Field['indexStart'] = 0; // or $Field->setIndex(0); |
| 68 | + |
| 69 | +// creating a new MARCspec by setting the Field |
| 70 | +$MARCspec = MARCspec::setField($Field); |
| 71 | + |
| 72 | +// creating a new Subfield |
| 73 | +$Subfield = new Subfield('a'); |
| 74 | + |
| 75 | +// adding the Subfield to the MARCspec |
| 76 | +$MARCspec->addSubfields($Subfield); |
| 77 | + |
| 78 | +// creating instances of MARCspec and ComparisonString |
| 79 | +$LeftSubTerm = new MARCspec('...$a/#'); |
| 80 | +$RightSubTerm = new ComparisonString(','); |
| 81 | + |
| 82 | +// creating a new SubSpec with instances above and an operator '=' |
| 83 | +$SubSpec = new SubSpec($LeftSubTerm,'=',$RightSubTerm); |
| 84 | + |
| 85 | +// adding the SubSpec to the Subfield |
| 86 | +$Subfield['subSpecs'] = $SubSpec; |
| 87 | + |
| 88 | +// echo whole MARCspec |
| 89 | +echo $MARCspec; // '...[0]__1$a{...$a/#=\,}' |
62 | 90 | ```
|
63 | 91 |
|
64 |
| -## Public methods |
65 |
| - |
66 |
| -### CK\MARCspec\MARCspec::__construct() |
67 |
| - |
68 |
| -Params: |
69 |
| - |
70 |
| -* string $spec: The MARC spec as string |
71 |
| - |
72 |
| -### CK\MARCspec\MARCspec::decode() |
73 |
| - |
74 |
| -Params: |
75 |
| - |
76 |
| -* string $spec: The MARC spec as string |
77 |
| - |
78 |
| -### CK\MARCspec\MARCspec::encode() |
79 |
| - |
80 |
| -Params: |
81 |
| - |
82 |
| -* string $encoding: The MARCspec encoding ("string" (default) and "json" currently supported) |
83 |
| - |
84 |
| -Return: string or JSON |
85 |
| - |
86 |
| -### CK\MARCspec\MARCspec::validate() |
87 |
| - |
88 |
| -Return: true | InvalidArgumentException |
89 |
| - |
90 |
| -### CK\MARCspec\MARCspec::setFieldTag() |
91 |
| - |
92 |
| -Params: |
93 |
| - |
94 |
| -* string $fieldTag: The field tag |
95 |
| - |
96 |
| -### CK\MARCspec\MARCspec::addSubfields() |
97 |
| - |
98 |
| -Params: |
99 |
| - |
100 |
| -* string $subfields: The string of subfield tags |
101 |
| - |
102 |
| -### CK\MARCspec\MARCspec::setIndicators() |
103 |
| - |
104 |
| -Params: |
105 |
| - |
106 |
| -* string $indicators: The string of indicators 1 and 2 |
107 |
| - |
108 |
| -### CK\MARCspec\MARCspec::setIndicator1() |
109 |
| - |
110 |
| -Params: |
111 |
| - |
112 |
| -* string $indicator1: Indicator 1 |
113 |
| - |
114 |
| -### CK\MARCspec\MARCspec::setIndicator2() |
115 |
| - |
116 |
| -Params: |
117 |
| - |
118 |
| -* string $indicator1: Indicator 2 |
119 |
| - |
120 |
| -### CK\MARCspec\MARCspec::setCharStart() |
121 |
| - |
122 |
| -Params: |
123 |
| - |
124 |
| -* int : charcter start position |
125 |
| - |
126 |
| -### CK\MARCspec\MARCspec::setCharEnd() |
127 |
| - |
128 |
| -Params: |
129 |
| - |
130 |
| -* int : charcter end position |
131 |
| - |
132 |
| -### CK\MARCspec\MARCspec::setCharLength() |
133 |
| - |
134 |
| -Params: |
135 |
| - |
136 |
| -* int : charcter range length |
137 |
| - |
138 |
| -### CK\MARCspec\MARCspec::getFieldTag() |
139 |
| - |
140 |
| -Return: string |
141 |
| - |
142 |
| -### CK\MARCspec\MARCspec::getSubfields() |
143 |
| - |
144 |
| -Return: array |
145 |
| - |
146 |
| -### CK\MARCspec\MARCspec::getIndicator1() |
147 |
| - |
148 |
| -Return: string |
149 |
| - |
150 |
| -### CK\MARCspec\MARCspec::getIndicator2() |
151 |
| - |
152 |
| -Return: string |
153 |
| - |
154 |
| -### CK\MARCspec\MARCspec::getCharStart() |
155 |
| - |
156 |
| -Return: int |
157 |
| - |
158 |
| -### CK\MARCspec\MARCspec::getCharEnd() |
159 |
| - |
160 |
| -Return: int |
161 |
| - |
162 |
| -### CK\MARCspec\MARCspec::getCharLength() |
163 |
| - |
164 |
| -Return: int |
165 |
| - |
| 92 | +# ArrayAccess vs. Methods |
| 93 | + |
| 94 | +MARCspec can be accessed like an immutable array with the following offsets or with its correponding methods (see source code for documentation of all methods). |
| 95 | + |
| 96 | +## Instances of MARCspec |
| 97 | + |
| 98 | +| offset | method get | method set | |
| 99 | +|:---------:|:-------------:|:-----------------:| |
| 100 | +| field | getField | setField| |
| 101 | +| subfields | getSubfields | addSubfields| |
| 102 | + |
| 103 | +## Instances of Field |
| 104 | + |
| 105 | +| offset | method get | method set | |
| 106 | +|:---------:|:-------------:|:-----------------:| |
| 107 | +| tag | getTag | setTag| |
| 108 | +| indicator1| getIndicator1 | setIndicator1| |
| 109 | +| indicator2| getIndicator2 | setIndicator2| |
| 110 | +| charStart | getCharStart | setCharStart| |
| 111 | +| charEnd | getCharEnd | setCharEnd| |
| 112 | +| charLength| getCharLength | | |
| 113 | +| indexStart| getIndexStart | setIndexStart| |
| 114 | +| indexEnd | getIndexEnd | setIndexEnd| |
| 115 | +| indexLength| getIndexLength | | |
| 116 | +| subSpecs | getSubSpecs | addSubSpecs| |
| 117 | + |
| 118 | +## Instances of Subfield |
| 119 | + |
| 120 | +| offset | method get | method set | |
| 121 | +|:---------:|:-------------:|:-----------------:| |
| 122 | +| tag | getTag | setTag| |
| 123 | +| charStart | getCharStart | setCharStart| |
| 124 | +| charEnd | getCharEnd | setCharEnd| |
| 125 | +| charLength| getCharLength | | |
| 126 | +| indexStart| getIndexStart | setIndexStart| |
| 127 | +| indexEnd | getIndexEnd | setIndexEnd| |
| 128 | +| indexLength| getIndexLength | | |
| 129 | +| subSpecs | getSubSpecs | addSubSpecs| |
| 130 | + |
| 131 | +## Instances of ComparisonString |
| 132 | + |
| 133 | +| offset | method get | method set | |
| 134 | +|:---------:|:-------------:|:-----------------:| |
| 135 | +| raw | getRaw | | |
| 136 | +| comparable| getComprable | | |
| 137 | + |
| 138 | +## Instances of SubSpec |
| 139 | + |
| 140 | +| offset | method get | method set | |
| 141 | +|:---------:|:-------------:|:-----------------:| |
| 142 | +| leftSubTerm| getLeftSubTerm| | |
| 143 | +| operator | getOperator | | |
| 144 | +| rightSubTerm| getRightSubTerm| | |
0 commit comments