@@ -64,11 +64,16 @@ class Field extends PositionOrRange implements FieldInterface, \JsonSerializable
64
64
*
65
65
* @throws InvalidMARCspecException
66
66
*/
67
- public function __construct ($ fieldspec )
67
+ public function __construct ($ fieldspec = null )
68
68
{
69
+ if (is_null ($ fieldspec )) return ;
70
+
69
71
$ this ->checkIfString ($ fieldspec );
72
+
70
73
$ spec = trim ($ fieldspec );
74
+
71
75
$ specLength = strlen ($ fieldspec );
76
+
72
77
// check string length
73
78
if (3 > $ specLength )
74
79
{
@@ -94,138 +99,51 @@ public function __construct($fieldspec)
94
99
$ fieldspec
95
100
);
96
101
}
97
-
98
- /**
99
- * $specMatches[0] => whole spec
100
- * $specMatches[1] => tag
101
- * $specMatches[2] => indexSpec
102
- * $specMatches[3] => charSpec
103
- * $specMatches[4] => indicatorSpec
104
- * $specMatches[5] => useless
105
- */
106
- if (0 === preg_match (
107
- '/^([a-z0-9\.]{3,3}|[A-Z0-9\.]{3,3}|[0-9\.]{3,3})(\[(?:(?:(?:[0-9]+|#)\-(?:[0-9]+|#))|(?:[0-9]+|#))\])?(?:(\/(?:(?:(?:[0-9]+|#)\-(?:[0-9]+|#))|(?:[0-9]+|#)))|(_[_a-z0-9][_a-z0-9]{0,1}))?(.*)?/ ' ,
108
- $ fieldspec ,$ specMatches )
109
- )
102
+
103
+ $ parser = new MARCspecParser ();
104
+
105
+ $ parser ->fieldToArray ($ fieldspec );
106
+
107
+ if (array_key_exists ('subfields ' ,$ parser ->parsed ))
110
108
{
111
109
throw new InvalidMARCspecException (
112
110
InvalidMARCspecException::FS .
113
- InvalidMARCspecException::UNKNOWN ,
111
+ InvalidMARCspecException::DETECTEDSF ,
114
112
$ fieldspec
115
113
);
116
114
}
117
-
118
- $ this ->setTag ($ specMatches [ 1 ]);
119
-
120
- if ($ specLength > 3 )
115
+
116
+ $ this ->setTag ($ parser -> field [ ' tag ' ]);
117
+
118
+ if (array_key_exists ( ' index ' , $ parser -> field ) )
121
119
{
122
- // check an set indexSpec
123
- if (!array_key_exists (2 ,$ specMatches ))
124
- {
125
- throw new InvalidMARCspecException (
126
- InvalidMARCspecException::FS .
127
- InvalidMARCspecException::INDEX ,
128
- $ fieldspec
129
- );
130
- }
131
-
132
- if (!empty ($ specMatches [2 ]))
133
- {
134
- if ( preg_match ('/^\[(.*)\]/ ' , $ specMatches [2 ], $ _assumedIndex ) )
135
- {
136
- if ( $ _index = $ this ->validatePos ($ _assumedIndex [1 ]) )
137
- {
138
- $ indexEnd = null ;
139
- if (array_key_exists ('1 ' ,$ _index ))
140
- {
141
- if (null !== ($ _index [1 ]))
142
- {
143
- $ indexEnd = $ _index [1 ];
144
- }
145
- }
146
- $ this ->setIndexStartEnd ($ _index [0 ],$ indexEnd );
147
- }
148
- }
149
- else
150
- {
151
- throw new InvalidMARCspecException (
152
- InvalidMARCspecException::FS .
153
- InvalidMARCspecException::INDEX ,
154
- $ fieldspec
155
- );
156
- }
157
- }
158
-
159
- if (!empty ($ specMatches [3 ]) && !empty ($ specMatches [4 ]))
160
- {
161
- throw new InvalidMARCspecException (
162
- InvalidMARCspecException::FS .
163
- InvalidMARCspecException::CHARORIND ,
164
- $ fieldspec
165
- );
166
- }
167
-
168
- if (!empty ($ specMatches [3 ]))
169
- {
170
- // check character position or range
171
- $ charPosOrRange = substr ($ specMatches [3 ], 1 );
172
- if ('' != $ charPosOrRange )
173
- {
174
- if ($ _charPosOrRange = $ this ->validatePos ($ charPosOrRange ))
175
- {
176
- $ charEnd = null ;
177
- if (array_key_exists (1 ,$ _charPosOrRange ))
178
- {
179
- if (isset ($ _charPosOrRange [1 ]))
180
- {
181
- $ charEnd = $ _charPosOrRange [1 ];
182
- }
183
- }
184
- $ this ->setCharStartEnd ($ _charPosOrRange [0 ],$ charEnd );
185
- }
186
- }
187
- else
188
- {
189
- throw new InvalidMARCspecException (
190
- InvalidMARCspecException::PR .
191
- InvalidMARCspecException::PRCHAR ,
192
- $ fieldspec
193
- );
194
- }
195
- }
196
-
197
- if (!empty ($ specMatches [4 ]))
198
- {
199
- $ this ->setIndicators (substr ($ specMatches [4 ],1 ));
200
- }
201
-
202
- if (!empty ($ specMatches [5 ]))
203
- {
204
- throw new InvalidMARCspecException (
205
- InvalidMARCspecException::FS .
206
- InvalidMARCspecException::USELESS ,
207
- $ fieldspec
208
- );
209
- }
120
+ $ _pos = MARCspec::validatePos ($ parser ->field ['index ' ]);
121
+
122
+ $ this ->setIndexStartEnd ($ _pos [0 ],$ _pos [1 ]);
210
123
}
211
124
else
212
125
{
213
126
// as of MARCspec 3.2.2 spec without index is always an abbreviation
214
127
$ this ->setIndexStartEnd (0 ,"# " );
215
128
}
129
+
130
+ if (array_key_exists ('indicators ' ,$ parser ->field ))
131
+ {
132
+ $ this ->setIndicators ($ parser ->field ['indicators ' ]);
133
+ }
134
+ elseif (array_key_exists ('charpos ' ,$ parser ->field ))
135
+ {
136
+ $ _chars = MARCspec::validatePos ($ parser ->field ['charpos ' ]);
137
+
138
+ $ this ->setCharStartEnd ($ _chars [0 ],$ _chars [1 ]);
139
+ }
216
140
}
217
141
218
142
/**
219
143
*
220
- * Set the field tag
221
- *
222
- * Provided param gets validated
223
- *
224
- * @access private
225
- *
226
- * @param string $arg The field tag
144
+ * {@inheritdoc}
227
145
*/
228
- private function setTag ($ arg )
146
+ public function setTag ($ arg )
229
147
{
230
148
if ($ this ->validateTag ($ arg )) $ this ->tag = $ arg ;
231
149
}
@@ -422,15 +340,9 @@ public function jsonSerialize()
422
340
{
423
341
$ _fieldSpec ['tag ' ] = $ this ->getTag ();
424
342
425
- if (($ indexStart = $ this ->getIndexStart ()) !== null )
426
- {
427
- $ _fieldSpec ['indexStart ' ] = $ indexStart ;
428
- }
343
+ $ _fieldSpec ['indexStart ' ] = $ this ->getIndexStart ();
429
344
430
- if (($ indexEnd = $ this ->getIndexEnd ()) !== null )
431
- {
432
- $ _fieldSpec ['indexEnd ' ] = $ indexEnd ;
433
- }
345
+ $ _fieldSpec ['indexEnd ' ] = $ this ->getIndexEnd ();
434
346
435
347
if (($ indexLength = $ this ->getIndexLength ()) !== null )
436
348
{
@@ -490,24 +402,23 @@ public function jsonSerialize()
490
402
public function getBaseSpec ()
491
403
{
492
404
$ fieldSpec = $ this ->getTag ();
493
- if (($ indexStart = $ this ->getIndexStart ()) !== null )
405
+ $ indexStart = $ this ->getIndexStart ();
406
+ $ indexEnd = $ this ->getIndexEnd ();
407
+ if (0 === $ indexStart && "# " === $ indexEnd )
494
408
{
495
- $ indexEnd = $ this ->getIndexEnd ();
496
- if (0 === $ indexStart && "# " === $ indexEnd )
497
- {
498
- // use abbreviation
499
- }
500
- else
409
+ // use abbreviation
410
+ }
411
+ else
412
+ {
413
+ $ fieldSpec .= "[ " .$ indexStart ;
414
+
415
+ if ($ indexEnd !== null && $ indexStart !== $ indexEnd )
501
416
{
502
- $ fieldSpec .= "[ " .$ indexStart ;
503
- if ($ indexEnd !== null )
504
- {
505
- $ fieldSpec .= "- " .$ indexEnd ;
506
- }
507
- $ fieldSpec .= "] " ;
417
+ $ fieldSpec .= "- " .$ indexEnd ;
508
418
}
509
-
419
+ $ fieldSpec .= " ] " ;
510
420
}
421
+
511
422
if (($ charStart = $ this ->getCharStart ()) !== null )
512
423
{
513
424
$ charEnd = $ this ->getCharEnd ();
0 commit comments