CKTIS / PHP
CachingIterator
Wrap an Iterator with one-element lookahead. hasNext reports whether another item follows the current one, useful for separators without trailing punctuation.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
new CachingIterator($iterator, CachingIterator::FULL_CACHE)Wrap an Iterator with one-element lookahead. hasNext reports whether another item follows the current one, useful for separators without trailing punctuation. FULL_CACHE retains visited key-value pairs for getCache; enabling it consumes memory and repeated keys overwrite earlier cached values.
$it=new CachingIterator(new ArrayIterator(["a"=>2,"b"=>3]),CachingIterator::FULL_CACHE);
$look=[]; foreach($it as $k=>$v){$look[]=[$k,$it->hasNext()];}
echo json_encode([$look,$it->getCache()]);
[[["a",true],["b",false]],{"a":2,"b":3}]
Examples
<?php
$it=new CachingIterator(new ArrayIterator(["a"=>2,"b"=>3]),CachingIterator::FULL_CACHE);
$look=[]; foreach($it as $k=>$v){$look[]=[$k,$it->hasNext()];}
echo json_encode([$look,$it->getCache()]);
Result
[[["a",true],["b",false]],{"a":2,"b":3}]