1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan; |
15
|
|
|
|
16
|
|
|
use Countable; |
17
|
|
|
use Iterator; |
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface; |
20
|
|
|
use Maslosoft\Mangan\Transformers\RawArray; |
21
|
|
|
use MongoCursor; |
22
|
|
|
use UnexpectedValueException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cursor |
26
|
|
|
* |
27
|
|
|
* Cursor object, that behaves much like the MongoCursor, |
28
|
|
|
* but this one returns instantiated objects |
29
|
|
|
* @author Ianaré Sévi |
30
|
|
|
* @author Dariusz Górecki <[email protected]> |
31
|
|
|
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org |
32
|
|
|
* @copyright 2011 CleverIT http://www.cleverit.com.pl |
33
|
|
|
* @since v1.3.4 |
34
|
|
|
*/ |
35
|
|
|
class Cursor implements Iterator, Countable |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* MongoCursor returned by the query |
40
|
|
|
* @var FinderCursorInterface|MongoCursor |
41
|
|
|
* @since v1.3.4 |
42
|
|
|
*/ |
43
|
|
|
private $cursor; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Model used for instantiating objects |
47
|
|
|
* @var AnnotatedInterface |
48
|
|
|
* @since v1.3.4 |
49
|
|
|
*/ |
50
|
|
|
private $model; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct a new Cursor |
54
|
|
|
* |
55
|
|
|
* @param FinderCursorInterface|MongoCursor $cursor the cursor returned by the query |
56
|
|
|
* @param AnnotatedInterface $model the model for instantiating objects |
57
|
|
|
* @since v1.3.4 |
58
|
|
|
*/ |
59
|
1 |
|
public function __construct(Iterator $cursor, AnnotatedInterface $model) |
60
|
|
|
{ |
61
|
1 |
|
assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor)))); |
62
|
1 |
|
$this->cursor = $cursor; |
63
|
1 |
|
$this->model = $model; |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return MongoCursor for additional tuning |
68
|
|
|
* |
69
|
|
|
* @return FinderCursorInterface|MongoCursor the cursor used for this query |
70
|
|
|
* @since v1.3.4 |
71
|
|
|
*/ |
72
|
|
|
public function getCursor() |
73
|
|
|
{ |
74
|
|
|
return $this->cursor; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the current element |
79
|
|
|
* @return AnnotatedInterface|Document|null |
80
|
|
|
* @since v1.3.4 |
81
|
|
|
*/ |
82
|
1 |
|
public function current() |
83
|
|
|
{ |
84
|
1 |
|
$document = $this->cursor->current(); |
85
|
1 |
|
if (empty($document)) |
86
|
|
|
{ |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return RawArray::toModel($document, $this->model); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the key of the current element |
95
|
|
|
* @return scalar |
96
|
|
|
* @since v1.3.4 |
97
|
|
|
*/ |
98
|
|
|
public function key() |
99
|
|
|
{ |
100
|
|
|
return $this->cursor->key(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Move forward to next element |
105
|
|
|
* @return void |
106
|
|
|
* @since v1.3.4 |
107
|
|
|
*/ |
108
|
1 |
|
public function next() |
109
|
|
|
{ |
110
|
1 |
|
$this->cursor->next(); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Rewind the Iterator to the first element |
115
|
|
|
* @return void |
116
|
|
|
* @since v1.3.4 |
117
|
|
|
*/ |
118
|
1 |
|
public function rewind() |
119
|
|
|
{ |
120
|
1 |
|
$this->cursor->rewind(); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Checks if current position is valid |
125
|
|
|
* @return boolean |
126
|
|
|
* @since v1.3.4 |
127
|
|
|
*/ |
128
|
1 |
|
public function valid() |
129
|
|
|
{ |
130
|
1 |
|
return $this->cursor->valid(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the number of documents found |
135
|
|
|
* {@see http://www.php.net/manual/en/mongocursor.count.php} |
136
|
|
|
* @param boolean $foundOnly default FALSE |
137
|
|
|
* @return integer count of documents found |
138
|
|
|
* @since v1.3.4 |
139
|
|
|
*/ |
140
|
1 |
|
public function count($foundOnly = false) |
141
|
|
|
{ |
142
|
1 |
|
return $this->cursor->count($foundOnly); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Apply a limit to this cursor |
147
|
|
|
* {@see http://www.php.net/manual/en/mongocursor.limit.php} |
148
|
|
|
* @param integer $limit new limit |
149
|
|
|
* @since v1.3.4 |
150
|
|
|
*/ |
151
|
|
|
public function limit($limit) |
152
|
|
|
{ |
153
|
|
|
$this->cursor->limit($limit); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Skip a $offset records |
158
|
|
|
* {@see http://www.php.net/manual/en/mongocursor.skip.php} |
159
|
|
|
* @param integer $offset new skip |
160
|
|
|
* @since v1.3.4 |
161
|
|
|
*/ |
162
|
|
|
public function offset($offset) |
163
|
|
|
{ |
164
|
|
|
$this->cursor->skip($offset); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Apply sorting directives |
169
|
|
|
* {@see http://www.php.net/manual/en/mongocursor.sort.php} |
170
|
|
|
* @param array $fields sorting directives |
171
|
|
|
* @since v1.3.4 |
172
|
|
|
*/ |
173
|
|
|
public function sort(array $fields) |
174
|
|
|
{ |
175
|
|
|
$this->cursor->sort($fields); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: