-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMARCspecToArray.php
More file actions
343 lines (308 loc) · 10.6 KB
/
MARCspecToArray.php
File metadata and controls
343 lines (308 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* Parses a MARCspec into an array
*/
class MARCspecParser
{
/**
* @var string Regex for field tag
*/
protected $FIELDTAG;
/**
* @var string Regex for position or range
*/
protected $POSITIONORRANGE;
/**
* @var string Regex for index
*/
protected $INDEX;
/**
* @var string Regex for charpos
*/
protected $CHARPOS;
/**
* @var string Regex for indicators
*/
protected $INDICATORS;
/**
* @var string Regex for field subspecs
*/
protected $F_SUBSPECS;
/**
* @var string Regex for subfield subspecs
*/
protected $SF_SUBSPECS;
/**
* @var string Regex for subfields
*/
protected $SUBFIELDS;
/**
* @var string Regex for field
*/
protected $FIELD;
/**
* @var string Regex for subfield range
*/
protected $SUBFIELDTAGRANGE;
/**
* @var string Regex for subfield tag
*/
protected $SUBFIELDTAG;
/**
* @var string Regex for subfield
*/
protected $SUBFIELD;
/**
* @var string Regex for leftSubTerm
*/
protected $LEFTSUBTERM;
/**
* @var string Regex for operator
*/
protected $OPERATOR;
/**
* @var string Regex for subterms
*/
protected $SUBTERMS;
/**
* @var string Regex for subspec
*/
protected $SUBSPEC;
/**
* @var array The parsed MARCspec
*/
public $parsed = [];
/**
* @var array The parsed fieldspec
*/
public $field = [];
/**
* @var array The parsed subfieldspecs
*/
public $subfields = [];
public function __construct($spec = null)
{
$this->setConstants();
if(is_null($spec)) return;
$this->fieldToArray($spec);
if(array_key_exists('subfields',$this->parsed))
{
$this->subfields = $this->matchSubfields($this->parsed['subfields']);
}
}
/**
* parses fieldspecs into array
*
* @param string $fieldspec The fieldspec
* @return array An Array of fieldspec
*/
public function fieldToArray($fieldspec)
{
$_fieldGroups = ['field','tag','index','charpos','indicators','subfields'];
if(!preg_match_all('/'.$this->FIELD.'/',$fieldspec,$_fieldMatches,PREG_SET_ORDER))
{
throw new UnexpectedValueException('Cannot detect fieldspec.');
}
$this->parsed = array_filter($_fieldMatches[0],'strlen');
if(!array_key_exists('field',$this->parsed))
{
throw new UnexpectedValueException('For fieldtag only "." and digits and lowercase alphabetic or digits and upper case alphabetics characters are allowed');
}
if(strlen($this->parsed['field']) !== strlen($fieldspec))
{
throw new UnexpectedValueException('Detected useless data fragment.');
}
foreach($_fieldGroups as $fieldgroup)
{
if(array_key_exists($fieldgroup,$this->parsed))
{
$this->field[$fieldgroup] = $this->parsed[$fieldgroup];
}
}
if(array_key_exists('charpos',$this->field))
{
if(array_key_exists('indicators',$this->field))
{
throw new UnexpectedValueException('Either characterSpec or indicators are allowed.');
}
if(array_key_exists('subfields',$this->field))
{
throw new UnexpectedValueException('Either characterSpec for field or subfields are allowed.');
}
}
if(array_key_exists('subspecs',$this->parsed))
{
$_fieldSubSpecs = $this->matchSubSpecs($this->parsed['subspecs']);
foreach($_fieldSubSpecs as $fieldSubSpec)
{
if(1 < count($fieldSubSpec))
{
foreach($fieldSubSpec as $orSubSpec)
{
$_or[] = $this->matchSubTerms($orSubSpec);
}
$this->field['subspecs'][] = $_or;
}
else
{
$this->field['subspecs'][] = $this->matchSubTerms($fieldSubSpec[0]);
}
}
}
}
/**
* Matches subfieldspecs
*
* @param string $subfieldspec A string of one or more subfieldspecs
*/
public function matchSubfields($subfieldspec)
{
if(!preg_match_all('/'.$this->SUBFIELD.'/',$subfieldspec,$_subfieldMatches,PREG_SET_ORDER))
{
throw new UnexpectedValueException('For subfields only digits, lowercase alphabetic characters or one of "!"#$%&\'()*+,-./0-9:;<=>?[\]^_`a-z{}~" are allowed.');
}
/**
* For each subfield (array) do anonymous function
* - first filter empty elements
* - second look for subspecs
* - match subspecs and match subTerms
* - return everything in the array of subfields
*/
array_walk(
$_subfieldMatches,
function(&$_subfield) use (&$test)
{
$_subfield = array_filter($_subfield,'strlen');
$test .= $_subfield['subfield'];
if(array_key_exists('subspecs',$_subfield))
{
$_ss = [];
if(!$_subfieldSubSpecs = $this->matchSubSpecs($_subfield['subspecs']))
{
// TODO: raise error;
}
foreach($_subfieldSubSpecs as $key => $_subfieldSubSpec)
{
if(1 < count($_subfieldSubSpec))
{
foreach($_subfieldSubSpec as $orSubSpec)
{
$_or[] = $this->matchSubTerms($orSubSpec);
}
$_ss[] = $_or;
}
else
{
$_ss[] = $this->matchSubTerms($_subfieldSubSpec[0]);
}
}
$_subfield['subspecs'] = $_ss;
}
}
);
if($test !== $subfieldspec)
{
throw new UnexpectedValueException('Detected useless data fragment.');
}
return $_subfieldMatches;
}
/**
* calls matchSubfields but makes sure only one subfield is present
*
* @param string $subfieldspec A subfieldspec
* @return array An Array of subfieldspec
*/
public function subfieldToArray($subfieldspec)
{
if(!$_sf = $this->matchSubfields($subfieldspec))
{
throw new UnexpectedValueException('Assuming invalid spec.');
}
if(1 < count($_sf))
{
throw new UnexpectedValueException('Detected more than one subfieldspecs. Use method addSubfields to add more than one subfield.');
}
if($_sf[0]['subfield'] !== $subfieldspec)
{
throw new UnexpectedValueException('Detected useless data fragment.');
}
return $_sf[0];
}
/**
* parses subspecs into an array
*
* @param string $subSpecs One or more subspecs
* @return array Array of subspecs
*/
private function matchSubSpecs($subSpecs)
{
$_subSpecs = [];
if(!preg_match_all('/'.$this->SUBSPEC.'/U',$subSpecs,$_subSpecMatches,PREG_SET_ORDER))
{
throw new UnexpectedValueException('Assuming invalid spec.');
}
foreach($_subSpecMatches as $key => $_subSpecMatch)
{
if(array_key_exists(1,$_subSpecMatch) && !empty($_subSpecMatch[1]))
{
$_subSpecs[$key] = preg_split('/(?<!\\\)\|/',$_subSpecMatch[1],-1,PREG_SPLIT_NO_EMPTY);
}
else
{
throw new UnexpectedValueException('Assuming invalid spec.');
}
}
return $_subSpecs;
}
/**
* Parses a single SubSpec into sunTerms
*
* @param string $subSpec A single SubSpec
* @return array subTerms as array
*/
private function matchSubTerms($subSpec)
{
if(preg_match('/(?<![\\\\\$])[\{\}]/',$subSpec,$_error, PREG_OFFSET_CAPTURE))
{
throw new UnexpectedValueException('Unescaped character detected');
}
if(preg_match_all('/'.$this->SUBTERMS.'/',$subSpec,$_subTermMatches,PREG_SET_ORDER))
{
if(empty($_subTermMatches[0]['operator']))
{
$_subTermMatches[0]['operator'] = "?";
}
if(!$_subTermMatches[0]['rightsubterm'])
{
throw new UnexpectedValueException('Right hand subTerm is missing.');
}
return array_filter($_subTermMatches[0],'strlen');
}
else
{
throw new UnexpectedValueException('Assuming invalid spec.');
}
}
/**
* Set regex variables (constant)
*/
private function setConstants()
{
$this->FIELDTAG = '^(?<tag>(?:[a-z0-9\.]{3,3}|[A-Z0-9\.]{3,3}|[0-9\.]{3,3}))?';
$this->POSITIONORRANGE = '(?:(?:(?:[0-9]+|#)\-(?:[0-9]+|#))|(?:[0-9]+|#))';
$this->INDEX = '(?:\[(?<index>'.$this->POSITIONORRANGE.')\])?';
$this->CHARPOS = '\/(?<charpos>'.$this->POSITIONORRANGE.')';
$this->INDICATORS = '_(?<indicators>(?:[_a-z0-9][_a-z0-9]{0,1}))';
$this->SUBSPECS = '(?<subspecs>(?:\{.+?(?<!(?<!(\$|\\\))(\$|\\\))\})*)';
#$this->SF_SUBSPECS = '(?<subspecs>(?:\{.+?\})*)';
$this->SUBFIELDS = '(?<subfields>\$.+)?';
$this->FIELD = '(?<field>(?:'.$this->FIELDTAG.$this->INDEX.'(?:'.$this->CHARPOS.'|'.$this->INDICATORS.')?'.$this->SUBSPECS.$this->SUBFIELDS.'))';
$this->SUBFIELDTAGRANGE = '(?<subfieldtagrange>(?:[0-9a-z]\-[0-9a-z]))';
$this->SUBFIELDTAG = '(?<subfieldtag>[\!-\?\[-\{\}-~])';
$this->SUBFIELD = '(?<subfield>\$(?:'.$this->SUBFIELDTAGRANGE.'|'.$this->SUBFIELDTAG.')'.$this->INDEX.'(?:'.$this->CHARPOS.')?'.$this->SUBSPECS.')';
$this->LEFTSUBTERM = '^(?<leftsubterm>(?:\\\(?:(?<=\\\)[\!\=\~\?]|[^\!\=\~\?])+)|(?:(?<=\$)[\!\=\~\?]|[^\!\=\~\?])+)?';
$this->OPERATOR = '(?<operator>\!\=|\!\~|\=|\~|\!|\?)';
$this->SUBTERMS = '(?:'.$this->LEFTSUBTERM.$this->OPERATOR.')?(?<rightsubterm>.+)$';
$this->SUBSPEC = '(?:\{(.+)\})';
}
}