1--TEST-- 2SPL: RecursiveIteratorIterator and hasChildren 3--FILE-- 4<?php 5 6class MyRecursiveArrayIterator extends RecursiveArrayIterator 7{ 8 function valid() 9 { 10 if (!parent::valid()) 11 { 12 echo __METHOD__ . " = false\n"; 13 return false; 14 } 15 else 16 { 17 return true; 18 } 19 } 20 21 function getChildren() 22 { 23 echo __METHOD__ . "\n"; 24 return parent::getChildren(); 25 } 26} 27 28class RecursiveArrayIteratorIterator extends RecursiveIteratorIterator 29{ 30 private $max_depth; 31 private $over = 0; 32 private $skip = false; 33 34 function __construct($it, $max_depth) 35 { 36 $this->max_depth = $max_depth; 37 parent::__construct($it); 38 } 39 40 function rewind() 41 { 42 echo __METHOD__ . "\n"; 43 $this->skip = false; 44 parent::rewind(); 45 } 46 47 function valid() 48 { 49 echo __METHOD__ . "\n"; 50 if ($this->skip) 51 { 52 $this->skip = false; 53 $this->next(); 54 } 55 return parent::valid(); 56 } 57 58 function current() 59 { 60 echo __METHOD__ . "\n"; 61 return parent::current(); 62 } 63 64 function key() 65 { 66 echo __METHOD__ . "\n"; 67 return parent::key(); 68 } 69 70 function next() 71 { 72 echo __METHOD__ . "\n"; 73 parent::next(); 74 } 75 76 function callHasChildren() 77 { 78 $this->skip = false; 79 $has = parent::callHasChildren(); 80 $res = $this->getDepth() < $this->max_depth && $has; 81 echo __METHOD__ . "(".$this->getDepth().") = ".($res?"yes":"no")."/".($has?"yes":"no")."\n"; 82 if ($has && !$res) 83 { 84 $this->over++; 85 if ($this->over == 2) { 86 $this->skip = true; 87 } 88 } 89 return $res; 90 } 91 92 function beginChildren() 93 { 94 echo __METHOD__ . "(".$this->getDepth().")\n"; 95 } 96 97 function endChildren() 98 { 99 echo __METHOD__ . "(".$this->getDepth().")\n"; 100 } 101} 102 103foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v) 104{ 105 if (is_array($v)) $v = join('',$v); 106 echo "$k=>$v\n"; 107} 108?> 109--EXPECT-- 110RecursiveArrayIteratorIterator::rewind 111RecursiveArrayIteratorIterator::callHasChildren(0) = no/no 112RecursiveArrayIteratorIterator::valid 113RecursiveArrayIteratorIterator::current 114RecursiveArrayIteratorIterator::key 1150=>a 116RecursiveArrayIteratorIterator::next 117RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes 118MyRecursiveArrayIterator::getChildren 119RecursiveArrayIteratorIterator::beginChildren(1) 120RecursiveArrayIteratorIterator::callHasChildren(1) = no/no 121RecursiveArrayIteratorIterator::valid 122RecursiveArrayIteratorIterator::current 123RecursiveArrayIteratorIterator::key 1240=>ba 125RecursiveArrayIteratorIterator::next 126RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes 127MyRecursiveArrayIterator::getChildren 128RecursiveArrayIteratorIterator::beginChildren(2) 129RecursiveArrayIteratorIterator::callHasChildren(2) = no/no 130RecursiveArrayIteratorIterator::valid 131RecursiveArrayIteratorIterator::current 132RecursiveArrayIteratorIterator::key 1330=>bba 134RecursiveArrayIteratorIterator::next 135RecursiveArrayIteratorIterator::callHasChildren(2) = no/no 136RecursiveArrayIteratorIterator::valid 137RecursiveArrayIteratorIterator::current 138RecursiveArrayIteratorIterator::key 1391=>bbb 140RecursiveArrayIteratorIterator::next 141MyRecursiveArrayIterator::valid = false 142RecursiveArrayIteratorIterator::endChildren(2) 143RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes 144MyRecursiveArrayIterator::getChildren 145RecursiveArrayIteratorIterator::beginChildren(2) 146RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes 147RecursiveArrayIteratorIterator::valid 148RecursiveArrayIteratorIterator::current 149RecursiveArrayIteratorIterator::key 1500=>bcaa 151RecursiveArrayIteratorIterator::next 152RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes 153RecursiveArrayIteratorIterator::valid 154RecursiveArrayIteratorIterator::next 155MyRecursiveArrayIterator::valid = false 156RecursiveArrayIteratorIterator::endChildren(2) 157MyRecursiveArrayIterator::valid = false 158RecursiveArrayIteratorIterator::endChildren(1) 159RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes 160MyRecursiveArrayIterator::getChildren 161RecursiveArrayIteratorIterator::beginChildren(1) 162RecursiveArrayIteratorIterator::callHasChildren(1) = no/no 163RecursiveArrayIteratorIterator::current 164RecursiveArrayIteratorIterator::key 1650=>ca 166RecursiveArrayIteratorIterator::next 167MyRecursiveArrayIterator::valid = false 168RecursiveArrayIteratorIterator::endChildren(1) 169RecursiveArrayIteratorIterator::callHasChildren(0) = no/no 170RecursiveArrayIteratorIterator::valid 171RecursiveArrayIteratorIterator::current 172RecursiveArrayIteratorIterator::key 1733=>d 174RecursiveArrayIteratorIterator::next 175MyRecursiveArrayIterator::valid = false 176RecursiveArrayIteratorIterator::valid 177MyRecursiveArrayIterator::valid = false 178