mirror of
https://github.com/php/php-src.git
synced 2024-10-22 16:57:05 +00:00
90 lines
1.4 KiB
Plaintext
90 lines
1.4 KiB
Plaintext
|
--TEST--
|
||
|
SPL: RecursiveIteratorIterator and beginChildren/endChildren
|
||
|
--FILE--
|
||
|
<?php
|
||
|
|
||
|
class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator
|
||
|
{
|
||
|
function hasChildren()
|
||
|
{
|
||
|
return is_array($this->current());
|
||
|
}
|
||
|
|
||
|
function getChildren()
|
||
|
{
|
||
|
return new RecursiveArrayIterator($this->current());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Menu extends ArrayObject
|
||
|
{
|
||
|
function getIterator()
|
||
|
{
|
||
|
echo __METHOD__ . "\n";
|
||
|
return new RecursiveArrayIterator($this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class MenuOutput extends RecursiveIteratorIterator
|
||
|
{
|
||
|
function __construct(Menu $it)
|
||
|
{
|
||
|
parent::__construct($it);
|
||
|
}
|
||
|
function rewind()
|
||
|
{
|
||
|
echo "<ul>\n";
|
||
|
parent::rewind();
|
||
|
}
|
||
|
function beginChildren()
|
||
|
{
|
||
|
echo str_repeat(' ',$this->getDepth())."<ul>\n";
|
||
|
}
|
||
|
|
||
|
function endChildren()
|
||
|
{
|
||
|
echo str_repeat(' ',$this->getDepth()+1)."</ul>\n";
|
||
|
}
|
||
|
function valid()
|
||
|
{
|
||
|
if (!parent::valid()) {
|
||
|
echo "<ul>\n";
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$arr = array("a", array("ba", array("bba", "bbb"), array(array("bcaa"))), array("ca"), "d");
|
||
|
$obj = new Menu($arr);
|
||
|
$rit = new MenuOutput($obj);
|
||
|
foreach($rit as $k=>$v)
|
||
|
{
|
||
|
echo str_repeat(' ',$rit->getDepth()+1)."$k=>$v\n";
|
||
|
}
|
||
|
?>
|
||
|
===DONE===
|
||
|
<?php exit(0); ?>
|
||
|
--EXPECTF--
|
||
|
Menu::getIterator
|
||
|
<ul>
|
||
|
0=>a
|
||
|
<ul>
|
||
|
0=>ba
|
||
|
<ul>
|
||
|
0=>bba
|
||
|
1=>bbb
|
||
|
</ul>
|
||
|
<ul>
|
||
|
<ul>
|
||
|
0=>bcaa
|
||
|
</ul>
|
||
|
</ul>
|
||
|
</ul>
|
||
|
<ul>
|
||
|
0=>ca
|
||
|
</ul>
|
||
|
3=>d
|
||
|
<ul>
|
||
|
===DONE===
|