Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Builder |
||
32 | { |
||
33 | /** |
||
34 | * The DocumentManager instance for this query |
||
35 | * |
||
36 | * @var DocumentManager |
||
37 | */ |
||
38 | private $dm; |
||
39 | |||
40 | /** |
||
41 | * The ClassMetadata instance. |
||
42 | * |
||
43 | * @var ClassMetadata |
||
44 | */ |
||
45 | private $class; |
||
46 | |||
47 | /** |
||
48 | * The current field we are operating on. |
||
49 | * |
||
50 | * @todo Change this to private once ODM requires doctrine/mongodb 1.1+ |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $currentField; |
||
54 | |||
55 | /** |
||
56 | * Whether or not to hydrate the data to documents. |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | private $hydrate = true; |
||
61 | |||
62 | /** |
||
63 | * Whether or not to refresh the data for documents that are already in the identity map. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $refresh = false; |
||
68 | |||
69 | /** |
||
70 | * Array of primer Closure instances. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $primers = []; |
||
75 | |||
76 | /** |
||
77 | * Whether or not to register documents in UnitOfWork. |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $readOnly = false; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | private $rewindable = true; |
||
87 | |||
88 | /** |
||
89 | * The Collection instance. |
||
90 | * |
||
91 | * @var Collection |
||
92 | */ |
||
93 | private $collection; |
||
94 | |||
95 | /** |
||
96 | * Array containing the query data. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | private $query = ['type' => Query::TYPE_FIND]; |
||
101 | |||
102 | /** |
||
103 | * The Expr instance used for building this query. |
||
104 | * |
||
105 | * This object includes the query criteria and the "new object" used for |
||
106 | * insert and update queries. |
||
107 | * |
||
108 | * @var Expr $expr |
||
109 | */ |
||
110 | private $expr; |
||
111 | |||
112 | 282 | /** |
|
113 | * Construct a Builder |
||
114 | 282 | * |
|
115 | 282 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
|
116 | 282 | */ |
|
117 | 9 | public function __construct(DocumentManager $dm, $documentName = null) |
|
118 | { |
||
119 | $this->dm = $dm; |
||
120 | 274 | $this->expr = new Expr($dm); |
|
121 | 273 | if ($documentName === null) { |
|
122 | return; |
||
123 | 1 | } |
|
124 | |||
125 | 1 | $this->setDocumentName($documentName); |
|
126 | 1 | } |
|
127 | |||
128 | public function __clone() |
||
129 | { |
||
130 | $this->expr = clone $this->expr; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Add one or more $and clauses to the current query. |
||
135 | * |
||
136 | * You can create a new expression using the {@link Builder::expr()} method. |
||
137 | * |
||
138 | * @see Expr::addAnd() |
||
139 | 4 | * @see http://docs.mongodb.org/manual/reference/operator/and/ |
|
140 | * |
||
141 | 4 | * @param array|Expr $expression |
|
142 | * @param array|Expr ...$expressions |
||
143 | 4 | */ |
|
144 | public function addAnd($expression, ...$expressions) : self |
||
145 | { |
||
146 | $this->expr->addAnd(...func_get_args()); |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Add one or more $nor clauses to the current query. |
||
153 | * |
||
154 | * You can create a new expression using the {@link Builder::expr()} method. |
||
155 | * |
||
156 | * @see Expr::addNor() |
||
157 | 3 | * @see http://docs.mongodb.org/manual/reference/operator/nor/ |
|
158 | * |
||
159 | 3 | * @param array|Expr $expression |
|
160 | * @param array|Expr ...$expressions |
||
161 | 3 | */ |
|
162 | public function addNor($expression, ...$expressions) : self |
||
163 | { |
||
164 | $this->expr->addNor(...func_get_args()); |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Add one or more $or clauses to the current query. |
||
171 | * |
||
172 | * You can create a new expression using the {@link Builder::expr()} method. |
||
173 | * |
||
174 | * @see Expr::addOr() |
||
175 | 6 | * @see http://docs.mongodb.org/manual/reference/operator/or/ |
|
176 | * |
||
177 | 6 | * @param array|Expr $expression |
|
178 | * @param array|Expr ...$expressions |
||
179 | 6 | */ |
|
180 | public function addOr($expression, ...$expressions) : self |
||
181 | { |
||
182 | $this->expr->addOr(...func_get_args()); |
||
183 | |||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Append one or more values to the current array field only if they do not |
||
189 | * already exist in the array. |
||
190 | * |
||
191 | * If the field does not exist, it will be set to an array containing the |
||
192 | * unique value(s) in the argument. If the field is not an array, the query |
||
193 | * will yield an error. |
||
194 | * |
||
195 | * Multiple values may be specified by provided an Expr object and using |
||
196 | * {@link Expr::each()}. |
||
197 | * |
||
198 | * @see Expr::addToSet() |
||
199 | 5 | * @see http://docs.mongodb.org/manual/reference/operator/addToSet/ |
|
200 | * @see http://docs.mongodb.org/manual/reference/operator/each/ |
||
201 | 5 | * |
|
202 | * @param mixed|Expr $valueOrExpression |
||
203 | 5 | */ |
|
204 | public function addToSet($valueOrExpression) : self |
||
205 | { |
||
206 | $this->expr->addToSet($valueOrExpression); |
||
207 | |||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | 3 | * Specify $all criteria for the current field. |
|
213 | * |
||
214 | 3 | * @see Expr::all() |
|
215 | * @see http://docs.mongodb.org/manual/reference/operator/all/ |
||
216 | 3 | */ |
|
217 | public function all(array $values) : self |
||
218 | { |
||
219 | $this->expr->all($values); |
||
220 | |||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Apply a bitwise and operation on the current field. |
||
226 | * |
||
227 | 1 | * @see Expr::bitAnd() |
|
228 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
||
229 | 1 | * |
|
230 | * @return $this |
||
231 | 1 | */ |
|
232 | public function bitAnd(int $value) : self |
||
233 | { |
||
234 | $this->expr->bitAnd($value); |
||
235 | |||
236 | return $this; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | 1 | * Apply a bitwise or operation on the current field. |
|
241 | * |
||
242 | 1 | * @see Expr::bitOr() |
|
243 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
||
244 | 1 | */ |
|
245 | public function bitOr(int $value) : self |
||
246 | { |
||
247 | $this->expr->bitOr($value); |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Matches documents where all of the bit positions given by the query are |
||
254 | * clear. |
||
255 | * |
||
256 | 1 | * @see Expr::bitsAllClear() |
|
257 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllClear/ |
||
258 | 1 | * |
|
259 | * @param int|array|Binary $value |
||
260 | 1 | */ |
|
261 | public function bitsAllClear($value) : self |
||
262 | { |
||
263 | $this->expr->bitsAllClear($value); |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Matches documents where all of the bit positions given by the query are |
||
270 | * set. |
||
271 | * |
||
272 | 1 | * @see Expr::bitsAllSet() |
|
273 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllSet/ |
||
274 | 1 | * |
|
275 | * @param int|array|Binary $value |
||
276 | 1 | */ |
|
277 | public function bitsAllSet($value) : self |
||
278 | { |
||
279 | $this->expr->bitsAllSet($value); |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Matches documents where any of the bit positions given by the query are |
||
286 | * clear. |
||
287 | * |
||
288 | 1 | * @see Expr::bitsAnyClear() |
|
289 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnyClear/ |
||
290 | 1 | * |
|
291 | * @param int|array|Binary $value |
||
292 | 1 | */ |
|
293 | public function bitsAnyClear($value) : self |
||
294 | { |
||
295 | $this->expr->bitsAnyClear($value); |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Matches documents where any of the bit positions given by the query are |
||
302 | * set. |
||
303 | * |
||
304 | 1 | * @see Expr::bitsAnySet() |
|
305 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnySet/ |
||
306 | 1 | * |
|
307 | * @param int|array|Binary $value |
||
308 | 1 | */ |
|
309 | public function bitsAnySet($value) : self |
||
310 | { |
||
311 | $this->expr->bitsAnySet($value); |
||
312 | |||
313 | return $this; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | 1 | * Apply a bitwise xor operation on the current field. |
|
318 | * |
||
319 | 1 | * @see Expr::bitXor() |
|
320 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
||
321 | 1 | */ |
|
322 | public function bitXor(int $value) : self |
||
323 | { |
||
324 | $this->expr->bitXor($value); |
||
325 | |||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * A boolean flag to enable or disable case sensitive search for $text |
||
331 | * criteria. |
||
332 | * |
||
333 | * This method must be called after text(). |
||
334 | * |
||
335 | 1 | * @see Expr::caseSensitive() |
|
336 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
||
337 | 1 | * |
|
338 | * @throws BadMethodCallException If the query does not already have $text criteria. |
||
339 | 1 | */ |
|
340 | public function caseSensitive(bool $caseSensitive) : self |
||
341 | { |
||
342 | $this->expr->caseSensitive($caseSensitive); |
||
343 | |||
344 | return $this; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | 1 | * Associates a comment to any expression taking a query predicate. |
|
349 | * |
||
350 | 1 | * @see Expr::comment() |
|
351 | * @see http://docs.mongodb.org/manual/reference/operator/query/comment/ |
||
352 | 1 | */ |
|
353 | public function comment(string $comment) : self |
||
354 | { |
||
355 | $this->expr->comment($comment); |
||
356 | |||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Change the query type to count. |
||
362 | */ |
||
363 | public function count() : self |
||
364 | { |
||
365 | $this->query['type'] = Query::TYPE_COUNT; |
||
366 | |||
367 | return $this; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | 3 | * Sets the value of the current field to the current date, either as a date or a timestamp. |
|
372 | * |
||
373 | 3 | * @see Expr::currentDate() |
|
374 | * @see http://docs.mongodb.org/manual/reference/operator/currentDate/ |
||
375 | 2 | */ |
|
376 | public function currentDate(string $type = 'date') : self |
||
377 | { |
||
378 | $this->expr->currentDate($type); |
||
379 | |||
380 | return $this; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Return an array of information about the Builder state for debugging. |
||
385 | * |
||
386 | * The $name parameter may be used to return a specific key from the |
||
387 | 26 | * internal $query array property. If omitted, the entire array will be |
|
388 | * returned. |
||
389 | 26 | * |
|
390 | * @return mixed |
||
391 | */ |
||
392 | public function debug(?string $name = null) |
||
393 | { |
||
394 | return $name !== null ? $this->query[$name] : $this->query; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * A boolean flag to enable or disable diacritic sensitive search for $text |
||
399 | * criteria. |
||
400 | * |
||
401 | * This method must be called after text(). |
||
402 | * |
||
403 | 1 | * @see Builder::diacriticSensitive() |
|
404 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
||
405 | 1 | * |
|
406 | * @throws BadMethodCallException If the query does not already have $text criteria. |
||
407 | 1 | */ |
|
408 | public function diacriticSensitive(bool $diacriticSensitive) : self |
||
409 | { |
||
410 | $this->expr->diacriticSensitive($diacriticSensitive); |
||
411 | |||
412 | return $this; |
||
413 | } |
||
414 | |||
415 | 2 | /** |
|
416 | * Change the query type to a distinct command. |
||
417 | 2 | * |
|
418 | 2 | * @see http://docs.mongodb.org/manual/reference/command/distinct/ |
|
419 | */ |
||
420 | 2 | public function distinct(string $field) : self |
|
421 | { |
||
422 | $this->query['type'] = Query::TYPE_DISTINCT; |
||
423 | $this->query['distinct'] = $field; |
||
424 | |||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Specify $elemMatch criteria for the current field. |
||
430 | * |
||
431 | * You can create a new expression using the {@link Builder::expr()} method. |
||
432 | * |
||
433 | 6 | * @see Expr::elemMatch() |
|
434 | * @see http://docs.mongodb.org/manual/reference/operator/elemMatch/ |
||
435 | 6 | * |
|
436 | * @param array|Expr $expression |
||
437 | 6 | */ |
|
438 | public function elemMatch($expression) : self |
||
439 | { |
||
440 | $this->expr->elemMatch($expression); |
||
441 | |||
442 | return $this; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Specify an equality match for the current field. |
||
447 | 79 | * |
|
448 | * @see Expr::equals() |
||
449 | 79 | * |
|
450 | * @param mixed $value |
||
451 | 79 | */ |
|
452 | public function equals($value) : self |
||
453 | { |
||
454 | $this->expr->equals($value); |
||
455 | |||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Set one or more fields to be excluded from the query projection. |
||
461 | * |
||
462 | 6 | * If fields have been selected for inclusion, only the "_id" field may be |
|
463 | * excluded. |
||
464 | 6 | * |
|
465 | 6 | * @param array|string $fieldName,... |
|
466 | */ |
||
467 | public function exclude($fieldName = null) : self |
||
468 | 6 | { |
|
469 | if (! isset($this->query['select'])) { |
||
470 | 6 | $this->query['select'] = []; |
|
471 | 4 | } |
|
472 | |||
473 | $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); |
||
474 | 6 | ||
475 | foreach ($fieldNames as $fieldName) { |
||
476 | $this->query['select'][$fieldName] = 0; |
||
477 | } |
||
478 | |||
479 | return $this; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | 5 | * Specify $exists criteria for the current field. |
|
484 | * |
||
485 | 5 | * @see Expr::exists() |
|
486 | * @see http://docs.mongodb.org/manual/reference/operator/exists/ |
||
487 | 5 | */ |
|
488 | public function exists(bool $bool) : self |
||
489 | { |
||
490 | $this->expr->exists($bool); |
||
491 | |||
492 | return $this; |
||
493 | 26 | } |
|
494 | |||
495 | 26 | /** |
|
496 | 26 | * Create a new Expr instance that can be used as an expression with the Builder |
|
497 | */ |
||
498 | 26 | public function expr() : Expr |
|
499 | { |
||
500 | $expr = new Expr($this->dm); |
||
501 | $expr->setClassMetadata($this->class); |
||
502 | |||
503 | return $expr; |
||
504 | 145 | } |
|
505 | |||
506 | 145 | /** |
|
507 | 145 | * Set the current field to operate on. |
|
508 | */ |
||
509 | 145 | public function field(string $field) : self |
|
510 | { |
||
511 | $this->currentField = $field; |
||
512 | $this->expr->field($field); |
||
513 | |||
514 | return $this; |
||
515 | 13 | } |
|
516 | |||
517 | 13 | /** |
|
518 | 13 | * Change the query type to find and optionally set and change the class being queried. |
|
519 | */ |
||
520 | 13 | public function find(?string $documentName = null) : self |
|
527 | |||
528 | 1 | public function findAndRemove(?string $documentName = null) : self |
|
535 | |||
536 | 13 | public function findAndUpdate(?string $documentName = null) : self |
|
537 | { |
||
538 | $this->setDocumentName($documentName); |
||
539 | $this->query['type'] = Query::TYPE_FIND_AND_UPDATE; |
||
540 | |||
541 | return $this; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Add $geoIntersects criteria with a GeoJSON geometry to the query. |
||
546 | * |
||
547 | * The geometry parameter GeoJSON object or an array corresponding to the |
||
548 | * geometry's JSON representation. |
||
549 | * |
||
550 | 1 | * @see Expr::geoIntersects() |
|
551 | * @see http://docs.mongodb.org/manual/reference/operator/geoIntersects/ |
||
552 | 1 | * |
|
553 | * @param array|Geometry $geometry |
||
554 | 1 | */ |
|
555 | public function geoIntersects($geometry) : self |
||
556 | { |
||
557 | $this->expr->geoIntersects($geometry); |
||
558 | |||
559 | return $this; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Add $geoWithin criteria with a GeoJSON geometry to the query. |
||
564 | * |
||
565 | * The geometry parameter GeoJSON object or an array corresponding to the |
||
566 | * geometry's JSON representation. |
||
567 | * |
||
568 | 1 | * @see Expr::geoWithin() |
|
569 | * @see http://docs.mongodb.org/manual/reference/operator/geoWithin/ |
||
570 | 1 | * |
|
571 | * @param array|Geometry $geometry |
||
572 | 1 | */ |
|
573 | public function geoWithin($geometry) : self |
||
574 | { |
||
575 | $this->expr->geoWithin($geometry); |
||
576 | |||
577 | return $this; |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Add $geoWithin criteria with a $box shape to the query. |
||
582 | * |
||
583 | * A rectangular polygon will be constructed from a pair of coordinates |
||
584 | * corresponding to the bottom left and top right corners. |
||
585 | * |
||
586 | * Note: the $box operator only supports legacy coordinate pairs and 2d |
||
587 | 1 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
|
588 | * |
||
589 | 1 | * @see Expr::geoWithinBox() |
|
590 | * @see http://docs.mongodb.org/manual/reference/operator/box/ |
||
591 | 1 | */ |
|
592 | public function geoWithinBox(float $x1, float $y1, float $x2, float $y2) : self |
||
593 | { |
||
594 | $this->expr->geoWithinBox($x1, $y1, $x2, $y2); |
||
595 | |||
596 | return $this; |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * Add $geoWithin criteria with a $center shape to the query. |
||
601 | * |
||
602 | * Note: the $center operator only supports legacy coordinate pairs and 2d |
||
603 | 1 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
|
604 | * |
||
605 | 1 | * @see Expr::geoWithinCenter() |
|
606 | * @see http://docs.mongodb.org/manual/reference/operator/center/ |
||
607 | 1 | */ |
|
608 | public function geoWithinCenter(float $x, float $y, float $radius) : self |
||
609 | { |
||
610 | $this->expr->geoWithinCenter($x, $y, $radius); |
||
611 | |||
612 | return $this; |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Add $geoWithin criteria with a $centerSphere shape to the query. |
||
617 | * |
||
618 | 1 | * Note: the $centerSphere operator supports both 2d and 2dsphere indexes. |
|
619 | * |
||
620 | 1 | * @see Expr::geoWithinCenterSphere() |
|
621 | * @see http://docs.mongodb.org/manual/reference/operator/centerSphere/ |
||
622 | 1 | */ |
|
623 | public function geoWithinCenterSphere(float $x, float $y, float $radius) : self |
||
624 | { |
||
625 | $this->expr->geoWithinCenterSphere($x, $y, $radius); |
||
626 | |||
627 | return $this; |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Add $geoWithin criteria with a $polygon shape to the query. |
||
632 | * |
||
633 | * Point coordinates are in x, y order (easting, northing for projected |
||
634 | * coordinates, longitude, latitude for geographic coordinates). |
||
635 | * |
||
636 | * The last point coordinate is implicitly connected with the first. |
||
637 | * |
||
638 | * Note: the $polygon operator only supports legacy coordinate pairs and 2d |
||
639 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
||
640 | * |
||
641 | * @see Expr::geoWithinPolygon() |
||
642 | * @see http://docs.mongodb.org/manual/reference/operator/polygon/ |
||
643 | * |
||
644 | 1 | * @param array $point1 First point of the polygon |
|
645 | * @param array $point2 Second point of the polygon |
||
646 | 1 | * @param array $point3 Third point of the polygon |
|
647 | * @param array ...$points Additional points of the polygon |
||
648 | 1 | */ |
|
649 | public function geoWithinPolygon($point1, $point2, $point3, ...$points) : self |
||
650 | { |
||
651 | $this->expr->geoWithinPolygon(...func_get_args()); |
||
652 | |||
653 | return $this; |
||
654 | } |
||
655 | |||
656 | 13 | /** |
|
657 | * Return the expression's "new object". |
||
658 | 13 | * |
|
659 | * @see Expr::getNewObj() |
||
660 | */ |
||
661 | public function getNewObj() : array |
||
662 | { |
||
663 | return $this->expr->getNewObj(); |
||
664 | 155 | } |
|
665 | |||
666 | 155 | /** |
|
667 | * Gets the Query executable. |
||
668 | 155 | */ |
|
669 | public function getQuery(array $options = []) : Query |
||
670 | 155 | { |
|
671 | 155 | $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); |
|
672 | 155 | ||
673 | $query = $this->query; |
||
674 | 155 | ||
675 | $query['query'] = $this->expr->getQuery(); |
||
676 | 155 | $query['query'] = $documentPersister->addDiscriminatorToPreparedQuery($query['query']); |
|
677 | 2 | $query['query'] = $documentPersister->addFilterToPreparedQuery($query['query']); |
|
678 | |||
679 | $query['newObj'] = $this->expr->getNewObj(); |
||
680 | 155 | ||
681 | 155 | if (isset($query['distinct'])) { |
|
682 | 1 | $query['distinct'] = $documentPersister->prepareFieldName($query['distinct']); |
|
683 | 1 | } |
|
684 | |||
685 | if ($this->class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION && ! empty($query['upsert']) && |
||
686 | 154 | (empty($query['query'][$this->class->discriminatorField]) || is_array($query['query'][$this->class->discriminatorField]))) { |
|
687 | 15 | throw new InvalidArgumentException('Upsert query that is to be performed on discriminated document does not have single ' . |
|
688 | 15 | 'discriminator. Either not use base class or set \'' . $this->class->discriminatorField . '\' field manually.'); |
|
689 | 15 | } |
|
690 | |||
691 | 2 | if (! empty($query['select'])) { |
|
692 | 2 | $query['select'] = $documentPersister->prepareProjection($query['select']); |
|
693 | 2 | if ($this->hydrate && $this->class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION |
|
694 | 1 | && ! isset($query['select'][$this->class->discriminatorField])) { |
|
695 | $includeMode = 0 < count(array_filter($query['select'], static function ($mode) { |
||
696 | return $mode === 1; |
||
697 | })); |
||
698 | if ($includeMode && ! isset($query['select'][$this->class->discriminatorField])) { |
||
699 | 154 | $query['select'][$this->class->discriminatorField] = 1; |
|
700 | 20 | } |
|
701 | } |
||
702 | } |
||
703 | 154 | ||
704 | 1 | if (isset($query['sort'])) { |
|
705 | $query['sort'] = $documentPersister->prepareSort($query['sort']); |
||
706 | } |
||
707 | 154 | ||
708 | 154 | if ($this->class->readPreference && ! array_key_exists('readPreference', $query)) { |
|
709 | 154 | $query['readPreference'] = new ReadPreference($this->class->readPreference, $this->class->readPreferenceTags); |
|
710 | 154 | } |
|
711 | 154 | ||
712 | 154 | return new Query( |
|
713 | 154 | $this->dm, |
|
714 | 154 | $this->class, |
|
715 | 154 | $this->collection, |
|
716 | 154 | $query, |
|
717 | $options, |
||
718 | $this->hydrate, |
||
719 | $this->refresh, |
||
720 | $this->primers, |
||
721 | $this->readOnly, |
||
722 | $this->rewindable |
||
723 | ); |
||
724 | } |
||
725 | 32 | ||
726 | /** |
||
727 | 32 | * Return the expression's query criteria. |
|
728 | * |
||
729 | * @see Expr::getQuery() |
||
730 | */ |
||
731 | public function getQueryArray() : array |
||
732 | { |
||
733 | return $this->expr->getQuery(); |
||
734 | } |
||
735 | |||
736 | /** |
||
737 | * Get the type of this query. |
||
738 | */ |
||
739 | public function getType() : int |
||
740 | { |
||
741 | return $this->query['type']; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Specify $gt criteria for the current field. |
||
746 | 2 | * |
|
747 | * @see Expr::gt() |
||
748 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/gt/ |
|
749 | * |
||
750 | 2 | * @param mixed $value |
|
751 | */ |
||
752 | public function gt($value) : self |
||
753 | { |
||
754 | $this->expr->gt($value); |
||
755 | |||
756 | return $this; |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Specify $gte criteria for the current field. |
||
761 | 2 | * |
|
762 | * @see Expr::gte() |
||
763 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/gte/ |
|
764 | * |
||
765 | 2 | * @param mixed $value |
|
766 | */ |
||
767 | public function gte($value) : self |
||
768 | { |
||
769 | $this->expr->gte($value); |
||
770 | |||
771 | return $this; |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * Set the index hint for the query. |
||
776 | * |
||
777 | * @param array|string $index |
||
778 | */ |
||
779 | public function hint($index) : self |
||
780 | 17 | { |
|
781 | $this->query['hint'] = $index; |
||
782 | 17 | ||
783 | return $this; |
||
784 | 17 | } |
|
785 | |||
786 | public function hydrate(bool $bool = true) : self |
||
787 | { |
||
788 | $this->hydrate = $bool; |
||
789 | |||
790 | return $this; |
||
791 | } |
||
792 | |||
793 | /** |
||
794 | * Set the immortal cursor flag. |
||
795 | */ |
||
796 | public function immortal(bool $bool = true) : self |
||
797 | { |
||
798 | $this->query['immortal'] = $bool; |
||
799 | |||
800 | return $this; |
||
801 | } |
||
802 | |||
803 | 24 | /** |
|
804 | * Specify $in criteria for the current field. |
||
805 | 24 | * |
|
806 | * @see Expr::in() |
||
807 | 24 | * @see http://docs.mongodb.org/manual/reference/operator/in/ |
|
808 | */ |
||
809 | public function in(array $values) : self |
||
810 | { |
||
811 | $this->expr->in($values); |
||
812 | |||
813 | return $this; |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * Increment the current field. |
||
818 | * |
||
819 | * If the field does not exist, it will be set to this value. |
||
820 | 6 | * |
|
821 | * @see Expr::inc() |
||
822 | 6 | * @see http://docs.mongodb.org/manual/reference/operator/inc/ |
|
823 | * |
||
824 | 6 | * @param float|int $value |
|
825 | */ |
||
826 | public function inc($value) : self |
||
832 | |||
833 | public function includesReferenceTo(object $document) : self |
||
839 | 1 | ||
840 | public function insert(?string $documentName = null) : self |
||
841 | { |
||
842 | $this->setDocumentName($documentName); |
||
843 | $this->query['type'] = Query::TYPE_INSERT; |
||
844 | |||
845 | return $this; |
||
846 | } |
||
847 | |||
848 | /** |
||
849 | * Set the $language option for $text criteria. |
||
850 | 1 | * |
|
851 | * This method must be called after text(). |
||
852 | 1 | * |
|
853 | * @see Expr::language() |
||
854 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
|
855 | */ |
||
856 | public function language(string $language) : self |
||
857 | { |
||
858 | $this->expr->language($language); |
||
859 | |||
860 | return $this; |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | 2 | * Set the limit for the query. |
|
865 | * |
||
866 | 2 | * This is only relevant for find queries and count commands. |
|
867 | * |
||
868 | 2 | * @see Query::prepareCursor() |
|
869 | */ |
||
870 | public function limit(int $limit) : self |
||
871 | { |
||
872 | $this->query['limit'] = $limit; |
||
873 | |||
874 | return $this; |
||
875 | } |
||
876 | |||
877 | /** |
||
878 | * Specify $lt criteria for the current field. |
||
879 | * |
||
880 | * @see Expr::lte() |
||
881 | * @see http://docs.mongodb.org/manual/reference/operator/lte/ |
||
882 | * |
||
883 | * @param mixed $value |
||
884 | */ |
||
885 | public function lt($value) : self |
||
886 | { |
||
887 | $this->expr->lt($value); |
||
888 | |||
889 | return $this; |
||
890 | } |
||
891 | |||
892 | /** |
||
893 | * Specify $lte criteria for the current field. |
||
894 | * |
||
895 | * @see Expr::lte() |
||
896 | * @see http://docs.mongodb.org/manual/reference/operator/lte/ |
||
897 | * |
||
898 | * @param mixed $value |
||
899 | */ |
||
900 | public function lte($value) : self |
||
901 | { |
||
902 | $this->expr->lte($value); |
||
903 | |||
904 | return $this; |
||
905 | } |
||
906 | |||
907 | /** |
||
908 | * Updates the value of the field to a specified value if the specified value is greater than the current value of the field. |
||
909 | 1 | * |
|
910 | * @see Expr::max() |
||
911 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/update/max/ |
|
912 | * |
||
913 | 1 | * @param mixed $value |
|
914 | */ |
||
915 | public function max($value) : self |
||
916 | { |
||
917 | $this->expr->max($value); |
||
918 | |||
919 | return $this; |
||
920 | } |
||
921 | |||
922 | /** |
||
923 | * Specifies a cumulative time limit in milliseconds for processing operations on a cursor. |
||
924 | */ |
||
925 | public function maxTimeMS(int $ms) : self |
||
926 | { |
||
927 | $this->query['maxTimeMS'] = $ms; |
||
928 | |||
929 | return $this; |
||
930 | } |
||
931 | |||
932 | /** |
||
933 | * Updates the value of the field to a specified value if the specified value is less than the current value of the field. |
||
934 | 1 | * |
|
935 | * @see Expr::min() |
||
936 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/update/min/ |
|
937 | * |
||
938 | 1 | * @param mixed $value |
|
939 | */ |
||
940 | public function min($value) : self |
||
941 | { |
||
942 | $this->expr->min($value); |
||
943 | |||
944 | return $this; |
||
945 | } |
||
946 | |||
947 | /** |
||
948 | * Specify $mod criteria for the current field. |
||
949 | * |
||
950 | 1 | * @see Expr::mod() |
|
951 | * @see http://docs.mongodb.org/manual/reference/operator/mod/ |
||
952 | 1 | * |
|
953 | * @param float|int $divisor |
||
954 | 1 | * @param float|int $remainder |
|
955 | */ |
||
956 | public function mod($divisor, $remainder = 0) : self |
||
957 | { |
||
958 | $this->expr->mod($divisor, $remainder); |
||
959 | |||
960 | return $this; |
||
961 | } |
||
962 | |||
963 | /** |
||
964 | * Multiply the current field. |
||
965 | * |
||
966 | * If the field does not exist, it will be set to 0. |
||
967 | 1 | * |
|
968 | * @see Expr::mul() |
||
969 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/mul/ |
|
970 | * |
||
971 | 1 | * @param float|int $value |
|
972 | */ |
||
973 | public function mul($value) : self |
||
974 | { |
||
975 | $this->expr->mul($value); |
||
976 | |||
977 | return $this; |
||
978 | } |
||
979 | |||
980 | /** |
||
981 | * Add $near criteria to the query. |
||
982 | * |
||
983 | * A GeoJSON point may be provided as the first and only argument for |
||
984 | * 2dsphere queries. This single parameter may be a GeoJSON point object or |
||
985 | * an array corresponding to the point's JSON representation. |
||
986 | * |
||
987 | 1 | * @see Expr::near() |
|
988 | * @see http://docs.mongodb.org/manual/reference/operator/near/ |
||
989 | 1 | * |
|
990 | * @param float|array|Point $x |
||
991 | 1 | * @param float $y |
|
992 | */ |
||
993 | public function near($x, $y = null) : self |
||
994 | { |
||
995 | $this->expr->near($x, $y); |
||
996 | |||
997 | return $this; |
||
998 | } |
||
999 | |||
1000 | /** |
||
1001 | * Add $nearSphere criteria to the query. |
||
1002 | * |
||
1003 | * A GeoJSON point may be provided as the first and only argument for |
||
1004 | * 2dsphere queries. This single parameter may be a GeoJSON point object or |
||
1005 | * an array corresponding to the point's JSON representation. |
||
1006 | * |
||
1007 | 1 | * @see Expr::nearSphere() |
|
1008 | * @see http://docs.mongodb.org/manual/reference/operator/nearSphere/ |
||
1009 | 1 | * |
|
1010 | * @param float|array|Point $x |
||
1011 | 1 | * @param float $y |
|
1012 | */ |
||
1013 | public function nearSphere($x, $y = null) : self |
||
1014 | { |
||
1015 | $this->expr->nearSphere($x, $y); |
||
1016 | |||
1017 | return $this; |
||
1018 | } |
||
1019 | |||
1020 | /** |
||
1021 | * Negates an expression for the current field. |
||
1022 | * |
||
1023 | * You can create a new expression using the {@link Builder::expr()} method. |
||
1024 | 3 | * |
|
1025 | * @see Expr::not() |
||
1026 | 3 | * @see http://docs.mongodb.org/manual/reference/operator/not/ |
|
1027 | * |
||
1028 | 3 | * @param array|Expr $expression |
|
1029 | */ |
||
1030 | public function not($expression) : self |
||
1031 | { |
||
1032 | $this->expr->not($expression); |
||
1033 | |||
1034 | return $this; |
||
1035 | } |
||
1036 | |||
1037 | /** |
||
1038 | * Specify $ne criteria for the current field. |
||
1039 | 4 | * |
|
1040 | * @see Expr::notEqual() |
||
1041 | 4 | * @see http://docs.mongodb.org/manual/reference/operator/ne/ |
|
1042 | * |
||
1043 | 4 | * @param mixed $value |
|
1044 | */ |
||
1045 | public function notEqual($value) : self |
||
1046 | { |
||
1047 | $this->expr->notEqual($value); |
||
1048 | |||
1049 | return $this; |
||
1050 | } |
||
1051 | |||
1052 | /** |
||
1053 | * Specify $nin criteria for the current field. |
||
1054 | 4 | * |
|
1055 | * @see Expr::notIn() |
||
1056 | 4 | * @see http://docs.mongodb.org/manual/reference/operator/nin/ |
|
1057 | * |
||
1058 | 4 | * @param array $values |
|
1059 | */ |
||
1060 | public function notIn(array $values) : self |
||
1061 | { |
||
1062 | $this->expr->notIn($values); |
||
1063 | |||
1064 | return $this; |
||
1065 | } |
||
1066 | |||
1067 | 3 | /** |
|
1068 | * Remove the first element from the current array field. |
||
1069 | 3 | * |
|
1070 | * @see Expr::popFirst() |
||
1071 | 3 | * @see http://docs.mongodb.org/manual/reference/operator/pop/ |
|
1072 | */ |
||
1073 | public function popFirst() : self |
||
1074 | { |
||
1075 | $this->expr->popFirst(); |
||
1076 | |||
1077 | return $this; |
||
1078 | } |
||
1079 | |||
1080 | 2 | /** |
|
1081 | * Remove the last element from the current array field. |
||
1082 | 2 | * |
|
1083 | * @see Expr::popLast() |
||
1084 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/pop/ |
|
1085 | */ |
||
1086 | public function popLast() : self |
||
1087 | { |
||
1088 | $this->expr->popLast(); |
||
1089 | |||
1090 | return $this; |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * Use a primer to eagerly load all references in the current field. |
||
1095 | * |
||
1096 | * If $primer is true or a callable is provided, referenced documents for |
||
1097 | * this field will loaded into UnitOfWork immediately after the query is |
||
1098 | * executed. This will avoid multiple queries due to lazy initialization of |
||
1099 | * Proxy objects. |
||
1100 | * |
||
1101 | * If $primer is false, no priming will take place. That is also the default |
||
1102 | * behavior. |
||
1103 | * |
||
1104 | * If a custom callable is used, its signature should conform to the default |
||
1105 | 22 | * Closure defined in {@link ReferencePrimer::__construct()}. |
|
1106 | * |
||
1107 | 22 | * @param bool|callable $primer |
|
1108 | 1 | * |
|
1109 | * @throws InvalidArgumentException If $primer is not boolean or callable. |
||
1110 | */ |
||
1111 | 21 | public function prime($primer = true) : self |
|
1112 | { |
||
1113 | if (! is_bool($primer) && ! is_callable($primer)) { |
||
1114 | throw new InvalidArgumentException('$primer is not a boolean or callable'); |
||
1115 | } |
||
1116 | |||
1117 | 21 | if ($primer === false) { |
|
1118 | unset($this->primers[$this->currentField]); |
||
1119 | 21 | ||
1120 | return $this; |
||
1121 | } |
||
1122 | |||
1123 | $this->primers[$this->currentField] = $primer; |
||
1124 | |||
1125 | return $this; |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * Remove all elements matching the given value or expression from the |
||
1130 | * current array field. |
||
1131 | 1 | * |
|
1132 | * @see Expr::pull() |
||
1133 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/pull/ |
|
1134 | * |
||
1135 | 1 | * @param mixed|Expr $valueOrExpression |
|
1136 | */ |
||
1137 | public function pull($valueOrExpression) : self |
||
1138 | { |
||
1139 | $this->expr->pull($valueOrExpression); |
||
1140 | |||
1141 | return $this; |
||
1142 | } |
||
1143 | |||
1144 | /** |
||
1145 | 1 | * Remove all elements matching any of the given values from the current |
|
1146 | * array field. |
||
1147 | 1 | * |
|
1148 | * @see Expr::pullAll() |
||
1149 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/pullAll/ |
|
1150 | */ |
||
1151 | public function pullAll(array $values) : self |
||
1152 | { |
||
1153 | $this->expr->pullAll($values); |
||
1154 | |||
1155 | return $this; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Append one or more values to the current array field. |
||
1160 | * |
||
1161 | * If the field does not exist, it will be set to an array containing the |
||
1162 | * value(s) in the argument. If the field is not an array, the query |
||
1163 | * will yield an error. |
||
1164 | * |
||
1165 | * Multiple values may be specified by providing an Expr object and using |
||
1166 | * {@link Expr::each()}. {@link Expr::slice()} and {@link Expr::sort()} may |
||
1167 | * also be used to limit and order array elements, respectively. |
||
1168 | * |
||
1169 | * @see Expr::push() |
||
1170 | * @see http://docs.mongodb.org/manual/reference/operator/push/ |
||
1171 | 6 | * @see http://docs.mongodb.org/manual/reference/operator/each/ |
|
1172 | * @see http://docs.mongodb.org/manual/reference/operator/slice/ |
||
1173 | 6 | * @see http://docs.mongodb.org/manual/reference/operator/sort/ |
|
1174 | * |
||
1175 | 6 | * @param mixed|Expr $valueOrExpression |
|
1176 | */ |
||
1177 | public function push($valueOrExpression) : self |
||
1178 | { |
||
1179 | $this->expr->push($valueOrExpression); |
||
1180 | |||
1181 | return $this; |
||
1182 | } |
||
1183 | |||
1184 | /** |
||
1185 | * Specify $gte and $lt criteria for the current field. |
||
1186 | * |
||
1187 | * This method is shorthand for specifying $gte criteria on the lower bound |
||
1188 | * and $lt criteria on the upper bound. The upper bound is not inclusive. |
||
1189 | 3 | * |
|
1190 | * @see Expr::range() |
||
1191 | 3 | * |
|
1192 | * @param mixed $start |
||
1193 | 3 | * @param mixed $end |
|
1194 | */ |
||
1195 | public function range($start, $end) : self |
||
1201 | |||
1202 | public function readOnly(bool $bool = true) : self |
||
1208 | |||
1209 | public function references(object $document) : self |
||
1215 | |||
1216 | public function refresh(bool $bool = true) : self |
||
1222 | 1 | ||
1223 | public function remove(?string $documentName = null) : self |
||
1224 | { |
||
1225 | $this->setDocumentName($documentName); |
||
1226 | $this->query['type'] = Query::TYPE_REMOVE; |
||
1227 | |||
1228 | return $this; |
||
1229 | } |
||
1230 | |||
1231 | /** |
||
1232 | * Rename the current field. |
||
1233 | * |
||
1234 | * @see Expr::rename() |
||
1235 | * @see http://docs.mongodb.org/manual/reference/operator/rename/ |
||
1236 | */ |
||
1237 | public function rename(string $name) : self |
||
1238 | 4 | { |
|
1239 | $this->expr->rename($name); |
||
1240 | 4 | ||
1241 | 4 | return $this; |
|
1242 | } |
||
1243 | 4 | ||
1244 | public function returnNew(bool $bool = true) : self |
||
1245 | { |
||
1246 | $this->refresh(true); |
||
1247 | $this->query['new'] = $bool; |
||
1248 | |||
1249 | return $this; |
||
1250 | } |
||
1251 | 19 | ||
1252 | /** |
||
1253 | 19 | * Set one or more fields to be included in the query projection. |
|
1254 | 18 | * |
|
1255 | * @param array|string $fieldName,... |
||
1256 | */ |
||
1257 | 19 | public function select($fieldName = null) : self |
|
1258 | { |
||
1259 | 19 | if (! isset($this->query['select'])) { |
|
1260 | 16 | $this->query['select'] = []; |
|
1261 | } |
||
1262 | |||
1263 | 19 | $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); |
|
1264 | |||
1265 | foreach ($fieldNames as $fieldName) { |
||
1266 | $this->query['select'][$fieldName] = 1; |
||
1267 | } |
||
1268 | |||
1269 | return $this; |
||
1270 | } |
||
1271 | |||
1272 | /** |
||
1273 | * Select only matching embedded documents in an array field for the query |
||
1274 | 2 | * projection. |
|
1275 | * |
||
1276 | 2 | * @see http://docs.mongodb.org/manual/reference/projection/elemMatch/ |
|
1277 | 1 | * |
|
1278 | * @param array|Expr $expression |
||
1279 | 2 | */ |
|
1280 | public function selectElemMatch(string $fieldName, $expression) : self |
||
1281 | 2 | { |
|
1282 | if ($expression instanceof Expr) { |
||
1283 | $expression = $expression->getQuery(); |
||
1284 | } |
||
1285 | $this->query['select'][$fieldName] = ['$elemMatch' => $expression]; |
||
1286 | |||
1287 | return $this; |
||
1288 | } |
||
1289 | 3 | ||
1290 | /** |
||
1291 | 3 | * Select a metadata field for the query projection. |
|
1292 | * |
||
1293 | 3 | * @see http://docs.mongodb.org/master/reference/operator/projection/meta/ |
|
1294 | */ |
||
1295 | public function selectMeta(string $fieldName, string $metaDataKeyword) : self |
||
1296 | { |
||
1297 | $this->query['select'][$fieldName] = ['$meta' => $metaDataKeyword]; |
||
1298 | |||
1299 | return $this; |
||
1300 | } |
||
1301 | |||
1302 | /** |
||
1303 | * Select a slice of an array field for the query projection. |
||
1304 | * |
||
1305 | 3 | * The $countOrSkip parameter has two very different meanings, depending on |
|
1306 | * whether or not $limit is provided. See the MongoDB documentation for more |
||
1307 | 3 | * information. |
|
1308 | 3 | * |
|
1309 | 2 | * @see http://docs.mongodb.org/manual/reference/projection/slice/ |
|
1310 | */ |
||
1311 | 3 | public function selectSlice(string $fieldName, int $countOrSkip, ?int $limit = null) : self |
|
1312 | { |
||
1313 | 3 | $slice = $countOrSkip; |
|
1314 | if ($limit !== null) { |
||
1315 | $slice = [$slice, $limit]; |
||
1316 | } |
||
1317 | $this->query['select'][$fieldName] = ['$slice' => $slice]; |
||
1318 | |||
1319 | return $this; |
||
1320 | } |
||
1321 | |||
1322 | /** |
||
1323 | * Set the current field to a value. |
||
1324 | * |
||
1325 | * This is only relevant for insert, update, or findAndUpdate queries. For |
||
1326 | * update and findAndUpdate queries, the $atomic parameter will determine |
||
1327 | * whether or not a $set operator is used. |
||
1328 | 16 | * |
|
1329 | * @see Expr::set() |
||
1330 | 16 | * @see http://docs.mongodb.org/manual/reference/operator/set/ |
|
1331 | * |
||
1332 | 16 | * @param mixed $value |
|
1333 | */ |
||
1334 | public function set($value, bool $atomic = true) : self |
||
1335 | { |
||
1336 | $this->expr->set($value, $atomic && $this->query['type'] !== Query::TYPE_INSERT); |
||
1337 | |||
1338 | return $this; |
||
1339 | } |
||
1340 | 1 | ||
1341 | /** |
||
1342 | 1 | * Set the expression's "new object". |
|
1343 | * |
||
1344 | 1 | * @see Expr::setNewObj() |
|
1345 | */ |
||
1346 | public function setNewObj(array $newObj) : self |
||
1347 | { |
||
1348 | $this->expr->setNewObj($newObj); |
||
1349 | |||
1350 | return $this; |
||
1351 | } |
||
1352 | |||
1353 | /** |
||
1354 | * Set the current field to the value if the document is inserted in an |
||
1355 | * upsert operation. |
||
1356 | * |
||
1357 | * If an update operation with upsert: true results in an insert of a |
||
1358 | * document, then $setOnInsert assigns the specified values to the fields in |
||
1359 | * the document. If the update operation does not result in an insert, |
||
1360 | * $setOnInsert does nothing. |
||
1361 | 2 | * |
|
1362 | * @see Expr::setOnInsert() |
||
1363 | 2 | * @see https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/ |
|
1364 | * |
||
1365 | 2 | * @param mixed $value |
|
1366 | */ |
||
1367 | public function setOnInsert($value) : self |
||
1368 | { |
||
1369 | $this->expr->setOnInsert($value); |
||
1370 | |||
1371 | return $this; |
||
1372 | } |
||
1373 | |||
1374 | /** |
||
1375 | 6 | * Set the read preference for the query. |
|
1376 | * |
||
1377 | 6 | * This is only relevant for read-only queries and commands. |
|
1378 | * |
||
1379 | 6 | * @see http://docs.mongodb.org/manual/core/read-preference/ |
|
1380 | */ |
||
1381 | public function setReadPreference(ReadPreference $readPreference) : self |
||
1382 | { |
||
1383 | $this->query['readPreference'] = $readPreference; |
||
1384 | |||
1385 | return $this; |
||
1386 | } |
||
1387 | 18 | ||
1388 | public function setRewindable(bool $rewindable = true): self |
||
1389 | 18 | { |
|
1390 | $this->rewindable = $rewindable; |
||
1391 | 18 | ||
1392 | return $this; |
||
1393 | } |
||
1394 | |||
1395 | /** |
||
1396 | * Set the expression's query criteria. |
||
1397 | * |
||
1398 | * @see Expr::setQuery() |
||
1399 | */ |
||
1400 | 1 | public function setQueryArray(array $query) : self |
|
1406 | |||
1407 | /** |
||
1408 | * Specify $size criteria for the current field. |
||
1409 | * |
||
1410 | * @see Expr::size() |
||
1411 | * @see http://docs.mongodb.org/manual/reference/operator/size/ |
||
1412 | */ |
||
1413 | public function size(int $size) : self |
||
1414 | { |
||
1415 | $this->expr->size($size); |
||
1416 | |||
1417 | return $this; |
||
1418 | } |
||
1419 | |||
1420 | /** |
||
1421 | * Set the skip for the query cursor. |
||
1422 | * |
||
1423 | * This is only relevant for find queries, or mapReduce queries that store |
||
1424 | * results in an output collection and return a cursor. |
||
1425 | * |
||
1426 | * @see Query::prepareCursor() |
||
1427 | */ |
||
1428 | public function skip(int $skip) : self |
||
1429 | { |
||
1430 | $this->query['skip'] = $skip; |
||
1431 | |||
1432 | return $this; |
||
1433 | } |
||
1434 | |||
1435 | /** |
||
1436 | * Set the snapshot cursor flag. |
||
1437 | */ |
||
1438 | public function snapshot(bool $bool = true) : self |
||
1439 | { |
||
1440 | $this->query['snapshot'] = $bool; |
||
1441 | 28 | ||
1442 | return $this; |
||
1443 | 28 | } |
|
1444 | 28 | ||
1445 | /** |
||
1446 | * Set one or more field/order pairs on which to sort the query. |
||
1447 | 28 | * |
|
1448 | * If sorting by multiple fields, the first argument should be an array of |
||
1449 | 28 | * field name (key) and order (value) pairs. |
|
1450 | 15 | * |
|
1451 | 9 | * @param array|string $fieldName Field name or array of field/order pairs |
|
1452 | * @param int|string $order Field order (if one field is specified) |
||
1453 | 15 | */ |
|
1454 | public function sort($fieldName, $order = 1) : self |
||
1455 | { |
||
1456 | 28 | if (! isset($this->query['sort'])) { |
|
1457 | $this->query['sort'] = []; |
||
1458 | } |
||
1459 | |||
1460 | $fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; |
||
1461 | |||
1462 | foreach ($fields as $fieldName => $order) { |
||
1463 | if (is_string($order)) { |
||
1464 | $order = strtolower($order) === 'asc' ? 1 : -1; |
||
1465 | } |
||
1466 | $this->query['sort'][$fieldName] = (int) $order; |
||
1467 | } |
||
1468 | |||
1469 | 3 | return $this; |
|
1470 | } |
||
1471 | |||
1472 | /** |
||
1473 | * Specify a projected metadata field on which to sort the query. |
||
1474 | * |
||
1475 | 3 | * Sort order is not configurable for metadata fields. Sorting by a metadata |
|
1476 | 2 | * field requires the same field and $meta expression to exist in the |
|
1477 | * projection document. This method will call {@link Builder::selectMeta()} |
||
1478 | * if the field is not already set in the projection. |
||
1479 | 3 | * |
|
1480 | * @see http://docs.mongodb.org/master/reference/operator/projection/meta/#sort |
||
1481 | 3 | */ |
|
1482 | public function sortMeta(string $fieldName, string $metaDataKeyword) : self |
||
1483 | { |
||
1484 | /* It's possible that the field is already projected without the $meta |
||
1485 | * operator. We'll assume that the user knows what they're doing in that |
||
1486 | * case and will not attempt to override the projection. |
||
1487 | */ |
||
1488 | if (! isset($this->query['select'][$fieldName])) { |
||
1489 | $this->selectMeta($fieldName, $metaDataKeyword); |
||
1490 | } |
||
1491 | |||
1492 | 1 | $this->query['sort'][$fieldName] = ['$meta' => $metaDataKeyword]; |
|
1493 | |||
1494 | 1 | return $this; |
|
1495 | } |
||
1496 | 1 | ||
1497 | /** |
||
1498 | * Specify $text criteria for the current field. |
||
1499 | * |
||
1500 | * The $language option may be set with {@link Builder::language()}. |
||
1501 | * |
||
1502 | * @see Expr::text() |
||
1503 | * @see http://docs.mongodb.org/master/reference/operator/query/text/ |
||
1504 | */ |
||
1505 | public function text(string $search) : self |
||
1506 | { |
||
1507 | 2 | $this->expr->text($search); |
|
1508 | |||
1509 | 2 | return $this; |
|
1510 | } |
||
1511 | 2 | ||
1512 | /** |
||
1513 | * Specify $type criteria for the current field. |
||
1514 | * |
||
1515 | * @see Expr::type() |
||
1516 | * @see http://docs.mongodb.org/manual/reference/operator/type/ |
||
1517 | * |
||
1518 | * @param int|string $type |
||
1519 | */ |
||
1520 | public function type($type) : self |
||
1521 | { |
||
1522 | 4 | $this->expr->type($type); |
|
1523 | |||
1524 | 4 | return $this; |
|
1525 | } |
||
1526 | 4 | ||
1527 | /** |
||
1528 | * Unset the current field. |
||
1529 | 23 | * |
|
1530 | * The field will be removed from the document (not set to null). |
||
1531 | 23 | * |
|
1532 | 23 | * @see Expr::unsetField() |
|
1533 | 23 | * @see http://docs.mongodb.org/manual/reference/operator/unset/ |
|
1534 | */ |
||
1535 | 23 | public function unsetField() : self |
|
1541 | 4 | ||
1542 | 4 | public function updateOne(?string $documentName = null) : self |
|
1543 | { |
||
1544 | 4 | $this->setDocumentName($documentName); |
|
1545 | $this->query['type'] = Query::TYPE_UPDATE; |
||
1546 | $this->query['multiple'] = false; |
||
1547 | |||
1548 | return $this; |
||
1549 | } |
||
1550 | 7 | ||
1551 | public function updateMany(?string $documentName = null) : self |
||
1552 | 7 | { |
|
1553 | $this->setDocumentName($documentName); |
||
1554 | 7 | $this->query['type'] = Query::TYPE_UPDATE; |
|
1555 | $this->query['multiple'] = true; |
||
1556 | |||
1557 | return $this; |
||
1558 | } |
||
1559 | |||
1560 | /** |
||
1561 | * Set the "upsert" option for an update or findAndUpdate query. |
||
1562 | */ |
||
1563 | public function upsert(bool $bool = true) : self |
||
1564 | { |
||
1565 | 3 | $this->query['upsert'] = $bool; |
|
1566 | |||
1567 | 3 | return $this; |
|
1568 | } |
||
1569 | 3 | ||
1570 | /** |
||
1571 | * Specify a JavaScript expression to use for matching documents. |
||
1572 | * |
||
1573 | * @see Expr::where() |
||
1574 | * @see http://docs.mongodb.org/manual/reference/operator/where/ |
||
1575 | * |
||
1576 | * @param string|Javascript $javascript |
||
1577 | */ |
||
1578 | public function where($javascript) : self |
||
1579 | 2 | { |
|
1580 | $this->expr->where($javascript); |
||
1581 | 2 | ||
1582 | 2 | return $this; |
|
1583 | 2 | } |
|
1584 | 2 | ||
1585 | 2 | /** |
|
1586 | 2 | * Get Discriminator Values |
|
1587 | 2 | * |
|
1588 | * @param string[] $classNames |
||
1589 | 2 | * |
|
1590 | 1 | * @throws InvalidArgumentException If the number of found collections > 1. |
|
1591 | */ |
||
1592 | private function getDiscriminatorValues($classNames) : array |
||
1593 | 1 | { |
|
1594 | $discriminatorValues = []; |
||
1595 | $collections = []; |
||
1596 | foreach ($classNames as $className) { |
||
1597 | $class = $this->dm->getClassMetadata($className); |
||
1598 | $discriminatorValues[] = $class->discriminatorValue; |
||
1599 | 281 | $key = $this->dm->getDocumentDatabase($className)->getDatabaseName() . '.' . $class->getCollection(); |
|
1600 | $collections[$key] = $key; |
||
1601 | 281 | } |
|
1602 | 2 | if (count($collections) > 1) { |
|
1603 | 2 | throw new InvalidArgumentException('Documents involved are not all mapped to the same database collection.'); |
|
1604 | } |
||
1605 | 2 | ||
1606 | 2 | return $discriminatorValues; |
|
1607 | 2 | } |
|
1608 | |||
1609 | /** |
||
1610 | 1 | * @param string[]|string|null $documentName an array of document names or just one. |
|
1611 | 1 | */ |
|
1612 | private function setDocumentName($documentName) |
||
1613 | { |
||
1614 | 1 | if (is_array($documentName)) { |
|
1615 | $documentNames = $documentName; |
||
1616 | $documentName = $documentNames[0]; |
||
1617 | 280 | ||
1618 | 42 | $metadata = $this->dm->getClassMetadata($documentName); |
|
1619 | $discriminatorField = $metadata->discriminatorField ?? ClassMetadata::DEFAULT_DISCRIMINATOR_FIELD; |
||
1620 | $discriminatorValues = $this->getDiscriminatorValues($documentNames); |
||
1621 | 280 | ||
1622 | 280 | // If a defaultDiscriminatorValue is set and it is among the discriminators being queries, add NULL to the list |
|
1623 | if ($metadata->defaultDiscriminatorValue && in_array($metadata->defaultDiscriminatorValue, $discriminatorValues)) { |
||
1624 | $discriminatorValues[] = null; |
||
1625 | 280 | } |
|
1626 | 280 | ||
1627 | $this->field($discriminatorField)->in($discriminatorValues); |
||
1628 | } |
||
1629 | |||
1630 | if ($documentName === null) { |
||
1640 | } |
||
1641 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.