Skip to content

Instantly share code, notes, and snippets.

@shrikeh
Last active August 29, 2015 13:57
Show Gist options
  • Save shrikeh/9828880 to your computer and use it in GitHub Desktop.
Save shrikeh/9828880 to your computer and use it in GitHub Desktop.
<?php
namespace TabletopWargaming\ValueObject\Army\ArmyList;
use \FilterIterator;
use \Iterator;
use \TabletopWargaming\ValueObject\Army\ArmyList;
class ListIterator extends FilterIterator
{
public function __construct(Iterator $iterator, Filter $filter = null) {
$this->filter = $filter;
parent::__construct($iterator);
}
public function accept()
{
$accept = true;
$unit = $this->current();
if ($filter = $this->getFilter()) {
$accept = $filter->filter($unit);
}
return $accept;
}
public function getFilter()
{
return $this->filter;
}
public function current()
{
return $this->getInnerIterator()->current();
}
public function key() {
return $this->getInnerIterator()->key();
}
public function next()
{
return $this->getInnerIterator()->next();
}
public function rewind()
{
return $this->getInnerIterator()->rewind();
}
public function valid()
{
return $this->getInnerIterator()->valid();
}
}
<?php
namespace spec\TabletopWargaming\ValueObject\Army\ArmyList;
use \ArrayIterator;
use \RecursiveIterator;
use \PhpSpec\ObjectBehavior;
use \Prophecy\Argument;
use \TabletopWargaming\ValueObject\Army\ArmyList\Filter;
use \TabletopWargaming\ValueObject\Army\ArmyList\ListIterator;
use \TabletopWargaming\ValueObject\Unit;
class ListIteratorSpec extends ObjectBehavior
{
function it_does_filter(ArrayIterator $it, Filter $filter, Unit $unit)
{
$it->current()->willReturn($unit);
$filter->filter($unit)->willReturn(false);
$this->beConstructedWith($it, $filter);
$this->accept()->shouldReturn(false);
}
function it_iterates_over_units(ArrayIterator $it, Unit $unit)
{
$it->current()->willReturn($unit);
$this->beConstructedWith($it);
$this->current()->shouldReturn($unit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment