Skip to content

Commit ba9a62e

Browse files
committedMay 7, 2015
Merge pull request #5 from cKlee/master
added indexLength+invalid regex
2 parents 2d570c8 + d983f9b commit ba9a62e

8 files changed

+200
-65
lines changed
 

‎Exception/InvalidMARCspecException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class InvalidMARCspecException extends \UnexpectedValueException {
4444
const LENGTHIND = 'For indicators only two characters at are allowed.';
4545
const INDCHAR1 = 'At minimum one indicator must be a digit or a lowercase alphabetic character.';
4646
const INDCHAR2 = 'For indicators only digits, lowercase alphabetic characters and "_" are allowed.';
47-
const NEGATIVE = 'Ending character position or index must be equal or higher than starting character potion or index.';
47+
const NEGATIVE = 'Ending character or index position must be equal or higher than starting character or index position.';
4848
const PR1 = 'Assuming index or character position or range. Minimum one character is required. None given.';
4949
const PR2 = 'Assuming index or character position or range. Only digits, the character # and one "-" is allowed.';
5050
const PR3 = 'Assuming index or character range. At least two digits or the character # must be present.';

‎Field.php

+20
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@ public function jsonSerialize()
425425
$_fieldSpec['indexEnd'] = $indexEnd;
426426
}
427427

428+
if(($indexLength = $this->getIndexLength()) !== null)
429+
{
430+
$_fieldSpec['indexLength'] = $indexLength;
431+
}
432+
428433
if(($charStart = $this->getCharStart()) !== null)
429434
{
430435
$_fieldSpec['charStart'] = $charStart;
@@ -549,6 +554,8 @@ public function offsetExists($offset)
549554
break;
550555
case 'indexEnd': return isset($this->indexEnd);
551556
break;
557+
case 'indexLength': return !is_null($this->getIndexLength());
558+
break;
552559
case 'charStart': return isset($this->charStart);
553560
break;
554561
case 'charEnd': return isset($this->charEnd);
@@ -583,6 +590,8 @@ public function offsetGet($offset)
583590
break;
584591
case 'indexEnd': return $this->getIndexEnd();
585592
break;
593+
case 'indexLength': return $this->getIndexLength();
594+
break;
586595
case 'charStart': return $this->getCharStart();
587596
break;
588597
case 'charEnd': return $this->getCharEnd();
@@ -613,6 +622,7 @@ public function offsetSet($offset,$value)
613622
{
614623
case 'indexStart': $this->setIndexStartEnd($value);
615624
break;
625+
616626
case 'indexEnd':
617627
if(!isset($this['indexStart']))
618628
{
@@ -623,8 +633,13 @@ public function offsetSet($offset,$value)
623633
$this->setIndexStartEnd($this['indexStart'],$value);
624634
}
625635
break;
636+
637+
case 'indexLength': throw new \UnexpectedValueException("indexLength is always calculated.");
638+
break;
639+
626640
case 'charStart': $this->setCharStartEnd($value);
627641
break;
642+
628643
case 'charEnd':
629644
if(!isset($this['charStart']))
630645
{
@@ -635,14 +650,19 @@ public function offsetSet($offset,$value)
635650
$this->setCharStartEnd($this['charStart'],$value);
636651
}
637652
break;
653+
638654
case 'charLength': throw new \UnexpectedValueException("CharLength is always calculated.");
639655
break;
656+
640657
case 'indicator1': $this->setIndicator1($value);
641658
break;
659+
642660
case 'indicator2': $this->setIndicator2($value);
643661
break;
662+
644663
case 'subSpecs': $this->addSubSpec($value);
645664
break;
665+
646666
default: throw new \UnexpectedValueException("Offset $offset cannot be set.");
647667
}
648668
}

‎PositionOrRange.php

+41-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,46 @@ public function getCharLength()
122122
}
123123
}
124124

