<?php
namespace young;class InnerIterator implements \Iterator{
private $dates;
private $position;
public function __construct($dates = [])
{
$this->dates = $dates;
$this->position = 0;
}
public function rewind()
{
echo 'call ' . __METHOD__ . '<br>';
reset($this->dates);
}
public function valid()
{
echo 'call ' . __METHOD__ . '<br>';
if ($this->position >= count($this->dates)) {
# code...
return false;
}
return true;
}
public function current()
{
echo 'call ' . __METHOD__ . '<br>';
return $this->dates[$this->position];
}
public function key()
{
echo 'call ' . __METHOD__ . '<br>';
return $this->position;
}
public function next()
{
echo 'call ' . __METHOD__ . '<br>';
++$this->position;
}}class OuterIterator extends \IteratorIterator{
function rewind()
{
echo __METHOD__ . '<br>';
return parent::rewind();
}
function valid()
{
echo __METHOD__ . '<br>';
return parent::valid();
}
function current()
{
echo __METHOD__ . '<br>';
return parent::current() . '_suffix';
}
function key()
{
echo __METHOD__ . '<br>';
return parent::key() ;
}
function next()
{
echo __METHOD__ . '<br>';
return parent::next() ;
}
function getInnerIterator()
{
echo __METHOD__ . '<br>';
return parent::getInnerIterator();
}}$tmpArr = array(
'2018-10-01',
'2018-10-02',
//'2018-10-03',);$inner = new InnerIterator($tmpArr);$outer = new OuterIterator($inner);foreach ($outer as $key => $value) {
# code...
echo $key , '=>' , $value . '<hr>';}