Cursor.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Cursor implements OuterIterator
  10. {
  11. protected $_cursor = null;
  12. protected $_config = array();
  13. public function __construct(MongoCursor $cursor, $config)
  14. {
  15. $this->_cursor = $cursor;
  16. $this->_config = $config;
  17. }
  18. /**
  19. * Get the inter iterator
  20. *
  21. * @return MongoCursor
  22. */
  23. public function getInnerIterator()
  24. {
  25. return $this->_cursor;
  26. }
  27. /**
  28. * Get the document class
  29. *
  30. * @return string
  31. */
  32. public function getDocumentClass()
  33. {
  34. return $this->_config['documentClass'];
  35. }
  36. /**
  37. * Get the document set class
  38. *
  39. * @return string
  40. */
  41. public function getDocumentSetClass()
  42. {
  43. return $this->_config['documentSetClass'];
  44. }
  45. /**
  46. * Export all data
  47. *
  48. * @return array
  49. */
  50. public function export()
  51. {
  52. $this->rewind();
  53. return iterator_to_array($this->getInnerIterator());
  54. }
  55. /**
  56. * Construct a document set from this cursor
  57. *
  58. * @return Mooses_Mongodb_Mongo_DocumentSet
  59. */
  60. public function makeDocumentSet()
  61. {
  62. $config = array();
  63. $config['new'] = false;
  64. $config['hasId'] = false;
  65. $config['connectionGroup'] = $this->_config['connectionGroup'];
  66. $config['db'] = $this->_config['db'];
  67. $config['collection'] = $this->_config['collection'];
  68. $config['requirementModifiers'] = array(
  69. Mooses_Mongodb_Mongo_DocumentSet::DYNAMIC_INDEX => array("Document:".$this->getDocumentClass())
  70. );
  71. $documentSetClass = $this->getDocumentSetClass();
  72. return new $documentSetClass($this->export(), $config);
  73. }
  74. /**
  75. * Get the current value
  76. *
  77. * @return mixed
  78. */
  79. public function current()
  80. {
  81. $data = $this->getInnerIterator()->current();
  82. if ($data === null) {
  83. return null;
  84. }
  85. $config = array();
  86. $config['new'] = false;
  87. $config['hasKey'] = true;
  88. $config['connectionGroup'] = $this->_config['connectionGroup'];
  89. $config['db'] = $this->_config['db'];
  90. $config['collection'] = $this->_config['collection'];
  91. $documentClass = $this->getDocumentClass();
  92. if (!empty($data['_type'][0])) {
  93. $documentClass = $data['_type'][0];
  94. }
  95. return new $documentClass($data, $config);
  96. }
  97. public function getNext()
  98. {
  99. $this->next();
  100. return $this->current();
  101. }
  102. public function key()
  103. {
  104. return $this->getInnerIterator()->key();
  105. }
  106. public function next()
  107. {
  108. return $this->getInnerIterator()->next();
  109. }
  110. public function rewind()
  111. {
  112. return $this->getInnerIterator()->rewind();
  113. }
  114. public function valid()
  115. {
  116. return $this->getInnerIterator()->valid();
  117. }
  118. public function count($all = false)
  119. {
  120. return $this->getInnerIterator()->count($all);
  121. }
  122. public function info()
  123. {
  124. return $this->getInnerIterator()->info();
  125. }
  126. public function __call($method, $arguments)
  127. {
  128. // Forward the call to the MongoCursor
  129. $res = call_user_func_array(array($this->getInnerIterator(), $method), $arguments);
  130. // Allow chaining
  131. if ($res instanceof MongoCursor) {
  132. return $this;
  133. }
  134. return $res;
  135. }
  136. }