forked from canni/YiiMongoDbSuite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EMongoCursor.php
148 lines (134 loc) · 2.95 KB
/
EMongoCursor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* EMongoCursor.php
*
* PHP version 5.2+
*
* @author Nagy Attila Gábor <[email protected]>
* @author Dariusz Górecki <[email protected]>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2010 CleverIT http://www.cleverit.com.pl
* @license http://www.yiiframework.com/license/ BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
*
*/
/**
* EMongoCursor
*
* Cursor object, that behaves much like the MongoCursor,
* but this one returns instantiated objects
*/
class EMongoCursor
implements Iterator, Countable
{
/**
* @var MongoCursor $_cursor the MongoCursor returned by the query
*/
protected $_cursor;
/**
* @var EMongoDocument $_model the model used for instantiating objects
*/
protected $_model;
/**
* Construct a new EMongoCursor
*
* @param MongoCursor $cursor the cursor returned by the query
* @param EMongoDocument $model the model for instantiating objects
*/
public function __construct(MongoCursor $cursor, EMongoDocument $model)
{
$this->_cursor = $cursor;
$this->_model = $model;
}
/**
* Return MongoCursor for additional tuning
*
* @return MongoCursor the cursor used for this query
*/
public function getCursor()
{
return $this->_cursor;
}
/**
* Return the current element
* @return EMongoDocument
*/
public function current()
{
$document = $this->_cursor->current();
if(empty($document))
return $document;
return $this->_model->populateRecord($document);
}
/**
* Return the key of the current element
* @return scalar
*/
public function key()
{
return $this->_cursor->key();
}
/**
* Move forward to next element
* @return void
*/
public function next()
{
$this->_cursor->next();
}
/**
* Rewind the Iterator to the first element
* @return void
*/
public function rewind()
{
$this->_cursor->rewind();
}
/**
* Checks if current position is valid
* @return boolean
*/
public function valid()
{
return $this->_cursor->valid();
}
/**
* Returns the number of documents found
* {@see http://www.php.net/manual/en/mongocursor.count.php}
* @param boolean $foundOnly default FALSE
* @return integer count of documents found
*/
public function count($foundOnly = false)
{
return $this->_cursor->count($foundOnly);
}
/**
* Apply a limit to this cursor
* {@see http://www.php.net/manual/en/mongocursor.limit.php}
* @param integer $limit new limit
*/
public function limit($limit)
{
$this->_cursor->limit($limit);
}
/**
* Skip a $offset records
* {@see http://www.php.net/manual/en/mongocursor.skip.php}
* @param integer $skip new skip
*/
public function offset($offset)
{
$this->_cursor->offset($offset);
}
/**
* Apply sorting directives
* {@see http://www.php.net/manual/en/mongocursor.sort.php}
* @param array $sort sorting directives
*/
public function sort(array $fields)
{
$this->_cursor->sort($fields);
}
}