1 | <?php |
||
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 | * Set which class to use when hydrating results as document class instances. |
||
83 | * |
||
84 | * @param string $className |
||
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | 1 | public function hydrate($className) |
|
89 | { |
||
90 | 1 | $this->hydrationClass = $className; |
|
91 | |||
92 | 1 | return $this; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return QueryExpr |
||
97 | */ |
||
98 | 3 | public function matchExpr() |
|
99 | { |
||
100 | 3 | $expr = new QueryExpr($this->dm); |
|
101 | 3 | $expr->setClassMetadata($this->class); |
|
102 | |||
103 | 3 | return $expr; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return Expr |
||
108 | */ |
||
109 | 4 | public function expr() |
|
110 | { |
||
111 | 4 | return new Expr($this->dm, $this->class); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return Stage\Match |
||
116 | */ |
||
117 | 2 | public function match() |
|
121 | |||
122 | /** |
||
123 | * @param string $from |
||
124 | * @return Stage\Lookup |
||
125 | */ |
||
126 | public function lookup($from) |
||
130 | |||
131 | /** |
||
132 | * @param string $from |
||
133 | * @return Stage\Out |
||
134 | */ |
||
135 | 4 | public function out($from) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 2 | public function sort($fieldName, $order = null) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 2 | public function unwind($fieldName) |
|
157 | |||
158 | /** |
||
159 | * Returns the assembled aggregation pipeline |
||
160 | * |
||
161 | * For pipelines where the first stage is a $geoNear stage, it will apply |
||
162 | * the document filters and discriminator queries to the query portion of |
||
163 | * the geoNear operation. For all other pipelines, it prepends a $match stage |
||
164 | * containing the required query. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 8 | public function getPipeline() |
|
169 | { |
||
170 | 8 | $pipeline = parent::getPipeline(); |
|
171 | |||
172 | 8 | if ($this->getStage(0) instanceof GeoNear) { |
|
173 | 1 | $pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']); |
|
174 | 1 | } else { |
|
175 | 7 | $matchStage = $this->applyFilters([]); |
|
176 | 7 | if ($matchStage !== []) { |
|
177 | 1 | array_unshift($pipeline, ['$match' => $matchStage]); |
|
178 | 1 | } |
|
179 | } |
||
180 | |||
181 | 8 | return $pipeline; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Applies filters and discriminator queries to the pipeline |
||
186 | * |
||
187 | * @param array $query |
||
188 | * @return array |
||
189 | */ |
||
190 | 8 | private function applyFilters(array $query) |
|
199 | |||
200 | /** |
||
201 | * @param BaseCommandCursor $cursor |
||
202 | * |
||
203 | * @return CommandCursor |
||
204 | */ |
||
205 | 2 | private function prepareCursor(BaseCommandCursor $cursor) |
|
206 | { |
||
207 | 2 | $class = null; |
|
208 | 2 | if ($this->hydrationClass) { |
|
209 | 1 | $class = $this->dm->getClassMetadata($this->hydrationClass); |
|
210 | 1 | } |
|
211 | |||
212 | 2 | return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class); |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister |
||
217 | */ |
||
218 | 2 | private function getDocumentPersister() |
|
222 | } |
||
223 |
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.