Extending the IteratorIterator class to get rid of the implementation of the entire iterator interface and / or to create an iterator decorator, I also came across this.
This decorator is already a solution to the problem, he only needs to implement the missing functionality to eliminate inconsistency. No need to rewind:
class IteratorDecorator extends IteratorIterator { public function valid() { return $this->getInnerIterator()->valid(); } }
Example. If you have an Iterator object that is valid by default, for example. ArrayIterator :
$it = new ArrayIterator(array(1)); var_dump($it->valid());
This indicates inconsistency in the implementation of the IteratorIterator implementation; the IteratorIterator object incorrectly reflects the internal state of the ArrayIterator . Using IteratorDecorator can fix this:
$decor = new IteratorDecorator($it); var_dump($decor->valid());
And if you have traced so far, here is another special case that you can consider: If you don't need to have rewind with an internal iterator, you can simply use NoRewindIterator , which returns the correctness as well:
$noretit = new NoRewindIterator($it); var_dump($noretit->valid());
Johannes’s “no automatic rewind” arguments make sense because NoRewindIterator expects the iterator to not rewind and correctly displays the correctness of the internal iterator.
But, as IteratorDecorator shows, I also do not automatically rewind to remove inconsistency.
hakre
source share