Skip to content

Commit 1517889

Browse files
committed
finished readme
1 parent f0924c2 commit 1517889

File tree

6 files changed

+152
-166
lines changed

6 files changed

+152
-166
lines changed

Exception/InvalidMARCspecException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class InvalidMARCspecException extends \UnexpectedValueException {
6060
const OPERATOR = 'Operator must be one of "=" / "!=" / "~" / "!~" / "!" / "?".';
6161
const HINTESCAPED = 'Hint: Check for unescaped characters.';
6262
const CHARORIND = 'Either characterSpec or indicators are allowed.';
63+
const CHARANDSF = 'Either characterSpec for field or subfields are allowed.';
6364

6465
public function __construct($message, $context = null)
6566
{

Field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ public function offsetGet($offset)
547547
break;
548548
case 'subSpecs': return $this->getSubSpecs();
549549
break;
550-
default: return null;
550+
default: throw new \UnexpectedValueException("Offset $offset does not exist.");
551551
}
552552
}
553553

@@ -588,9 +588,9 @@ public function offsetSet($offset,$value)
588588
break;
589589
case 'charLength': throw new \UnexpectedValueException("CharLength is always calculated.");
590590
break;
591-
case 'indicator1': $this->setIndicator1();
591+
case 'indicator1': $this->setIndicator1($value);
592592
break;
593-
case 'indicator2': $this->setIndicator2();
593+
case 'indicator2': $this->setIndicator2($value);
594594
break;
595595
case 'subSpecs': $this->addSubSpec($value);
596596
break;

MARCspec.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ private function createInstances($_detected)
397397
$_subfields = [];
398398
if(array_key_exists('subfield',$_dataRef))
399399
{
400+
if(!is_null($this->field->getCharStart()))
401+
{
402+
throw new InvalidMARCspecException(
403+
InvalidMARCspecException::MS.
404+
InvalidMARCspecException::CHARANDSF
405+
);
406+
}
400407
if(2 == strpos($_dataRef['subfield'],'-')) // assuming subfield range
401408
{
402409
$_subfields = $this->handleSubfieldRanges(substr($_dataRef['subfield'],1));

README.md

Lines changed: 134 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,144 @@
11
# PHP MARCspec parser
22

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.
44

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).
510
# Usage
611

712
```php
813
<?php
914

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/#=\,}'
6290
```
6391

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| |

Subfield.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ public function __construct($subfieldspec)
6060

6161
if('$' !== $subfieldspec[0])
6262
{
63-
throw new InvalidMARCspecException(
64-
InvalidMARCspecException::SF.
65-
InvalidMARCspecException::PREFIX,
66-
$subfieldspec
67-
);
63+
$this->validateSubfield($subfieldspec);
64+
}
65+
else
66+
{
67+
$this->validateSubfield(substr($subfieldspec,1));
6868
}
69-
$this->validateSubfield(substr($subfieldspec,1));
7069
}
7170

7271

Test/MarcSpecTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public function testInvalidArgument2Decode()
4848
$this->marcspec(array('245$a'));
4949
}
5050
/**
51-
* @expectedException InvalidArgumentException
51+
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
5252
*/
5353
public function testInvalidArgument3Decode()
5454
{
55-
$this->marcspec(array('245/#$a'));
55+
$this->marcspec('245/#$a');
5656
}
5757

5858

0 commit comments

Comments
 (0)