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\Aggregation\Stage\Match; |
26
|
|
|
use Doctrine\ODM\MongoDB\CommandCursor; |
27
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
28
|
|
|
use Doctrine\ODM\MongoDB\Query\Expr as QueryExpr; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fluent interface for building aggregation pipelines. |
32
|
|
|
*/ |
33
|
|
|
class Builder extends BaseBuilder |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The DocumentManager instance for this query |
37
|
|
|
* |
38
|
|
|
* @var DocumentManager |
39
|
|
|
*/ |
40
|
|
|
private $dm; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The ClassMetadata instance. |
44
|
|
|
* |
45
|
|
|
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
46
|
|
|
*/ |
47
|
|
|
private $class; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $hydrationClass; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a new aggregation builder. |
56
|
|
|
* |
57
|
|
|
* @param DocumentManager $dm |
58
|
|
|
* @param string $documentName |
59
|
|
|
*/ |
60
|
11 |
|
public function __construct(DocumentManager $dm, $documentName) |
61
|
|
|
{ |
62
|
11 |
|
$this->dm = $dm; |
63
|
11 |
|
$this->class = $this->dm->getClassMetadata($documentName); |
64
|
|
|
|
65
|
11 |
|
parent::__construct($this->dm->getDocumentCollection($documentName)); |
66
|
11 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function execute($options = []) |
72
|
|
|
{ |
73
|
|
|
// Force cursor to be used |
74
|
2 |
|
$options = array_merge($options, ['cursor' => true]); |
75
|
|
|
|
76
|
2 |
|
$cursor = parent::execute($options); |
77
|
|
|
|
78
|
2 |
|
return $this->prepareCursor($cursor); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $className |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
1 |
|
public function hydrate($className) |
87
|
|
|
{ |
88
|
1 |
|
$this->hydrationClass = $className; |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return QueryExpr |
95
|
|
|
*/ |
96
|
3 |
|
public function matchExpr() |
97
|
|
|
{ |
98
|
3 |
|
$expr = new QueryExpr($this->dm); |
99
|
3 |
|
$expr->setClassMetadata($this->class); |
100
|
|
|
|
101
|
3 |
|
return $expr; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Expr |
106
|
|
|
*/ |
107
|
4 |
|
public function expr() |
108
|
|
|
{ |
109
|
4 |
|
return new Expr($this->dm, $this->class); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Stage\Match |
114
|
|
|
*/ |
115
|
2 |
|
public function match() |
116
|
|
|
{ |
117
|
2 |
|
return $this->addStage(new Stage\Match($this)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $from |
122
|
|
|
* @return Stage\Lookup |
123
|
|
|
*/ |
124
|
|
|
public function lookup($from) |
125
|
|
|
{ |
126
|
|
|
return $this->addStage(new Stage\Lookup($this, $from, $this->dm, $this->class)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $from |
131
|
|
|
* @return Stage\Out |
132
|
|
|
*/ |
133
|
4 |
|
public function out($from) |
134
|
|
|
{ |
135
|
4 |
|
return $this->addStage(new Stage\Out($this, $from, $this->dm)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
2 |
|
public function sort($fieldName, $order = null) |
142
|
|
|
{ |
143
|
2 |
|
$fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; |
144
|
2 |
|
return parent::sort($this->getDocumentPersister()->prepareSortOrProjection($fields)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
2 |
|
public function unwind($fieldName) |
151
|
|
|
{ |
152
|
2 |
|
$fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName); |
153
|
2 |
|
return parent::unwind($fieldName); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the assembled aggregation pipeline |
158
|
|
|
* |
159
|
|
|
* For pipelines where the first stage is a $geoNear stage, it will apply |
160
|
|
|
* the document filters and discriminator queries to the query portion of |
161
|
|
|
* the geoNear operation. For all other pipelines, it prepends a $match stage |
162
|
|
|
* containing the required query. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
8 |
|
public function getPipeline() |
167
|
|
|
{ |
168
|
8 |
|
$pipeline = parent::getPipeline(); |
169
|
|
|
|
170
|
8 |
|
if ($this->getStage(0) instanceof GeoNear) { |
171
|
1 |
|
$pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']); |
172
|
|
|
} else { |
173
|
7 |
|
$matchStage = $this->applyFilters([]); |
174
|
7 |
|
if ($matchStage !== []) { |
175
|
1 |
|
array_unshift($pipeline, ['$match' => $matchStage]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
8 |
|
return $pipeline; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Applies filters and discriminator queries to the pipeline |
184
|
|
|
* |
185
|
|
|
* @param array $query |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
8 |
|
private function applyFilters(array $query) |
189
|
|
|
{ |
190
|
8 |
|
$documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
191
|
|
|
|
192
|
8 |
|
$query = $documentPersister->addDiscriminatorToPreparedQuery($query); |
193
|
8 |
|
$query = $documentPersister->addFilterToPreparedQuery($query); |
194
|
|
|
|
195
|
8 |
|
return $query; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param BaseCommandCursor $cursor |
200
|
|
|
* |
201
|
|
|
* @return CommandCursor |
202
|
|
|
*/ |
203
|
2 |
|
private function prepareCursor(BaseCommandCursor $cursor) |
204
|
|
|
{ |
205
|
2 |
|
$class = null; |
206
|
2 |
|
if ($this->hydrationClass) { |
207
|
1 |
|
$class = $this->dm->getClassMetadata($this->hydrationClass); |
208
|
|
|
} |
209
|
|
|
|
210
|
2 |
|
return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister |
215
|
|
|
*/ |
216
|
2 |
|
private function getDocumentPersister() |
217
|
|
|
{ |
218
|
2 |
|
return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
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.