125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function getIndexLength()
129+
{
130+
if( is_null($this->getIndexStart()) && is_null($this->getIndexEnd()) )
131+
{
132+
return null;
133+
}
134+
if( !is_null($this->getIndexStart()) && is_null($this->getIndexEnd()) )
135+
{
136+
return 1;
137+
}
138+
// both defined
139+
if($this->indexStart === $this->indexEnd)
140+
{
141+
return 1;
142+
}
143+
if('#' === $this->indexStart && '#' !== $this->indexEnd)
144+
{
145+
return $this->indexEnd + 1;
146+
}
147+
if('#' !== $this->indexStart && '#' === $this->indexEnd)
148+
{
149+
return null;
150+
}
151+
else
152+
{
153+
$length = $this->indexEnd - $this->indexStart + 1;
154+
if(1 > $length)
155+
{
156+
throw new InvalidMARCspecException(
157+
InvalidMARCspecException::PR.
158+
InvalidMARCspecException::NEGATIVE
159+
);
160+
}
161+
return $length;
162+
}
163+
}
164+
125165
/**
126166
* {@inheritdoc}
127167
*/
@@ -157,7 +197,7 @@ protected function validatePos($pos)
157197

158198
for($x = 0; $x < $posLength; $x++)
159199
{
160-
if(!preg_match('/[0-9-#]/', $pos[$x])) // alphabetic characters etc. are not valid
200+
if(!preg_match('/[0-9\-#]/', $pos[$x])) // alphabetic characters etc. are not valid
161201
{
162202
throw new InvalidMARCspecException(
163203
InvalidMARCspecException::PR.

‎PositionOrRangeInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,18 @@ public function getCharEnd();
133133
* @throws \InvalidArgumentException if length is less than 1
134134
*/
135135
public function getCharLength();
136+
137+
/**
138+
*
139+
* Get length of index range
140+
*
141+
* @api
142+
*
143+
* @access public
144+
*
145+
* @return null|int $length The index range length
146+
*
147+
* @throws \InvalidArgumentException if length is less than 1
148+
*/
149+
public function getIndexLength();
136150
} // EOI

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ For currently supported version of **MARCspec - A common MARC record path langua
66

77
# Installation
88

9-
Installation can be done by using [composer](https://getcomposer.org/doc/00-intro.md)
9+
Installation can be done by using [composer](https://getcomposer.org/doc/00-intro.md).
1010

11-
```php
11+
```json
1212
{
1313
"require": {
1414
"ck/php-marcspec": "1.*"

‎Subfield.php

+52-54
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ public function jsonSerialize()
278278
$_subfieldSpec['indexEnd'] = $indexEnd;
279279
}
280280

281+
if(($indexLength = $this->getIndexLength()) !== null)
282+
{
283+
$_subfieldSpec['indexLength'] = $indexLength;
284+
}
285+
281286
if(($charStart = $this->getCharStart()) !== null)
282287
{
283288
$_subfieldSpec['charStart'] = $charStart;
@@ -384,26 +389,21 @@ public function offsetExists($offset)
384389
{
385390

386391
case 'tag': return isset($this->tag);
387-
break;
388-
392+
break;
389393
case 'indexStart': return isset($this->indexStart);
390-
break;
391-
394+
break;
392395
case 'indexEnd': return isset($this->indexEnd);
393-
break;
394-
396+
break;
397+
case 'indexLength': return !is_null($this->getIndexLength());
398+
break;
395399
case 'charStart': return isset($this->charStart);
396-
break;
397-
400+
break;
398401
case 'charEnd': return isset($this->charEnd);
399-
break;
400-
402+
break;
401403
case 'charLength': return !is_null($this->getCharLength());
402-
break;
403-
404+
break;
404405
case 'subSpecs': return (0 < count($this->subSpecs)) ? true : false;
405-
break;
406-
406+
break;
407407
default: return false;
408408
}
409409
}
@@ -420,26 +420,21 @@ public function offsetGet($offset)
420420
switch($offset)
421421
{
422422
case 'tag': return $this->getTag();
423-
break;
424-
423+
break;
425424
case 'indexStart': return $this->getIndexStart();
426-
break;
427-
425+
break;
428426
case 'indexEnd': return $this->getIndexEnd();
429-
break;
430-
427+
break;
428+
case 'indexLength': return $this->getIndexLength();
429+
break;
431430
case 'charStart': return $this->getCharStart();
432-
break;
433-
431+
break;
434432
case 'charEnd': return $this->getCharEnd();
435-
break;
436-
433+
break;
437434
case 'charLength': return $this->getCharLength();
438-
break;
439-
435+
break;
440436
case 'subSpecs': return $this->getSubSpecs();
441-
break;
442-
437+
break;
443438
default: return null;
444439
}
445440
}
@@ -456,39 +451,42 @@ public function offsetSet($offset,$value)
456451
switch($offset)
457452
{
458453
case 'indexStart': $this->setIndexStartEnd($value);
459-
break;
454+
break;
460455

461456
case 'indexEnd':
462-
if(!isset($this['indexStart']))
463-
{
464-
$this->setIndexStartEnd($value,$value);
465-
}
466-
else
467-
{
468-
$this->setIndexStartEnd($this['indexStart'],$value);
469-
}
470-
break;
457+
if(!isset($this['indexStart']))
458+
{
459+
$this->setIndexStartEnd($value,$value);
460+
}
461+
else
462+
{
463+
$this->setIndexStartEnd($this['indexStart'],$value);
464+
}
465+
break;
471466

472-
case 'charStart': $this->setCharStartEnd($value);
473-
break;
467+
case 'indexLength': throw new \UnexpectedValueException("indexLength is always calculated.");
468+
break;
474469

470+
case 'charStart': $this->setCharStartEnd($value);
471+
break;
472+
475473
case 'charEnd':
476-
if(!isset($this['charStart']))
477-
{
478-
$this->setCharStartEnd($value,$value);
479-
}
480-
else
481-
{
482-
$this->setCharStartEnd($this['charStart'],$value);
483-
}
484-
break;
485-
474+
if(!isset($this['charStart']))
475+
{
476+
$this->setCharStartEnd($value,$value);
477+
}
478+
else
479+
{
480+
$this->setCharStartEnd($this['charStart'],$value);
481+
}
482+
break;
483+
486484
case 'charLength': throw new \UnexpectedValueException("CharLength is always calculated.");
487-
break;
488-
485+
break;
486+
489487
case 'subSpecs': $this->addSubSpec($value);
490-
break;
491-
488+
break;
489+
492490
default: throw new \UnexpectedValueException("Offset $offset cannot be set.");
493491
}
494492
}

‎Test/FieldTest.php

+36-4
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ public function testValidFieldSpec1()
234234

235235
$fieldSpec = $this->fieldspec('245[1-3]');
236236
$this->assertSame(1, $fieldSpec->getIndexStart());
237-
$this->assertSame(3, $fieldSpec->getIndexEnd());
237+
$this->assertSame(3, $fieldSpec->getIndexEnd());
238238

239239
$fieldSpec = $this->fieldspec('245[1-#]');
240240
$this->assertSame(1, $fieldSpec->getIndexStart());
241-
$this->assertSame('#', $fieldSpec->getIndexEnd());
241+
$this->assertSame('#', $fieldSpec->getIndexEnd());
242242

243243
$fieldSpec = $this->fieldspec('245[#-3]');
244244
$this->assertSame('#', $fieldSpec->getIndexStart());
@@ -320,7 +320,7 @@ public function testValidFieldSpec23()
320320
/**
321321
* test character position and range
322322
*/
323-
public function testSetAndGet()
323+
public function testSetAndGetChar()
324324
{
325325
$fieldSpec = $this->fieldspec('LDR');
326326
$fieldSpec->setCharStartEnd(0,3);
@@ -349,8 +349,40 @@ public function testSetAndGet()
349349
$this->assertSame("#", $fieldSpec->getCharStart());
350350
$this->assertSame(3, $fieldSpec->getCharEnd());
351351
$this->assertSame(4, $fieldSpec->getCharLength());
352+
}
353+
354+
/**
355+
* test index position and range
356+
*/
357+
public function testSetAndGetIndex()
358+
{
359+
$fieldSpec = $this->fieldspec('300');
360+
$fieldSpec->setIndexStartEnd(0,3);
361+
$this->assertSame('300', $fieldSpec->getTag());
362+
$this->assertSame(0, $fieldSpec->getIndexStart());
363+
$this->assertSame(3, $fieldSpec->getIndexEnd());
364+
$this->assertSame(4, $fieldSpec->getIndexLength());
352365

353-
366+
$fieldSpec = $this->fieldspec('300');
367+
$fieldSpec->setIndexStartEnd("#",3);
368+
$this->assertSame('300', $fieldSpec->getTag());
369+
$this->assertSame("#", $fieldSpec->getIndexStart());
370+
$this->assertSame(3, $fieldSpec->getIndexEnd());
371+
$this->assertSame(4, $fieldSpec->getIndexLength());
372+
373+
$fieldSpec = $this->fieldspec('300');
374+
$fieldSpec->setIndexStartEnd(0,4);
375+
$this->assertSame('300', $fieldSpec->getTag());
376+
$this->assertSame(0, $fieldSpec->getIndexStart());
377+
$this->assertSame(4, $fieldSpec->getIndexEnd());
378+
$this->assertSame(5, $fieldSpec->getIndexLength());
379+
380+
$fieldSpec = $this->fieldspec('300');
381+
$fieldSpec->setIndexStartLength("#",4);
382+
$this->assertSame('300', $fieldSpec->getTag());
383+
$this->assertSame("#", $fieldSpec->getIndexStart());
384+
$this->assertSame(3, $fieldSpec->getIndexEnd());
385+
$this->assertSame(4, $fieldSpec->getIndexLength());
354386
}
355387

356388
/**

‎Test/SubfieldTest.php

+34-3
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function testValidSubfieldSpec22()
255255
/**
256256
* test character position and range
257257
*/
258-
public function testSetAndGet()
258+
public function testSetAndGetChar()
259259
{
260260
$Subfield = $this->subfieldspec('$a');
261261
$Subfield->setCharStartEnd('0','3');
@@ -284,10 +284,41 @@ public function testSetAndGet()
284284
$this->assertSame("#", $Subfield->getCharStart());
285285
$this->assertSame(3, $Subfield->getCharEnd());
286286
$this->assertSame(4, $Subfield->getCharLength());
287-
287+
}
288288

289+
/**
290+
* test index position and range
291+
*/
292+
public function testSetAndGetIndex()
293+
{
294+
$Subfield = $this->subfieldspec('$a');
295+
$Subfield->setIndexStartEnd('0','3');
296+
$this->assertSame('a', $Subfield->getTag());
297+
$this->assertSame(0, $Subfield->getIndexStart());
298+
$this->assertSame(3, $Subfield->getIndexEnd());
299+
$this->assertSame(4, $Subfield->getIndexLength());
300+
301+
$Subfield = $this->subfieldspec('$a');
302+
$Subfield->setIndexStartEnd("#",3);
303+
$this->assertSame('a', $Subfield->getTag());
304+
$this->assertSame("#", $Subfield->getIndexStart());
305+
$this->assertSame(3, $Subfield->getIndexEnd());
306+
$this->assertSame(4, $Subfield->getIndexLength());
307+
308+
$Subfield = $this->subfieldspec('$a');
309+
$Subfield->setIndexStartEnd(0,4);
310+
$this->assertSame('a', $Subfield->getTag());
311+
$this->assertSame(0, $Subfield->getIndexStart());
312+
$this->assertSame(4, $Subfield->getIndexEnd());
313+
$this->assertSame(5, $Subfield->getIndexLength());
314+
315+
$Subfield = $this->subfieldspec('$a');
316+
$Subfield->setIndexStartLength("#",4);
317+
$this->assertSame('a', $Subfield->getTag());
318+
$this->assertSame("#", $Subfield->getIndexStart());
319+
$this->assertSame(3, $Subfield->getIndexEnd());
320+
$this->assertSame(4, $Subfield->getIndexLength());
289321
}
290-
291322
/**
292323
* test encoding
293324
*/

0 commit comments

Comments
 (0)
Please sign in to comment.