The CachingIterator class

Introduction

This object supports cached iteration over another iterator.

Class synopsis

CachingIterator extends IteratorIterator implements OuterIterator , Traversable , Iterator , ArrayAccess , Countable {
/* Constants */
const integer CachingIterator::CALL_TOSTRING = 1 ;
const integer CachingIterator::CATCH_GET_CHILD = 16 ;
const integer CachingIterator::FULL_CACHE = 256 ;
/* Methods */
__construct ( Iterator $iterator [, string $flags ] )
public int count ( void )
public void current ( void )
public void getCache ( void )
public void getFlags ( void )
public void getInnerIterator ( void )
public void hasNext ( void )
public void key ( void )
public void next ( void )
public void offsetExists ( string $index )
public void offsetGet ( string $index )
public void offsetSet ( string $index , string $newval )
public void offsetUnset ( string $index )
public void rewind ( void )
public void setFlags ( bitmask $flags )
public void __toString ( void )
public void valid ( void )
}

Predefined Constants

CachingIterator Node Types

CachingIterator::CALL_TOSTRING

Description here...

CachingIterator::CATCH_GET_CHILD

Description here...

CachingIterator::TOSTRING_USE_KEY

Description here...

CachingIterator::TOSTRING_USE_CURRENT

Description here...

CachingIterator::TOSTRING_USE_INNER

Description here...

CachingIterator::FULL_CACHE

Cache all read data.

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
1
lsroudi at gmail dot com
8 months ago
<?php

/**
* Description of CachingIterator
*
* (c) lsroudi http://lsroudi.com/ <lsroudi@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OOPSPL\Iterator\Caching;

use
OOPSPL\Iterator\ArrayIterator\ArrayIterator;

class
CachingIterator {

    private
$numberIterator;
    private
$cachingarrayiterator;

   
/**
     *
     * @param type $b
     * @param type $l
     */
   
public function generateRangeNumber($b, $l)
    {
       
$this->numberIterator = new ArrayIterator(range($b, $l));
    }

   
/**
     *
     * @param \ArrayIterator $it
     */
   
public function addIteratorToCache(\ArrayIterator $it)
    {
       
$this->cachingarrayiterator = new \CachingIterator($it);
    }

   
/**
     *
     * @return \ArrayIterator
     */
   
public function getRangeNumberIterator()
    {
        return
$this->numberIterator;
    }

   
/**
     *
     * @return \CachingIterator
     */
   
public function getCachingArrayIterator()
    {
        return
$this->cachingarrayiterator;
    }

}
?>

in this example i show you how you can use CachingIterator to deal with the next element

<?php

/**
* Description of CachingIteratorTest
*
* (c) lsroudi http://lsroudi.com/ <lsroudi@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require 'vendor/autoload.php';

use
OOPSPL\Iterator\Caching\CachingIterator;

class
CachingIteratorTest extends \PHPUnit_Framework_TestCase {

    public function
testArrayIterator()
    {
       
$it = new CachingIterator();
       
$it->generateRangeNumber(1, 3);

       
$this->assertEquals(3, $it->getRangeNumberIterator()->count());
       
       
$it->addIteratorToCache($it->getRangeNumberIterator());
       
$ci = $it->getCachingArrayIterator();
       
        foreach (
$ci as $v)
        {
            if (!
$ci->hasNext()){
               
$this->assertEquals(NULL, $ci->next());
            }
        }
    }

}
?>
To Top