Annotation of embedaddon/php/ext/spl/tests/iterator_023.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: SPL: RecursiveIteratorIterator and catch getChildren
! 3: --FILE--
! 4: <?php
! 5:
! 6: class MyRecursiveArrayIterator extends RecursiveArrayIterator
! 7: {
! 8: function getChildren()
! 9: {
! 10: echo __METHOD__ . "\n";
! 11: return $this->current();
! 12: }
! 13:
! 14: function valid()
! 15: {
! 16: if (!parent::valid())
! 17: {
! 18: echo __METHOD__ . " = false\n";
! 19: return false;
! 20: }
! 21: else
! 22: {
! 23: return true;
! 24: }
! 25: }
! 26: }
! 27:
! 28: class 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, RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD);
! 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 callGetChildren()
! 93: {
! 94: if ($this->over == 2)
! 95: {
! 96: echo __METHOD__ . "(throw)\n";
! 97: throw new Exception("Thrown in callGetChildren()");
! 98: }
! 99: echo __METHOD__ . "(ok:{$this->over})\n";
! 100: return new MyRecursiveArrayIterator($this->current());
! 101: }
! 102:
! 103: function beginChildren()
! 104: {
! 105: echo __METHOD__ . "(".$this->getDepth().")\n";
! 106: }
! 107:
! 108: function endChildren()
! 109: {
! 110: echo __METHOD__ . "(".$this->getDepth().")\n";
! 111: }
! 112: }
! 113:
! 114: try
! 115: {
! 116: foreach(new RecursiveArrayIteratorIterator(new MyRecursiveArrayIterator(array("a", array("ba", array("bba", "bbb"), array(array("bcaa"), array("bcba"))), array("ca"), "d")), 2) as $k=>$v)
! 117: {
! 118: if (is_array($v)) $v = join('',$v);
! 119: echo "$k=>$v\n";
! 120: }
! 121: }
! 122: catch(UnexpectedValueException $e)
! 123: {
! 124: echo $e->getMessage() . "\n";
! 125: }
! 126:
! 127: ?>
! 128: ===DONE===
! 129: <?php exit(0); ?>
! 130: --EXPECT--
! 131: RecursiveArrayIteratorIterator::rewind
! 132: RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
! 133: RecursiveArrayIteratorIterator::valid
! 134: RecursiveArrayIteratorIterator::current
! 135: RecursiveArrayIteratorIterator::key
! 136: 0=>a
! 137: RecursiveArrayIteratorIterator::next
! 138: RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
! 139: RecursiveArrayIteratorIterator::callGetChildren(ok:0)
! 140: RecursiveArrayIteratorIterator::current
! 141: RecursiveArrayIteratorIterator::beginChildren(1)
! 142: RecursiveArrayIteratorIterator::callHasChildren(1) = no/no
! 143: RecursiveArrayIteratorIterator::valid
! 144: RecursiveArrayIteratorIterator::current
! 145: RecursiveArrayIteratorIterator::key
! 146: 0=>ba
! 147: RecursiveArrayIteratorIterator::next
! 148: RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
! 149: RecursiveArrayIteratorIterator::callGetChildren(ok:0)
! 150: RecursiveArrayIteratorIterator::current
! 151: RecursiveArrayIteratorIterator::beginChildren(2)
! 152: RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
! 153: RecursiveArrayIteratorIterator::valid
! 154: RecursiveArrayIteratorIterator::current
! 155: RecursiveArrayIteratorIterator::key
! 156: 0=>bba
! 157: RecursiveArrayIteratorIterator::next
! 158: RecursiveArrayIteratorIterator::callHasChildren(2) = no/no
! 159: RecursiveArrayIteratorIterator::valid
! 160: RecursiveArrayIteratorIterator::current
! 161: RecursiveArrayIteratorIterator::key
! 162: 1=>bbb
! 163: RecursiveArrayIteratorIterator::next
! 164: MyRecursiveArrayIterator::valid = false
! 165: RecursiveArrayIteratorIterator::endChildren(2)
! 166: RecursiveArrayIteratorIterator::callHasChildren(1) = yes/yes
! 167: RecursiveArrayIteratorIterator::callGetChildren(ok:0)
! 168: RecursiveArrayIteratorIterator::current
! 169: RecursiveArrayIteratorIterator::beginChildren(2)
! 170: RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
! 171: RecursiveArrayIteratorIterator::valid
! 172: RecursiveArrayIteratorIterator::current
! 173: RecursiveArrayIteratorIterator::key
! 174: 0=>bcaa
! 175: RecursiveArrayIteratorIterator::next
! 176: RecursiveArrayIteratorIterator::callHasChildren(2) = no/yes
! 177: RecursiveArrayIteratorIterator::valid
! 178: RecursiveArrayIteratorIterator::next
! 179: MyRecursiveArrayIterator::valid = false
! 180: RecursiveArrayIteratorIterator::endChildren(2)
! 181: MyRecursiveArrayIterator::valid = false
! 182: RecursiveArrayIteratorIterator::endChildren(1)
! 183: RecursiveArrayIteratorIterator::callHasChildren(0) = yes/yes
! 184: RecursiveArrayIteratorIterator::callGetChildren(throw)
! 185: RecursiveArrayIteratorIterator::callHasChildren(0) = no/no
! 186: RecursiveArrayIteratorIterator::current
! 187: RecursiveArrayIteratorIterator::key
! 188: 3=>d
! 189: RecursiveArrayIteratorIterator::next
! 190: MyRecursiveArrayIterator::valid = false
! 191: RecursiveArrayIteratorIterator::valid
! 192: MyRecursiveArrayIterator::valid = false
! 193: ===DONE===
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>