iterator_002.phpt 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. --TEST--
  2. SPL: Iterator using getInnerIterator
  3. --FILE--
  4. <?php
  5. class RecursiceArrayIterator extends ArrayIterator implements RecursiveIterator
  6. {
  7. function hasChildren(): bool
  8. {
  9. return is_array($this->current());
  10. }
  11. function getChildren(): RecursiceArrayIterator
  12. {
  13. return new RecursiceArrayIterator($this->current());
  14. }
  15. }
  16. class CrashIterator extends FilterIterator implements RecursiveIterator
  17. {
  18. function accept(): bool
  19. {
  20. return true;
  21. }
  22. function hasChildren(): bool
  23. {
  24. return $this->getInnerIterator()->hasChildren();
  25. }
  26. function getChildren(): RecursiceArrayIterator
  27. {
  28. return new RecursiceArrayIterator($this->getInnerIterator()->current());
  29. }
  30. }
  31. $array = array(1, 2 => array(21, 22 => array(221, 222), 23 => array(231)), 3);
  32. $dir = new RecursiveIteratorIterator(new CrashIterator(new RecursiceArrayIterator($array)), RecursiveIteratorIterator::LEAVES_ONLY);
  33. foreach ($dir as $file) {
  34. print "$file\n";
  35. }
  36. ?>
  37. --EXPECT--
  38. 1
  39. 21
  40. 221
  41. 222
  42. 231
  43. 3