diff --git a/MARCspec.php b/MARCspec.php index 0899e20..6b6703f 100755 --- a/MARCspec.php +++ b/MARCspec.php @@ -17,7 +17,7 @@ * For Specification of MARC spec as string see * */ -class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess +class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { /** @@ -674,4 +674,9 @@ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } + + public function getIterator() + { + return new SpecIterator(["field" => $this->field, "subfields" => $this->subfields]); + } } // EOC diff --git a/SpecIterator.php b/SpecIterator.php new file mode 100755 index 0000000..a6d5b5d --- /dev/null +++ b/SpecIterator.php @@ -0,0 +1,53 @@ + +* @package CK\MARCspec +* @copyright For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ +namespace CK\MARCspec; + +class SpecIterator implements \Iterator +{ + private $var = array(); + + public function __construct($array) + { + if (is_array($array)) + { + $this->var = $array; + } + } + + public function rewind() + { + reset($this->var); + } + + public function current() + { + $var = current($this->var); + return $var; + } + + public function key() + { + $var = key($this->var); + return $var; + } + + public function next() + { + $var = next($this->var); + return $var; + } + + public function valid() + { + $var = $this->current() !== false; + return $var; + } +} \ No newline at end of file diff --git a/SubSpec.php b/SubSpec.php index e00c252..5ad53e1 100755 --- a/SubSpec.php +++ b/SubSpec.php @@ -15,7 +15,7 @@ /** * A MARCspec subspec class */ -class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess +class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { /** @@ -202,4 +202,15 @@ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } + + public function getIterator() + { + return new SpecIterator( + [ + "leftSubTerm" => $this->leftSubTerm, + "operator" => $this->operator, + "rightSubTerm" => $this->rightSubTerm + ] + ); + } } // EOC diff --git a/Subfield.php b/Subfield.php index c22bdf8..a96539c 100755 --- a/Subfield.php +++ b/Subfield.php @@ -15,7 +15,7 @@ /** * A MARCspec subfield class */ -class Subfield extends PositionOrRange implements SubfieldInterface, \JsonSerializable, \ArrayAccess +class Subfield extends PositionOrRange implements SubfieldInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { /** @@ -497,4 +497,13 @@ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } + + public function getIterator() + { + foreach($this as $key => $value) + { + $_[$key] = $value; + } + return new SpecIterator($_); + } } // EOC diff --git a/Test/MarcSpecTest.php b/Test/MarcSpecTest.php index ee492fc..f930ca9 100755 --- a/Test/MarcSpecTest.php +++ b/Test/MarcSpecTest.php @@ -171,6 +171,49 @@ public function testValidMarcSpec5() } + public function testIteration() + { + $ms = $this->marcspec('245$a-c{$b|$c}{$e}'); + $count = 0; + + foreach($ms as $key => $value) + { + $count++; + } + $this->assertSame(2, $count); + + $count = 0; + foreach($ms['subfields'] as $key => $value) + { + $count++; + } + $this->assertSame(3, $count); + + + foreach($ms['subfields'] as $subfield) + { + $count = 0; + foreach($subfield['subSpecs'] as $subSpec) + { + if(is_array($subSpec)) + { + $this->assertSame(2, count($subSpec)); + } + else + { + foreach($subSpec as $key => $prop) + { + $this->assertTrue(in_array($key,["leftSubTerm","operator","rightSubTerm"])); + } + } + + $count++; + } + $this->assertSame(2, $count); + } + + + } } diff --git a/composer.json b/composer.json index 77826b7..8ae561a 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,6 @@ "type": "library", "description": "PHP based MARCspec parser and validator", "keywords": [ "MARCspec", "MARC 21" ], - "homepage": "http://cklee.github.io/marc-spec/marc-spec.html", "authors": [ { "name": "Carsten Klee", @@ -14,6 +13,9 @@ "require": { "php": ">=5.4.0" }, + "require-dev": { + "phpunit/phpunit": "4.5.*" + }, "autoload": { "psr-0": { "CK\\MARCspec": "" } },