SunshinePHP Developer Conference 2015

La clase CachingIterator

(PHP 5)

Introducción

Este objeto admite iteraciones almacenadas en caché sobre otros iteradores.

Sinopsis de la Clase

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étodos */
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 predefinidas

CachingIterator::CALL_TOSTRING

Convierte cada elemento a un string.

CachingIterator::CATCH_GET_CHILD

No lanzar una excepción en el acceso a los hijos.

CachingIterator::TOSTRING_USE_KEY

Usa key para la conversión a string.

CachingIterator::TOSTRING_USE_CURRENT

Usa current para la conversión a string.

CachingIterator::TOSTRING_USE_INNER

Usa inner para la conversión a string.

CachingIterator::FULL_CACHE

Almacena en caché todos los datos leídos.

Tabla de contenidos

add a note add a note

User Contributed Notes 1 note

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