PHP Australia Conference 2015

A classe CachingIterator

Introdução

...

Sinopse da classe

CachingIterator extends IteratorIterator implements OuterIterator , Traversable , Iterator , ArrayAccess , Countable {
/* Métodos */
public __construct ( Iterator $iterator [, string $flags = self::CALL_TOSTRING ] )
public int count ( void )
public void current ( void )
public void getCache ( void )
public void getFlags ( void )
public Iterator getInnerIterator ( void )
bool hasNext ( void )
public scalar key ( void )
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 )
void rewind ( void )
public void setFlags ( bitmask $flags )
string __toString ( void )
bool valid ( void )
}

Índice

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