Default.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @category Thinkopen
  4. * @package Mooses_Mongodb_Mongo
  5. * @copyright Thinkopen s.r.l.
  6. * @license New BSD License
  7. * @author Coen Hyde
  8. */
  9. class Mooses_Mongodb_Mongo_Iterator_Default implements SeekableIterator, RecursiveIterator, Countable
  10. {
  11. protected $_document = null;
  12. protected $_position = null;
  13. protected $_properties = array();
  14. protected $_init = false;
  15. protected $_counter = 0;
  16. public function __construct(Mooses_Mongodb_Mongo_Document $document)
  17. {
  18. $this->_document = $document;
  19. $this->_properties = $document->getPropertyKeys();
  20. $this->_position = current($this->_properties);
  21. reset($this->_properties);
  22. }
  23. /**
  24. * Get the document
  25. *
  26. * @return Mooses_Mongodb_Mongo_Document
  27. */
  28. public function getDocument()
  29. {
  30. return $this->_document;
  31. }
  32. /**
  33. * Get the properties
  34. *
  35. * @return array
  36. */
  37. public function getDocumentProperties()
  38. {
  39. return $this->_properties;
  40. }
  41. /**
  42. * Seek to a position
  43. *
  44. * @param unknown_type $position
  45. */
  46. public function seek($position)
  47. {
  48. $this->_position = $position;
  49. if (!$this->valid()) {
  50. throw new OutOfBoundsException("invalid seek position ($position)");
  51. }
  52. }
  53. /**
  54. * Get the current value
  55. *
  56. * @return mixed
  57. */
  58. public function current()
  59. {
  60. return $this->getDocument()->getProperty($this->key());
  61. }
  62. /**
  63. * Get the current key
  64. *
  65. * @return string
  66. */
  67. public function key()
  68. {
  69. return $this->_position;
  70. }
  71. /**
  72. * Move next
  73. */
  74. public function next()
  75. {
  76. next($this->_properties);
  77. $this->_position = current($this->_properties);
  78. $this->_counter = $this->_counter + 1;
  79. }
  80. /**
  81. * Rewind the iterator
  82. */
  83. public function rewind()
  84. {
  85. reset($this->_properties);
  86. $this->_position = current($this->_properties);
  87. }
  88. /**
  89. * Is the current position valid
  90. *
  91. * @return boolean
  92. */
  93. public function valid()
  94. {
  95. return in_array($this->key(), $this->_properties, true);
  96. }
  97. /**
  98. * Count all properties
  99. *
  100. * @return int
  101. */
  102. public function count()
  103. {
  104. return count($this->getDocumentProperties());
  105. }
  106. /**
  107. * Determine if the current node has an children
  108. *
  109. * @return boolean
  110. */
  111. public function hasChildren()
  112. {
  113. return ($this->current() instanceof Mooses_Mongodb_Mongo_Document);
  114. }
  115. /*
  116. * Get children
  117. *
  118. * @return RecursiveIterator
  119. */
  120. public function getChildren()
  121. {
  122. return $this->current()->getIterator();
  123. }
  124. }