gielfeldt/iterators

View on GitHub
src/FiniteIterator.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Gielfeldt\Iterators;

class FiniteIterator extends TraversableIterator
{
    private $endCondition;

    public function __construct(\Traversable $iterator, callable $endCondition = null)
    {
        $this->endCondition = $endCondition ? \Closure::fromCallable($endCondition) : null;
        parent::__construct($iterator);
    }

    public function valid()
    {
        return $this->endCondition ? !($this->endCondition)($this) : parent::valid();
    }
}