The CachingIterator class

(PHP 5)

Introduction

This object supports cached iteration over another iterator.

Class synopsis

CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/* Constants */
const integer CALL_TOSTRING = 1 ;
const integer CATCH_GET_CHILD = 16 ;
const integer TOSTRING_USE_KEY = 2 ;
const integer TOSTRING_USE_CURRENT = 4 ;
const integer TOSTRING_USE_INNER = 8 ;
const integer FULL_CACHE = 256 ;
/* Methods */
public __construct ( Iterator $iterator [, string $flags = self::CALL_TOSTRING ] )
public int count ( void )
public void current ( void )
public array getCache ( void )
public void getFlags ( void )
public Iterator getInnerIterator ( void )
public void hasNext ( void )
public scalar 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::CALL_TOSTRING

Convert every element to string.

CachingIterator::CATCH_GET_CHILD

Don't throw exception in accessing children.

CachingIterator::TOSTRING_USE_KEY

Use key for conversion to string.

CachingIterator::TOSTRING_USE_CURRENT

Use current for conversion to string.

CachingIterator::TOSTRING_USE_INNER

Use inner for conversion to string.

CachingIterator::FULL_CACHE

Cache all read data.

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
0
lsroudi at gmail dot com
13 days 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