1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Aggregation; |
21
|
|
|
|
22
|
|
|
use Doctrine\MongoDB\Aggregation\Builder as BaseBuilder; |
23
|
|
|
use Doctrine\MongoDB\Aggregation\Stage\GeoNear; |
24
|
|
|
use Doctrine\MongoDB\CommandCursor as BaseCommandCursor; |
25
|
|
|
use Doctrine\ODM\MongoDB\CommandCursor; |
26
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
27
|
|
|
use Doctrine\ODM\MongoDB\Query\Expr as QueryExpr; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fluent interface for building aggregation pipelines. |
31
|
|
|
*/ |
32
|
|
|
class Builder extends BaseBuilder |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The DocumentManager instance for this query |
36
|
|
|
* |
37
|
|
|
* @var DocumentManager |
38
|
|
|
*/ |
39
|
|
|
private $dm; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The ClassMetadata instance. |
43
|
|
|
* |
44
|
|
|
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
45
|
|
|
*/ |
46
|
|
|
private $class; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $hydrationClass; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new aggregation builder. |
55
|
|
|
* |
56
|
|
|
* @param DocumentManager $dm |
57
|
|
|
* @param string $documentName |
58
|
|
|
*/ |
59
|
31 |
|
public function __construct(DocumentManager $dm, $documentName) |
60
|
|
|
{ |
61
|
31 |
|
$this->dm = $dm; |
62
|
31 |
|
$this->class = $this->dm->getClassMetadata($documentName); |
63
|
|
|
|
64
|
31 |
|
parent::__construct($this->dm->getDocumentCollection($documentName)); |
65
|
31 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
12 |
|
public function execute($options = []) |
71
|
|
|
{ |
72
|
|
|
// Force cursor to be used |
73
|
12 |
|
$options = array_merge($options, ['cursor' => true]); |
74
|
|
|
|
75
|
12 |
|
$cursor = parent::execute($options); |
76
|
|
|
|
77
|
12 |
|
return $this->prepareCursor($cursor); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set which class to use when hydrating results as document class instances. |
82
|
|
|
* |
83
|
|
|
* @param string $className |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
1 |
|
public function hydrate($className) |
88
|
|
|
{ |
89
|
1 |
|
$this->hydrationClass = $className; |
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return QueryExpr |
96
|
|
|
*/ |
97
|
7 |
|
public function matchExpr() |
98
|
|
|
{ |
99
|
7 |
|
$expr = new QueryExpr($this->dm); |
100
|
7 |
|
$expr->setClassMetadata($this->class); |
101
|
|
|
|
102
|
7 |
|
return $expr; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Expr |
107
|
|
|
*/ |
108
|
10 |
|
public function expr() |
109
|
|
|
{ |
110
|
10 |
|
return new Expr($this->dm, $this->class); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Stage\Bucket |
115
|
|
|
*/ |
116
|
1 |
|
public function bucket() |
117
|
|
|
{ |
118
|
1 |
|
return $this->addStage(new Stage\Bucket($this, $this->dm, $this->class)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Stage\BucketAuto |
123
|
|
|
*/ |
124
|
1 |
|
public function bucketAuto() |
125
|
|
|
{ |
126
|
1 |
|
return $this->addStage(new Stage\BucketAuto($this, $this->dm, $this->class)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $from |
131
|
|
|
* |
132
|
|
|
* @return Stage\GraphLookup |
133
|
|
|
*/ |
134
|
|
|
public function graphLookup($from) |
135
|
|
|
{ |
136
|
|
|
return $this->addStage(new Stage\GraphLookup($this, $from, $this->dm, $this->class)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Stage\Match |
141
|
|
|
*/ |
142
|
6 |
|
public function match() |
143
|
|
|
{ |
144
|
6 |
|
return $this->addStage(new Stage\Match($this)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $from |
149
|
|
|
* @return Stage\Lookup |
150
|
|
|
*/ |
151
|
13 |
|
public function lookup($from) |
152
|
|
|
{ |
153
|
13 |
|
return $this->addStage(new Stage\Lookup($this, $from, $this->dm, $this->class)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $from |
158
|
|
|
* @return Stage\Out |
159
|
|
|
*/ |
160
|
4 |
|
public function out($from) |
161
|
|
|
{ |
162
|
4 |
|
return $this->addStage(new Stage\Out($this, $from, $this->dm)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string|null $expression Optional. A replacement expression that |
167
|
|
|
* resolves to a document. |
168
|
|
|
* @return Stage\ReplaceRoot |
169
|
|
|
*/ |
170
|
6 |
|
public function replaceRoot($expression = null) |
171
|
|
|
{ |
172
|
6 |
|
return $this->addStage(new Stage\ReplaceRoot($this, $this->dm, $this->class, $expression)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return Stage\SortByCount |
177
|
|
|
*/ |
178
|
1 |
|
public function sortByCount($expression) |
179
|
|
|
{ |
180
|
1 |
|
return $this->addStage(new Stage\SortByCount($this, $expression, $this->dm, $this->class)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
2 |
|
public function sort($fieldName, $order = null) |
187
|
|
|
{ |
188
|
2 |
|
$fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; |
189
|
2 |
|
return parent::sort($this->getDocumentPersister()->prepareSortOrProjection($fields)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
4 |
|
public function unwind($fieldName) |
196
|
|
|
{ |
197
|
4 |
|
$fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName); |
198
|
4 |
|
return parent::unwind($fieldName); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns the assembled aggregation pipeline |
203
|
|
|
* |
204
|
|
|
* For pipelines where the first stage is a $geoNear stage, it will apply |
205
|
|
|
* the document filters and discriminator queries to the query portion of |
206
|
|
|
* the geoNear operation. For all other pipelines, it prepends a $match stage |
207
|
|
|
* containing the required query. |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
22 |
|
public function getPipeline() |
212
|
|
|
{ |
213
|
22 |
|
$pipeline = parent::getPipeline(); |
214
|
|
|
|
215
|
22 |
|
if ($this->getStage(0) instanceof GeoNear) { |
216
|
1 |
|
$pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']); |
217
|
|
|
} else { |
218
|
21 |
|
$matchStage = $this->applyFilters([]); |
219
|
21 |
|
if ($matchStage !== []) { |
220
|
1 |
|
array_unshift($pipeline, ['$match' => $matchStage]); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
22 |
|
return $pipeline; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Applies filters and discriminator queries to the pipeline |
229
|
|
|
* |
230
|
|
|
* @param array $query |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
22 |
|
private function applyFilters(array $query) |
234
|
|
|
{ |
235
|
22 |
|
$documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
236
|
|
|
|
237
|
22 |
|
$query = $documentPersister->addDiscriminatorToPreparedQuery($query); |
238
|
22 |
|
$query = $documentPersister->addFilterToPreparedQuery($query); |
239
|
|
|
|
240
|
22 |
|
return $query; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param BaseCommandCursor $cursor |
245
|
|
|
* |
246
|
|
|
* @return CommandCursor |
247
|
|
|
*/ |
248
|
12 |
|
private function prepareCursor(BaseCommandCursor $cursor) |
249
|
|
|
{ |
250
|
12 |
|
$class = null; |
251
|
12 |
|
if ($this->hydrationClass) { |
252
|
1 |
|
$class = $this->dm->getClassMetadata($this->hydrationClass); |
253
|
|
|
} |
254
|
|
|
|
255
|
12 |
|
return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister |
260
|
|
|
*/ |
261
|
4 |
|
private function getDocumentPersister() |
262
|
|
|
{ |
263
|
4 |
|
return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.