ConFoo: Call for paper is now Open

La classe CachingIterator

(PHP 5)

Introduction

Cet objet supporte les itérations en cache sur un autre itérateur.

Synopsis de la classe

CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/* Constantes */
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 ;
/* Méthodes */
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 )
}

Constantes pré-définies

CachingIterator::CALL_TOSTRING

Convertit tous les éléments en chaines.

CachingIterator::CATCH_GET_CHILD

N'envoie aucune exception lors de la tentative d'accéder à un enfant.

CachingIterator::TOSTRING_USE_KEY

Utilise key lors de la conversion en chaine.

CachingIterator::TOSTRING_USE_CURRENT

Utilise current lors de la conversion en chaine.

CachingIterator::TOSTRING_USE_INNER

Utilise inner lors de la conversion en chaine.

CachingIterator::FULL_CACHE

Met en cache toutes les données lues.

Sommaire

add a note add a note

User Contributed Notes 1 note

up
1
lsroudi at gmail dot com
5 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