|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHPMongo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sokil\Mongo; |
|
13
|
|
|
|
|
14
|
|
|
class ResultSet implements \Iterator, \Countable, \ArrayAccess |
|
15
|
|
|
{ |
|
16
|
|
|
protected $documents = array(); |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(array $documents = null) |
|
19
|
|
|
{ |
|
20
|
|
|
if ($documents) { |
|
21
|
|
|
$this->documents = $documents; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function map($callable) |
|
26
|
|
|
{ |
|
27
|
|
|
return new ResultSet(array_map($callable, $this->documents)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function filter($callable) |
|
31
|
|
|
{ |
|
32
|
|
|
return new ResultSet(array_filter($this->documents, $callable)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function each($callable) |
|
36
|
|
|
{ |
|
37
|
|
|
foreach ($this->documents as $id => $document) { |
|
38
|
|
|
call_user_func($callable, $document, $id, $this); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param callable $callable apply arguments [$accumulator, $document] |
|
47
|
|
|
* @param mixed $initial |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function reduce($callable, $initial = null) |
|
51
|
|
|
{ |
|
52
|
|
|
return array_reduce($this->documents, $callable, $initial); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Count documents in result set |
|
57
|
|
|
* @return int count |
|
58
|
|
|
*/ |
|
59
|
|
|
public function count() |
|
60
|
|
|
{ |
|
61
|
|
|
return count($this->documents); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function keys() |
|
65
|
|
|
{ |
|
66
|
|
|
return array_keys($this->documents); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function values() |
|
70
|
|
|
{ |
|
71
|
|
|
return array_values($this->documents); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return the current element |
|
76
|
|
|
* @return array|\Sokil\Mongo\Document |
|
77
|
|
|
*/ |
|
78
|
|
|
public function current() |
|
79
|
|
|
{ |
|
80
|
|
|
return current($this->documents); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Move forward to next element |
|
85
|
|
|
* @return void Any returned value is ignored. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function next() |
|
88
|
|
|
{ |
|
89
|
|
|
next($this->documents); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the key of the current element |
|
94
|
|
|
* @return scalar scalar on success, or NULL on failure. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function key() |
|
97
|
|
|
{ |
|
98
|
|
|
return key($this->documents); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Checks if current position is valid |
|
103
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
104
|
|
|
* Returns TRUE on success or FALSE on failure. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function valid() |
|
107
|
|
|
{ |
|
108
|
|
|
return key($this->documents) !== null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Rewind the Iterator to the first element |
|
113
|
|
|
* @return void Any returned value is ignored. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function rewind() |
|
116
|
|
|
{ |
|
117
|
|
|
reset($this->documents); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Whether a offset exists |
|
122
|
|
|
* @param mixed $offset |
|
123
|
|
|
* An offset to check for. |
|
124
|
|
|
* @return boolean TRUE on success or FALSE on failure. |
|
125
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function offsetExists($offset) |
|
128
|
|
|
{ |
|
129
|
|
|
return array_key_exists($offset, $this->documents); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Offset to retrieve |
|
134
|
|
|
* @param mixed $offset The offset to retrieve. |
|
135
|
|
|
* @return array|\Sokil\Mongo\Document |
|
136
|
|
|
*/ |
|
137
|
|
|
public function offsetGet($offset) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->documents[$offset]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Offset to set |
|
144
|
|
|
* @param mixed $offset The offset to assign the value to. |
|
145
|
|
|
* @param mixed $value The value to set. |
|
146
|
|
|
* @return void No value is returned. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function offsetSet($offset, $value) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->documents[$offset] = $value; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Offset to unset |
|
155
|
|
|
* @param mixed $offset The offset to unset. |
|
156
|
|
|
* @return void No value is returned. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function offsetUnset($offset) |
|
159
|
|
|
{ |
|
160
|
|
|
unset($this->documents[$offset]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function toArray() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->documents; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|