tmgmt_file.recursive_iterator.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @file
  4. * Contains RecursiveDOMIterator.
  5. */
  6. /**
  7. * Class used to iterate through DOMDocument.
  8. */
  9. class RecursiveDOMIterator implements RecursiveIterator {
  10. /**
  11. * Current position in DOMNodeList.
  12. *
  13. * @var int
  14. */
  15. protected $position;
  16. /**
  17. * The DOMNodeList with all children to iterate over.
  18. *
  19. * @var DOMNodeList
  20. */
  21. protected $nodeList;
  22. /**
  23. * Constructor.
  24. *
  25. * @param DOMNode $domNode
  26. * DOMNode to iterate over.
  27. */
  28. public function __construct(DOMNode $domNode) {
  29. $this->position = 0;
  30. $this->nodeList = $domNode->childNodes;
  31. }
  32. /**
  33. * Returns the current DOMNode.
  34. *
  35. * @return DOMNode
  36. * Current DOMNode object.
  37. */
  38. public function current() {
  39. return $this->nodeList->item($this->position);
  40. }
  41. /**
  42. * Returns an iterator for the current iterator entry.
  43. *
  44. * @return RecursiveDOMIterator
  45. * Iterator with children elements.
  46. */
  47. public function getChildren() {
  48. return new self($this->current());
  49. }
  50. /**
  51. * Checks if current element has children.
  52. *
  53. * @return bool
  54. * Has children.
  55. */
  56. public function hasChildren() {
  57. return $this->current()->hasChildNodes();
  58. }
  59. /**
  60. * Returns the current position.
  61. *
  62. * @return int
  63. * Current position
  64. */
  65. public function key() {
  66. return $this->position;
  67. }
  68. /**
  69. * Moves the current position to the next element.
  70. */
  71. public function next() {
  72. $this->position++;
  73. }
  74. /**
  75. * Rewind the Iterator to the first element.
  76. */
  77. public function rewind() {
  78. $this->position = 0;
  79. }
  80. /**
  81. * Checks if current position is valid.
  82. *
  83. * @return bool
  84. * Is valid.
  85. */
  86. public function valid() {
  87. return $this->position < $this->nodeList->length;
  88. }
  89. }