Skip to content

Commit

Permalink
Merge pull request #3 from cKlee/master
Browse files Browse the repository at this point in the history
added iterators
  • Loading branch information
cKlee committed Apr 24, 2015
2 parents 23619f1 + f9a35d7 commit 1eb0d45
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 4 deletions.
7 changes: 6 additions & 1 deletion MARCspec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* For Specification of MARC spec as string see
* <http://cklee.github.io/marc-spec/marc-spec.html>
*/
class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess
class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate
{

/**
Expand Down Expand Up @@ -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
53 changes: 53 additions & 0 deletions SpecIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* MARCspec is the specification of a reference, encoded as string, to a set of data
* from within a MARC record.
*
* @author Carsten Klee <mailme.klee@yahoo.de>
* @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;
}
}
13 changes: 12 additions & 1 deletion SubSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* A MARCspec subspec class
*/
class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess
class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate
{

/**
Expand Down Expand Up @@ -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
11 changes: 10 additions & 1 deletion Subfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down Expand Up @@ -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
43 changes: 43 additions & 0 deletions Test/MarcSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}


}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -14,6 +13,9 @@
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.5.*"
},
"autoload": {
"psr-0": { "CK\\MARCspec": "" }
},
Expand Down

0 comments on commit 1eb0d45

Please sign in to comment.