| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * @category Thinkopen
- * @package Mooses_Mongodb_Mongo
- * @copyright Thinkopen s.r.l.
- * @license New BSD License
- * @author Coen Hyde
- */
- class Mooses_Mongodb_Mongo_Iterator_Cursor implements OuterIterator
- {
- protected $_cursor = null;
- protected $_config = array();
-
- public function __construct(MongoCursor $cursor, $config)
- {
- $this->_cursor = $cursor;
- $this->_config = $config;
- }
-
- /**
- * Get the inter iterator
- *
- * @return MongoCursor
- */
- public function getInnerIterator()
- {
- return $this->_cursor;
- }
-
- /**
- * Get the document class
- *
- * @return string
- */
- public function getDocumentClass()
- {
- return $this->_config['documentClass'];
- }
-
- /**
- * Get the document set class
- *
- * @return string
- */
- public function getDocumentSetClass()
- {
- return $this->_config['documentSetClass'];
- }
-
- /**
- * Export all data
- *
- * @return array
- */
- public function export()
- {
- $this->rewind();
- return iterator_to_array($this->getInnerIterator());
- }
-
- /**
- * Construct a document set from this cursor
- *
- * @return Mooses_Mongodb_Mongo_DocumentSet
- */
- public function makeDocumentSet()
- {
- $config = array();
- $config['new'] = false;
- $config['hasId'] = false;
- $config['connectionGroup'] = $this->_config['connectionGroup'];
- $config['db'] = $this->_config['db'];
- $config['collection'] = $this->_config['collection'];
- $config['requirementModifiers'] = array(
- Mooses_Mongodb_Mongo_DocumentSet::DYNAMIC_INDEX => array("Document:".$this->getDocumentClass())
- );
-
- $documentSetClass = $this->getDocumentSetClass();
- return new $documentSetClass($this->export(), $config);
- }
-
- /**
- * Get the current value
- *
- * @return mixed
- */
- public function current()
- {
- $data = $this->getInnerIterator()->current();
- if ($data === null) {
- return null;
- }
-
- $config = array();
- $config['new'] = false;
- $config['hasKey'] = true;
- $config['connectionGroup'] = $this->_config['connectionGroup'];
- $config['db'] = $this->_config['db'];
- $config['collection'] = $this->_config['collection'];
-
- $documentClass = $this->getDocumentClass();
- if (!empty($data['_type'][0])) {
- $documentClass = $data['_type'][0];
- }
-
- return new $documentClass($data, $config);
- }
-
- public function getNext()
- {
- $this->next();
- return $this->current();
- }
-
- public function key()
- {
- return $this->getInnerIterator()->key();
- }
-
- public function next()
- {
- return $this->getInnerIterator()->next();
- }
-
- public function rewind()
- {
- return $this->getInnerIterator()->rewind();
- }
-
- public function valid()
- {
- return $this->getInnerIterator()->valid();
- }
-
- public function count($all = false)
- {
- return $this->getInnerIterator()->count($all);
- }
-
- public function info()
- {
- return $this->getInnerIterator()->info();
- }
-
- public function __call($method, $arguments)
- {
- // Forward the call to the MongoCursor
- $res = call_user_func_array(array($this->getInnerIterator(), $method), $arguments);
-
- // Allow chaining
- if ($res instanceof MongoCursor) {
- return $this;
- }
-
- return $res;
- }
- }
|