-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoCursor.php
96 lines (72 loc) · 2.36 KB
/
MongoCursor.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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017-11-15
* Time: 11:40
*/
namespace SDF\Db;
class MongoCursor implements \Iterator{
private $currenCursor = null;
private $queryProcess = array();
private $skip = 0;
private $limit = 0;
private $sort = array();
private $mongoCollection;
private $is_iterator;
public function __construct($mongoCollection,$is_iterator=true){
$this->mongoCollection = $mongoCollection;
$this->is_iterator = $is_iterator;
$this->skip = $mongoCollection->skip;
$this->limit = $mongoCollection->limit;
$this->sort = $mongoCollection->sort;
}
function rewind() {
$this->currenCursor = $this->is_query(true);
return $this->currenCursor->rewind();
}
function current() {
$this->currenCursor = $this->is_query();
return $this->mongoCollection->object_array($this->currenCursor->current());
}
function key() {
$this->currenCursor = $this->is_query();
return $this->currenCursor->key();
}
function next() {
$this->currenCursor = $this->is_query();
return $this->currenCursor->next();
}
function valid() {
$this->currenCursor = $this->is_query();
return $this->currenCursor->valid();
}
/**
* 获取查询的游标对象
* @param bool $is_iterator
* @return mixed
*/
public function is_query($is_new=false){
$key_str = md5(serialize($this->mongoCollection->filter).$this->mongoCollection->databaseName.$this->mongoCollection->collectionName);
if($is_new || !isset($this->queryProcess[$key_str])){
$cursor = $this->mongoCollection->findMany($this->mongoCollection->filter,$this->mongoCollection->projection,$this->limit,$this->skip,$this->sort);
$this->queryProcess[$key_str] = $this->is_iterator ? new \IteratorIterator($cursor) : $cursor;
}
return $this->queryProcess[$key_str];
}
public function count(){
return $this->mongoCollection->count($this->mongoCollection->filter);
}
public function sort($sort){
$this->sort = $sort;
return $this;
}
public function skip($skip){
$this->skip = $skip;
return $this;
}
public function limit($limit){
$this->limit = $limit;
return $this;
}
}