Completed
Pull Request — master (#1787)
by Stefano
21:31
created

Match::addOr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
6
7
use Doctrine\ODM\MongoDB\Aggregation\Builder;
8
use Doctrine\ODM\MongoDB\Aggregation\Stage;
9
use Doctrine\ODM\MongoDB\Query\Expr;
10
use GeoJson\Geometry\Geometry;
11
use function func_get_args;
12
13
/**
14
 * Fluent interface for building aggregation pipelines.
15
 */
16
class Match extends Stage
17
{
18
    /** @var Expr */
19
    protected $query;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 60
    public function __construct(Builder $builder)
25
    {
26 60
        parent::__construct($builder);
27
28 60
        $this->query = $this->expr();
29 60
    }
30
31
    /**
32
     * @see http://php.net/manual/en/language.oop5.cloning.php
33
     */
34
    public function __clone()
35
    {
36
        $this->query = clone $this->query;
37
    }
38
39
    /**
40
     * Add one or more $and clauses to the current query.
41
     *
42
     * You can create a new expression using the {@link Builder::matchExpr()}
43
     * method.
44
     *
45
     * @see Expr::addAnd()
46
     * @see http://docs.mongodb.org/manual/reference/operator/and/
47
     * @param array|Expr $expression
48
     * @param array|Expr ...$expressions
49
     * @return $this
50
     */
51 2
    public function addAnd($expression, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expressions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53 2
        $this->query->addAnd(...func_get_args());
54
55 2
        return $this;
56
    }
57
58
    /**
59
     * Add one or more $nor clauses to the current query.
60
     *
61
     * You can create a new expression using the {@link Builder::matchExpr()}
62
     * method.
63
     *
64
     * @see Expr::addNor()
65
     * @see http://docs.mongodb.org/manual/reference/operator/nor/
66
     * @param array|Expr $expression
67
     * @param array|Expr ...$expressions
68
     * @return $this
69
     */
70 2
    public function addNor($expression, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expressions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72 2
        $this->query->addNor(...func_get_args());
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * Add one or more $or clauses to the current query.
79
     *
80
     * You can create a new expression using the {@link Builder::matchExpr()}
81
     * method.
82
     *
83
     * @see Expr::addOr()
84
     * @see http://docs.mongodb.org/manual/reference/operator/or/
85
     * @param array|Expr $expression
86
     * @param array|Expr ...$expressions
87
     * @return $this
88
     */
89 3
    public function addOr($expression, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expressions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91 3
        $this->query->addOr(...func_get_args());
92
93 3
        return $this;
94
    }
95
96
    /**
97
     * Specify $all criteria for the current field.
98
     *
99
     * @see Expr::all()
100
     * @see http://docs.mongodb.org/manual/reference/operator/all/
101
     * @param array $values
102
     * @return $this
103
     */
104 1
    public function all(array $values)
105
    {
106 1
        $this->query->all($values);
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * Return an array of information about the Builder state for debugging.
113
     *
114
     * The $name parameter may be used to return a specific key from the
115
     * internal $query array property. If omitted, the entire array will be
116
     * returned.
117
     *
118
     * @param string $name
119
     * @return mixed
120
     */
121
    public function debug($name = null)
122
    {
123
        return $name !== null ? $this->query[$name] : $this->query;
124
    }
125
126
    /**
127
     * Specify $elemMatch criteria for the current field.
128
     *
129
     * You can create a new expression using the {@link Builder::matchExpr()}
130
     * method.
131
     *
132
     * @see Expr::elemMatch()
133
     * @see http://docs.mongodb.org/manual/reference/operator/elemMatch/
134
     * @param array|Expr $expression
135
     * @return $this
136
     */
137
    public function elemMatch($expression)
138
    {
139
        $this->query->elemMatch($expression);
140
141
        return $this;
142
    }
143
144
    /**
145
     * Specify an equality match for the current field.
146
     *
147
     * @see Expr::equals()
148
     * @param mixed $value
149
     * @return $this
150
     */
151 11
    public function equals($value)
152
    {
153 11
        $this->query->equals($value);
154
155 11
        return $this;
156
    }
157
158
    /**
159
     * Specify $exists criteria for the current field.
160
     *
161
     * @see Expr::exists()
162
     * @see http://docs.mongodb.org/manual/reference/operator/exists/
163
     * @param bool $bool
164
     * @return $this
165
     */
166 2
    public function exists($bool)
167
    {
168 2
        $this->query->exists((bool) $bool);
169
170 2
        return $this;
171
    }
172
173
    /**
174
     * Create a new Expr instance that can be used to build partial expressions
175
     * for other operator methods.
176
     *
177
     * @return Expr $expr
178
     */
179 60
    public function expr()
180
    {
181 60
        return $this->builder->matchExpr();
182
    }
183
184
    /**
185
     * Set the current field for building the expression.
186
     *
187
     * @see Expr::field()
188
     * @param string $field
189
     * @return $this
190
     */
191 13
    public function field($field)
192
    {
193 13
        $this->query->field((string) $field);
194
195 13
        return $this;
196
    }
197
198
    /**
199
     * Add $geoIntersects criteria with a GeoJSON geometry to the query.
200
     *
201
     * The geometry parameter GeoJSON object or an array corresponding to the
202
     * geometry's JSON representation.
203
     *
204
     * @see Expr::geoIntersects()
205
     * @see http://docs.mongodb.org/manual/reference/operator/geoIntersects/
206
     * @param array|Geometry $geometry
207
     * @return $this
208
     */
209 1
    public function geoIntersects($geometry)
210
    {
211 1
        $this->query->geoIntersects($geometry);
212
213 1
        return $this;
214
    }
215
216
    /**
217
     * Add $geoWithin criteria with a GeoJSON geometry to the query.
218
     *
219
     * The geometry parameter GeoJSON object or an array corresponding to the
220
     * geometry's JSON representation.
221
     *
222
     * @see Expr::geoWithin()
223
     * @see http://docs.mongodb.org/manual/reference/operator/geoWithin/
224
     * @return $this
225
     */
226 1
    public function geoWithin(Geometry $geometry)
227
    {
228 1
        $this->query->geoWithin($geometry);
229
230 1
        return $this;
231
    }
232
233
    /**
234
     * Add $geoWithin criteria with a $box shape to the query.
235
     *
236
     * A rectangular polygon will be constructed from a pair of coordinates
237
     * corresponding to the bottom left and top right corners.
238
     *
239
     * Note: the $box operator only supports legacy coordinate pairs and 2d
240
     * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
241
     *
242
     * @see Expr::geoWithinBox()
243
     * @see http://docs.mongodb.org/manual/reference/operator/box/
244
     * @param float $x1
245
     * @param float $y1
246
     * @param float $x2
247
     * @param float $y2
248
     * @return $this
249
     */
250 1
    public function geoWithinBox($x1, $y1, $x2, $y2)
251
    {
252 1
        $this->query->geoWithinBox($x1, $y1, $x2, $y2);
253
254 1
        return $this;
255
    }
256
257
    /**
258
     * Add $geoWithin criteria with a $center shape to the query.
259
     *
260
     * Note: the $center operator only supports legacy coordinate pairs and 2d
261
     * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
262
     *
263
     * @see Expr::geoWithinCenter()
264
     * @see http://docs.mongodb.org/manual/reference/operator/center/
265
     * @param float $x
266
     * @param float $y
267
     * @param float $radius
268
     * @return $this
269
     */
270 1
    public function geoWithinCenter($x, $y, $radius)
271
    {
272 1
        $this->query->geoWithinCenter($x, $y, $radius);
273
274 1
        return $this;
275
    }
276
277
    /**
278
     * Add $geoWithin criteria with a $centerSphere shape to the query.
279
     *
280
     * Note: the $centerSphere operator supports both 2d and 2dsphere indexes.
281
     *
282
     * @see Expr::geoWithinCenterSphere()
283
     * @see http://docs.mongodb.org/manual/reference/operator/centerSphere/
284
     * @param float $x
285
     * @param float $y
286
     * @param float $radius
287
     * @return $this
288
     */
289 1
    public function geoWithinCenterSphere($x, $y, $radius)
290
    {
291 1
        $this->query->geoWithinCenterSphere($x, $y, $radius);
292
293 1
        return $this;
294
    }
295
296
    /**
297
     * Add $geoWithin criteria with a $polygon shape to the query.
298
     *
299
     * Point coordinates are in x, y order (easting, northing for projected
300
     * coordinates, longitude, latitude for geographic coordinates).
301
     *
302
     * The last point coordinate is implicitly connected with the first.
303
     *
304
     * Note: the $polygon operator only supports legacy coordinate pairs and 2d
305
     * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes.
306
     *
307
     * @see Expr::geoWithinPolygon()
308
     * @see http://docs.mongodb.org/manual/reference/operator/polygon/
309
     * @param array $point1    First point of the polygon
310
     * @param array $point2    Second point of the polygon
311
     * @param array $point3    Third point of the polygon
312
     * @param array ...$points Additional points of the polygon
313
     * @return $this
314
     */
315 1
    public function geoWithinPolygon($point1, $point2, $point3, ...$points)
0 ignored issues
show
Unused Code introduced by
The parameter $point1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $point2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $point3 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $points is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
316
    {
317 1
        $this->query->geoWithinPolygon(...func_get_args());
318
319 1
        return $this;
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325 9
    public function getExpression()
326
    {
327
        return [
328 9
            '$match' => $this->query->getQuery() ?: (object) [],
329
        ];
330
    }
331
332
    /**
333
     * Specify $gt criteria for the current field.
334
     *
335
     * @see Expr::gt()
336
     * @see http://docs.mongodb.org/manual/reference/operator/gt/
337
     * @param mixed $value
338
     * @return $this
339
     */
340 2
    public function gt($value)
341
    {
342 2
        $this->query->gt($value);
343
344 2
        return $this;
345
    }
346
347
    /**
348
     * Specify $gte criteria for the current field.
349
     *
350
     * @see Expr::gte()
351
     * @see http://docs.mongodb.org/manual/reference/operator/gte/
352
     * @param mixed $value
353
     * @return $this
354
     */
355 2
    public function gte($value)
356
    {
357 2
        $this->query->gte($value);
358
359 2
        return $this;
360
    }
361
362
    /**
363
     * Specify $in criteria for the current field.
364
     *
365
     * @see Expr::in()
366
     * @see http://docs.mongodb.org/manual/reference/operator/in/
367
     * @param array $values
368
     * @return $this
369
     */
370 2
    public function in(array $values)
371
    {
372 2
        $this->query->in($values);
373
374 2
        return $this;
375
    }
376
377
    /**
378
     * @param object $document
379
     * @return $this
380
     */
381
    public function includesReferenceTo($document)
382
    {
383
        $this->query->includesReferenceTo($document);
384
385
        return $this;
386
    }
387
388
    /**
389
     * Set the $language option for $text criteria.
390
     *
391
     * This method must be called after text().
392
     *
393
     * @see Expr::language()
394
     * @see http://docs.mongodb.org/manual/reference/operator/text/
395
     * @param string $language
396
     * @return $this
397
     */
398 1
    public function language($language)
399
    {
400 1
        $this->query->language($language);
401
402 1
        return $this;
403
    }
404
405
    /**
406
     * Specify $lt criteria for the current field.
407
     *
408
     * @see Expr::lte()
409
     * @see http://docs.mongodb.org/manual/reference/operator/lte/
410
     * @param mixed $value
411
     * @return $this
412
     */
413
    public function lt($value)
414
    {
415
        $this->query->lt($value);
416
417
        return $this;
418
    }
419
420
    /**
421
     * Specify $lte criteria for the current field.
422
     *
423
     * @see Expr::lte()
424
     * @see http://docs.mongodb.org/manual/reference/operator/lte/
425
     * @param mixed $value
426
     * @return $this
427
     */
428 1
    public function lte($value)
429
    {
430 1
        $this->query->lte($value);
431
432 1
        return $this;
433
    }
434
435
    /**
436
     * Add $maxDistance criteria to the query.
437
     *
438
     * If the query uses GeoJSON points, $maxDistance will be interpreted in
439
     * meters. If legacy point coordinates are used, $maxDistance will be
440
     * interpreted in radians.
441
     *
442
     * @see Expr::maxDistance()
443
     * @see http://docs.mongodb.org/manual/reference/command/geoNear/
444
     * @see http://docs.mongodb.org/manual/reference/operator/maxDistance/
445
     * @see http://docs.mongodb.org/manual/reference/operator/near/
446
     * @see http://docs.mongodb.org/manual/reference/operator/nearSphere/
447
     * @param float $maxDistance
448
     * @return $this
449
     */
450
    public function maxDistance($maxDistance)
451
    {
452
        $this->query->maxDistance($maxDistance);
0 ignored issues
show
Bug introduced by
The method maxDistance() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Expr>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
453
454
        return $this;
455
    }
456
457
    /**
458
     * Add $minDistance criteria to the query.
459
     *
460
     * If the query uses GeoJSON points, $minDistance will be interpreted in
461
     * meters. If legacy point coordinates are used, $minDistance will be
462
     * interpreted in radians.
463
     *
464
     * @see Expr::minDistance()
465
     * @see http://docs.mongodb.org/manual/reference/command/geoNear/
466
     * @see http://docs.mongodb.org/manual/reference/operator/minDistance/
467
     * @see http://docs.mongodb.org/manual/reference/operator/near/
468
     * @see http://docs.mongodb.org/manual/reference/operator/nearSphere/
469
     * @param float $minDistance
470
     * @return $this
471
     */
472
    public function minDistance($minDistance)
473
    {
474
        $this->query->minDistance($minDistance);
0 ignored issues
show
Bug introduced by
The method minDistance() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Expr>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
475
476
        return $this;
477
    }
478
479
    /**
480
     * Specify $mod criteria for the current field.
481
     *
482
     * @see Expr::mod()
483
     * @see http://docs.mongodb.org/manual/reference/operator/mod/
484
     * @param float|int $divisor
485
     * @param float|int $remainder
486
     * @return $this
487
     */
488 1
    public function mod($divisor, $remainder = 0)
489
    {
490 1
        $this->query->mod($divisor, $remainder);
491
492 1
        return $this;
493
    }
494
495
    /**
496
     * Negates an expression for the current field.
497
     *
498
     * You can create a new expression using the {@link Builder::matchExpr()}
499
     * method.
500
     *
501
     * @see Expr::not()
502
     * @see http://docs.mongodb.org/manual/reference/operator/not/
503
     * @param array|Expr $expression
504
     * @return $this
505
     */
506 1
    public function not($expression)
507
    {
508 1
        $this->query->not($expression);
509
510 1
        return $this;
511
    }
512
513
    /**
514
     * Specify $ne criteria for the current field.
515
     *
516
     * @see Expr::notEqual()
517
     * @see http://docs.mongodb.org/manual/reference/operator/ne/
518
     * @param mixed $value
519
     * @return $this
520
     */
521 2
    public function notEqual($value)
522
    {
523 2
        $this->query->notEqual($value);
524
525 2
        return $this;
526
    }
527
528
    /**
529
     * Specify $nin criteria for the current field.
530
     *
531
     * @see Expr::notIn()
532
     * @see http://docs.mongodb.org/manual/reference/operator/nin/
533
     * @param array $values
534
     * @return $this
535
     */
536 1
    public function notIn(array $values)
537
    {
538 1
        $this->query->notIn($values);
539
540 1
        return $this;
541
    }
542
543
    /**
544
     * Specify $gte and $lt criteria for the current field.
545
     *
546
     * This method is shorthand for specifying $gte criteria on the lower bound
547
     * and $lt criteria on the upper bound. The upper bound is not inclusive.
548
     *
549
     * @see Expr::range()
550
     * @param mixed $start
551
     * @param mixed $end
552
     * @return $this
553
     */
554 1
    public function range($start, $end)
555
    {
556 1
        $this->query->range($start, $end);
557
558 1
        return $this;
559
    }
560
561
    /**
562
     * @param object $document
563
     * @return $this
564
     */
565
    public function references($document)
566
    {
567
        $this->query->references($document);
568
569
        return $this;
570
    }
571
572
    /**
573
     * Specify $size criteria for the current field.
574
     *
575
     * @see Expr::size()
576
     * @see http://docs.mongodb.org/manual/reference/operator/size/
577
     * @param int $size
578
     * @return $this
579
     */
580 1
    public function size($size)
581
    {
582 1
        $this->query->size((int) $size);
583
584 1
        return $this;
585
    }
586
587
    /**
588
     * Specify $text criteria for the current field.
589
     *
590
     * The $language option may be set with {@link Builder::language()}.
591
     *
592
     * You can only use this in the first $match stage of a pipeline.
593
     *
594
     * @see Expr::text()
595
     * @see http://docs.mongodb.org/master/reference/operator/query/text/
596
     * @param string $search
597
     * @return $this
598
     */
599 1
    public function text($search)
600
    {
601 1
        $this->query->text($search);
602
603 1
        return $this;
604
    }
605
606
    /**
607
     * Specify $type criteria for the current field.
608
     *
609
     * @see Expr::type()
610
     * @see http://docs.mongodb.org/manual/reference/operator/type/
611
     * @param int $type
612
     * @return $this
613
     */
614 1
    public function type($type)
615
    {
616 1
        $this->query->type($type);
617
618 1
        return $this;
619
    }
620
}
621