Skip to content

Commit 1eb0d45

Browse files
committed
Merge pull request #3 from cKlee/master
added iterators
2 parents 23619f1 + f9a35d7 commit 1eb0d45

File tree

6 files changed

+127
-4
lines changed

6 files changed

+127
-4
lines changed

MARCspec.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* For Specification of MARC spec as string see
1818
* <http://cklee.github.io/marc-spec/marc-spec.html>
1919
*/
20-
class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess
20+
class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate
2121
{
2222

2323
/**
@@ -674,4 +674,9 @@ public function offsetUnset($offset)
674674
{
675675
throw new \BadMethodCallException("Offset $offset can not be unset.");
676676
}
677+
678+
public function getIterator()
679+
{
680+
return new SpecIterator(["field" => $this->field, "subfields" => $this->subfields]);
681+
}
677682
} // EOC

SpecIterator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* MARCspec is the specification of a reference, encoded as string, to a set of data
4+
* from within a MARC record.
5+
*
6+
* @author Carsten Klee <mailme.klee@yahoo.de>
7+
* @package CK\MARCspec
8+
* @copyright For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace CK\MARCspec;
12+
13+
class SpecIterator implements \Iterator
14+
{
15+
private $var = array();
16+
17+
public function __construct($array)
18+
{
19+
if (is_array($array))
20+
{
21+
$this->var = $array;
22+
}
23+
}
24+
25+
public function rewind()
26+
{
27+
reset($this->var);
28+
}
29+
30+
public function current()
31+
{
32+
$var = current($this->var);
33+
return $var;
34+
}
35+
36+
public function key()
37+
{
38+
$var = key($this->var);
39+
return $var;
40+
}
41+
42+
public function next()
43+
{
44+
$var = next($this->var);
45+
return $var;
46+
}
47+
48+
public function valid()
49+
{
50+
$var = $this->current() !== false;
51+
return $var;
52+
}
53+
}

SubSpec.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* A MARCspec subspec class
1717
*/
18-
class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess
18+
class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate
1919
{
2020

2121
/**
@@ -202,4 +202,15 @@ public function offsetUnset($offset)
202202
{
203203
throw new \BadMethodCallException("Offset $offset can not be unset.");
204204
}
205+
206+
public function getIterator()
207+
{
208+
return new SpecIterator(
209+
[
210+
"leftSubTerm" => $this->leftSubTerm,
211+
"operator" => $this->operator,
212+
"rightSubTerm" => $this->rightSubTerm
213+
]
214+
);
215+
}
205216
} // EOC

Subfield.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* A MARCspec subfield class
1717
*/
18-
class Subfield extends PositionOrRange implements SubfieldInterface, \JsonSerializable, \ArrayAccess
18+
class Subfield extends PositionOrRange implements SubfieldInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate
1919
{
2020

2121
/**
@@ -497,4 +497,13 @@ public function offsetUnset($offset)
497497
{
498498
throw new \BadMethodCallException("Offset $offset can not be unset.");
499499
}
500+
501+
public function getIterator()
502+
{
503+
foreach($this as $key => $value)
504+
{
505+
$_[$key] = $value;
506+
}
507+
return new SpecIterator($_);
508+
}
500509
} // EOC

Test/MarcSpecTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,49 @@ public function testValidMarcSpec5()
171171

172172
}
173173

174+
public function testIteration()
175+
{
176+
$ms = $this->marcspec('245$a-c{$b|$c}{$e}');
177+
$count = 0;
178+
179+
foreach($ms as $key => $value)
180+
{
181+
$count++;
182+
}
183+
$this->assertSame(2, $count);
184+
185+
$count = 0;
186+
foreach($ms['subfields'] as $key => $value)
187+
{
188+
$count++;
189+
}
190+
$this->assertSame(3, $count);
191+
192+
193+
foreach($ms['subfields'] as $subfield)
194+
{
195+
$count = 0;
196+
foreach($subfield['subSpecs'] as $subSpec)
197+
{
198+
if(is_array($subSpec))
199+
{
200+
$this->assertSame(2, count($subSpec));
201+
}
202+
else
203+
{
204+
foreach($subSpec as $key => $prop)
205+
{
206+
$this->assertTrue(in_array($key,["leftSubTerm","operator","rightSubTerm"]));
207+
}
208+
}
209+
210+
$count++;
211+
}
212+
$this->assertSame(2, $count);
213+
}
214+
215+
216+
}
174217

175218

176219
}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"type": "library",
44
"description": "PHP based MARCspec parser and validator",
55
"keywords": [ "MARCspec", "MARC 21" ],
6-
"homepage": "http://cklee.github.io/marc-spec/marc-spec.html",
76
"authors": [
87
{
98
"name": "Carsten Klee",
@@ -14,6 +13,9 @@
1413
"require": {
1514
"php": ">=5.4.0"
1615
},
16+
"require-dev": {
17+
"phpunit/phpunit": "4.5.*"
18+
},
1719
"autoload": {
1820
"psr-0": { "CK\\MARCspec": "" }
1921
},

0 commit comments

Comments
 (0)