Completed
Push — master ( 08b9e1...e0c601 )
by Andreas
13s
created

Expr   F

Complexity

Total Complexity 131

Size/Duplication

Total Lines 1682
Duplicated Lines 3.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 131
lcom 1
cbo 4
dl 59
loc 1682
ccs 264
cts 275
cp 0.96
rs 0.8
c 0
b 0
f 0

102 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A abs() 0 4 1
A add() 0 4 1
A addAnd() 10 10 2
A addOr() 10 10 2
A addToSet() 0 4 1
A allElementsTrue() 0 4 1
A anyElementTrue() 0 4 1
A arrayElemAt() 0 4 1
A avg() 0 4 1
A case() 0 8 1
A ceil() 0 4 1
A cmp() 0 4 1
A concat() 0 4 1
A concatArrays() 0 4 1
A cond() 0 4 1
A convertExpression() 0 10 3
A dateToString() 0 4 1
A dayOfMonth() 0 4 1
A dayOfWeek() 0 4 1
A dayOfYear() 0 4 1
A default() 0 12 2
A divide() 0 4 1
A eq() 0 4 1
A exp() 0 4 1
A expr() 0 4 1
A expression() 0 7 1
A field() 0 7 1
A filter() 0 4 1
A first() 0 4 1
A floor() 0 4 1
A getExpression() 0 4 1
A gt() 0 4 1
A gte() 0 4 1
A hour() 0 4 1
A ifNull() 0 4 1
A in() 0 4 1
A indexOfArray() 13 13 3
A indexOfBytes() 13 13 3
A indexOfCP() 13 13 3
A isArray() 0 4 1
A isoDayOfWeek() 0 4 1
A isoWeek() 0 4 1
A isoWeekYear() 0 4 1
A last() 0 4 1
A let() 0 4 1
A literal() 0 4 1
A ln() 0 4 1
A log() 0 4 1
A log10() 0 4 1
A lt() 0 4 1
A lte() 0 4 1
A map() 0 4 1
A max() 0 4 1
A meta() 0 4 1
A millisecond() 0 4 1
A min() 0 4 1
A minute() 0 4 1
A mod() 0 4 1
A month() 0 4 1
A multiply() 0 4 1
A ne() 0 4 1
A not() 0 4 1
A pow() 0 4 1
A push() 0 4 1
A range() 0 4 1
A reduce() 0 4 1
A reverseArray() 0 4 1
A second() 0 4 1
A setDifference() 0 4 1
A setEquals() 0 4 1
A setIntersection() 0 4 1
A setIsSubset() 0 4 1
A setUnion() 0 4 1
A size() 0 4 1
A slice() 0 8 2
A split() 0 4 1
A sqrt() 0 4 1
A stdDevPop() 0 6 2
A stdDevSamp() 0 6 2
A strcasecmp() 0 4 1
A strLenBytes() 0 4 1
A strLenCP() 0 4 1
A substr() 0 4 1
A substrBytes() 0 4 1
A substrCP() 0 4 1
A subtract() 0 4 1
A sum() 0 4 1
A toLower() 0 4 1
A toUpper() 0 4 1
A trunc() 0 4 1
A type() 0 4 1
A week() 0 4 1
A year() 0 4 1
A zip() 0 12 3
A ensureArray() 0 13 5
A getDocumentPersister() 0 4 1
A operator() 0 10 2
A requiresCurrentField() 0 6 3
A requiresSwitchStatement() 0 12 5
A switch() 0 6 1
A then() 0 18 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Expr 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 Expr, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Aggregation;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
10
use Doctrine\ODM\MongoDB\Types\Type;
11
use function array_map;
12
use function array_merge;
13
use function func_get_args;
14
use function is_array;
15
use function is_string;
16
use function substr;
17
18
/**
19
 * Fluent interface for building aggregation pipelines.
20
 */
21
class Expr
22
{
23
    /** @var DocumentManager */
24
    private $dm;
25
26
    /** @var ClassMetadata */
27
    private $class;
28
29
    /** @var array */
30
    private $expr = [];
31
32
    /**
33
     * The current field we are operating on.
34
     *
35
     * @var string
36
     */
37
    private $currentField;
38
39
    /** @var array */
40
    private $switchBranch;
41
42
    /**
43
     * @inheritDoc
44
     */
45 367
    public function __construct(DocumentManager $dm, ClassMetadata $class)
46
    {
47 367
        $this->dm = $dm;
48 367
        $this->class = $class;
49 367
    }
50
51
    /**
52
     * Returns the absolute value of a number.
53
     *
54
     * The <number> argument can be any valid expression as long as it resolves
55
     * to a number.
56
     *
57
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/abs/
58
     * @param mixed|self $number
59
     * @return $this
60
     *
61
     */
62 3
    public function abs($number)
63
    {
64 3
        return $this->operator('$abs', $number);
65
    }
66
67
    /**
68
     * Adds numbers together or adds numbers and a date. If one of the arguments
69
     * is a date, $add treats the other arguments as milliseconds to add to the
70
     * date.
71
     *
72
     * The arguments can be any valid expression as long as they resolve to
73
     * either all numbers or to numbers and a date.
74
     *
75
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/add/
76
     * @param mixed|self $expression1
77
     * @param mixed|self $expression2
78
     * @param mixed|self ...$expressions Additional expressions
79
     * @return $this
80
     */
81 15
    public function add($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
82
    {
83 15
        return $this->operator('$add', func_get_args());
84
    }
85
86
    /**
87
     * Adds one or more $and clauses to the current expression.
88
     *
89
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/and/
90
     * @param array|self $expression
91
     * @param array|self ...$expressions
92
     * @return $this
93
     */
94 1 View Code Duplication
    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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96 1
        if (! isset($this->expr['$and'])) {
97 1
            $this->expr['$and'] = [];
98
        }
99
100 1
        $this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'ensureArray'], func_get_args()));
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * Adds one or more $or clause to the current expression.
107
     *
108
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/or/
109
     * @param array|self $expression
110
     * @param array|self ...$expressions
111
     * @return $this
112
     */
113 View Code Duplication
    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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        if (! isset($this->expr['$or'])) {
116
            $this->expr['$or'] = [];
117
        }
118
119
        $this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'ensureArray'], func_get_args()));
120
121
        return $this;
122
    }
123
124
    /**
125
     * Returns an array of all unique values that results from applying an
126
     * expression to each document in a group of documents that share the same
127
     * group by key. Order of the elements in the output array is unspecified.
128
     *
129
     * AddToSet is an accumulator operation only available in the group stage.
130
     *
131
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet/
132
     * @param mixed|self $expression
133
     * @return $this
134
     */
135 2
    public function addToSet($expression)
136
    {
137 2
        return $this->operator('$addToSet', $expression);
138
    }
139
140
    /**
141
     * Evaluates an array as a set and returns true if no element in the array
142
     * is false. Otherwise, returns false. An empty array returns true.
143
     *
144
     * The expression must resolve to an array.
145
     *
146
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/allElementsTrue/
147
     * @param mixed|self $expression
148
     * @return $this
149
     */
150 3
    public function allElementsTrue($expression)
151
    {
152 3
        return $this->operator('$allElementsTrue', $expression);
153
    }
154
155
    /**
156
     * Evaluates an array as a set and returns true if any of the elements are
157
     * true and false otherwise. An empty array returns false.
158
     *
159
     * The expression must resolve to an array.
160
     *
161
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/anyElementTrue/
162
     * @param array|self $expression
163
     * @return $this
164
     */
165 3
    public function anyElementTrue($expression)
166
    {
167 3
        return $this->operator('$anyElementTrue', $expression);
168
    }
169
170
    /**
171
     * Returns the element at the specified array index.
172
     *
173
     * The <array> expression can be any valid expression as long as it resolves
174
     * to an array.
175
     * The <idx> expression can be any valid expression as long as it resolves
176
     * to an integer.
177
     *
178
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/arrayElemAt/
179
     * @param mixed|self $array
180
     * @param mixed|self $index
181
     * @return $this
182
     *
183
     */
184 3
    public function arrayElemAt($array, $index)
185
    {
186 3
        return $this->operator('$arrayElemAt', [$array, $index]);
187
    }
188
189
    /**
190
     * Returns the average value of the numeric values that result from applying
191
     * a specified expression to each document in a group of documents that
192
     * share the same group by key. Ignores nun-numeric values.
193
     *
194
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/avg/
195
     * @param mixed|self $expression
196
     * @return $this
197
     */
198 10
    public function avg($expression)
199
    {
200 10
        return $this->operator('$avg', $expression);
201
    }
202
203
    /**
204
     * Adds a case statement for a branch of the $switch operator.
205
     *
206
     * Requires {@link switch()} to be called first. The argument can be any
207
     * valid expression that resolves to a boolean. If the result is not a
208
     * boolean, it is coerced to a boolean value.
209
     *
210
     * @param mixed|self $expression
211
     *
212
     * @return $this
213
     */
214 6
    public function case($expression)
215
    {
216 6
        $this->requiresSwitchStatement(static::class . '::case');
217
218 4
        $this->switchBranch = ['case' => $expression];
219
220 4
        return $this;
221
    }
222
223
    /**
224
     * Returns the smallest integer greater than or equal to the specified number.
225
     *
226
     * The <number> expression can be any valid expression as long as it
227
     * resolves to a number.
228
     *
229
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/ceil/
230
     * @param mixed|self $number
231
     * @return $this
232
     *
233
     */
234 3
    public function ceil($number)
235
    {
236 3
        return $this->operator('$ceil', $number);
237
    }
238
239
    /**
240
     * Compares two values and returns:
241
     * -1 if the first value is less than the second.
242
     * 1 if the first value is greater than the second.
243
     * 0 if the two values are equivalent.
244
     *
245
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/cmp/
246
     * @param mixed|self $expression1
247
     * @param mixed|self $expression2
248
     * @return $this
249
     */
250 3
    public function cmp($expression1, $expression2)
251
    {
252 3
        return $this->operator('$cmp', [$expression1, $expression2]);
253
    }
254
255
    /**
256
     * Concatenates strings and returns the concatenated string.
257
     *
258
     * The arguments can be any valid expression as long as they resolve to
259
     * strings. If the argument resolves to a value of null or refers to a field
260
     * that is missing, $concat returns null.
261
     *
262
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/concat/
263
     * @param mixed|self $expression1
264
     * @param mixed|self $expression2
265
     * @param mixed|self ...$expressions Additional expressions
266
     * @return $this
267
     */
268 9
    public function concat($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
269
    {
270 9
        return $this->operator('$concat', func_get_args());
271
    }
272
273
    /**
274
     * Concatenates arrays to return the concatenated array.
275
     *
276
     * The <array> expressions can be any valid expression as long as they
277
     * resolve to an array.
278
     *
279
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/concatArrays/
280
     * @param mixed|self $array1
281
     * @param mixed|self $array2
282
     * @param mixed|self ...$arrays Additional expressions
283
     * @return $this
284
     *
285
     */
286 6
    public function concatArrays($array1, $array2, ...$arrays)
0 ignored issues
show
Unused Code introduced by
The parameter $array1 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 $array2 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 $arrays 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...
287
    {
288 6
        return $this->operator('$concatArrays', func_get_args());
289
    }
290
291
    /**
292
     * Evaluates a boolean expression to return one of the two specified return
293
     * expressions.
294
     *
295
     * The arguments can be any valid expression.
296
     *
297
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/cond/
298
     * @param mixed|self $if
299
     * @param mixed|self $then
300
     * @param mixed|self $else
301
     * @return $this
302
     */
303 10
    public function cond($if, $then, $else)
304
    {
305 10
        return $this->operator('$cond', ['if' => $if, 'then' => $then, 'else' => $else]);
306
    }
307
308
    /**
309
     * Converts an expression object into an array, recursing into nested items
310
     *
311
     * For expression objects, it calls getExpression on the expression object.
312
     * For arrays, it recursively calls itself for each array item. Other values
313
     * are returned directly.
314
     *
315
     * @param mixed|self $expression
316
     * @return string|array
317
     * @internal
318
     */
319 1
    public static function convertExpression($expression)
320
    {
321 1
        if (is_array($expression)) {
322
            return array_map(['static', 'convertExpression'], $expression);
323 1
        } elseif ($expression instanceof self) {
324 1
            return $expression->getExpression();
325
        }
326
327
        return $expression;
328
    }
329
330
    /**
331
     * Converts a date object to a string according to a user-specified format.
332
     *
333
     * The format string can be any string literal, containing 0 or more format
334
     * specifiers.
335
     * The date argument can be any expression as long as it resolves to a date.
336
     *
337
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dateToString/
338
     * @param string     $format
339
     * @param mixed|self $expression
340
     * @return $this
341
     */
342 3
    public function dateToString($format, $expression)
343
    {
344 3
        return $this->operator('$dateToString', ['format' => $format, 'date' => $expression]);
345
    }
346
347
    /**
348
     * Returns the day of the month for a date as a number between 1 and 31.
349
     *
350
     * The argument can be any expression as long as it resolves to a date.
351
     *
352
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfMonth/
353
     * @param mixed|self $expression
354
     * @return $this
355
     */
356 7
    public function dayOfMonth($expression)
357
    {
358 7
        return $this->operator('$dayOfMonth', $expression);
359
    }
360
361
    /**
362
     * Returns the day of the week for a date as a number between 1 (Sunday) and
363
     * 7 (Saturday).
364
     *
365
     * The argument can be any expression as long as it resolves to a date.
366
     *
367
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfWeek/
368
     * @param mixed|self $expression
369
     * @return $this
370
     */
371 7
    public function dayOfWeek($expression)
372
    {
373 7
        return $this->operator('$dayOfWeek', $expression);
374
    }
375
376
    /**
377
     * Returns the day of the year for a date as a number between 1 and 366.
378
     *
379
     * The argument can be any expression as long as it resolves to a date.
380
     *
381
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfYear/
382
     * @param mixed|self $expression
383
     * @return $this
384
     */
385 3
    public function dayOfYear($expression)
386
    {
387 3
        return $this->operator('$dayOfYear', $expression);
388
    }
389
390
    /**
391
     * Adds a default statement for the current $switch operator.
392
     *
393
     * Requires {@link switch()} to be called first. The argument can be any
394
     * valid expression.
395
     *
396
     * Note: if no default is specified and no branch evaluates to true, the
397
     * $switch operator throws an error.
398
     *
399
     * @param mixed|self $expression
400
     *
401
     * @return $this
402
     */
403 4
    public function default($expression)
404
    {
405 4
        $this->requiresSwitchStatement(static::class . '::default');
406
407 2
        if ($this->currentField) {
408
            $this->expr[$this->currentField]['$switch']['default'] = $this->ensureArray($expression);
409
        } else {
410 2
            $this->expr['$switch']['default'] = $this->ensureArray($expression);
411
        }
412
413 2
        return $this;
414
    }
415
416
    /**
417
     * Divides one number by another and returns the result. The first argument
418
     * is divided by the second argument.
419
     *
420
     * The arguments can be any valid expression as long as the resolve to numbers.
421
     *
422
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/divide/
423
     * @param mixed|self $expression1
424
     * @param mixed|self $expression2
425
     * @return $this
426
     */
427 3
    public function divide($expression1, $expression2)
428
    {
429 3
        return $this->operator('$divide', [$expression1, $expression2]);
430
    }
431
432
    /**
433
     * Compares two values and returns whether the are equivalent.
434
     *
435
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/eq/
436
     * @param mixed|self $expression1
437
     * @param mixed|self $expression2
438
     * @return $this
439
     */
440 9
    public function eq($expression1, $expression2)
441
    {
442 9
        return $this->operator('$eq', [$expression1, $expression2]);
443
    }
444
445
    /**
446
     * Raises Euler’s number to the specified exponent and returns the result.
447
     *
448
     * The <exponent> expression can be any valid expression as long as it
449
     * resolves to a number.
450
     *
451
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/exp/
452
     * @param mixed|self $exponent
453
     * @return $this
454
     *
455
     */
456 3
    public function exp($exponent)
457
    {
458 3
        return $this->operator('$exp', $exponent);
459
    }
460
461
    /**
462
     * Returns a new expression object
463
     *
464
     * @return static
465
     *
466
     */
467 4
    public function expr()
468
    {
469 4
        return new static($this->dm, $this->class);
470
    }
471
472
    /**
473
     * Allows any expression to be used as a field value.
474
     *
475
     * @see http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions
476
     * @param mixed|self $value
477
     * @return $this
478
     */
479 12
    public function expression($value)
480
    {
481 12
        $this->requiresCurrentField(__METHOD__);
482 11
        $this->expr[$this->currentField] = $this->ensureArray($value);
483
484 11
        return $this;
485
    }
486
487
    /**
488
     * Set the current field for building the expression.
489
     *
490
     * @param string $fieldName
491
     * @return $this
492
     */
493 139
    public function field($fieldName)
494
    {
495 139
        $fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName);
496 139
        $this->currentField = (string) $fieldName;
497
498 139
        return $this;
499
    }
500
501
    /**
502
     * Selects a subset of the array to return based on the specified condition.
503
     *
504
     * Returns an array with only those elements that match the condition. The
505
     * returned elements are in the original order.
506
     *
507
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/filter/
508
     * @param mixed|self $input
509
     * @param mixed|self $as
510
     * @param mixed|self $cond
511
     * @return $this
512
     *
513
     */
514 3
    public function filter($input, $as, $cond)
515
    {
516 3
        return $this->operator('$filter', ['input' => $input, 'as' => $as, 'cond' => $cond]);
517
    }
518
519
    /**
520
     * Returns the value that results from applying an expression to the first
521
     * document in a group of documents that share the same group by key. Only
522
     * meaningful when documents are in a defined order.
523
     *
524
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/first/
525
     * @param mixed|self $expression
526
     * @return $this
527
     */
528 2
    public function first($expression)
529
    {
530 2
        return $this->operator('$first', $expression);
531
    }
532
533
    /**
534
     * Returns the largest integer less than or equal to the specified number.
535
     *
536
     * The <number> expression can be any valid expression as long as it
537
     * resolves to a number.
538
     *
539
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/floor/
540
     * @param mixed|self $number
541
     * @return $this
542
     *
543
     */
544 3
    public function floor($number)
545
    {
546 3
        return $this->operator('$floor', $number);
547
    }
548
549
    /**
550
     * @return array
551
     */
552 339
    public function getExpression()
553
    {
554 339
        return $this->expr;
555
    }
556
557
    /**
558
     * Compares two values and returns:
559
     * true when the first value is greater than the second value.
560
     * false when the first value is less than or equivalent to the second
561
     * value.
562
     *
563
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/gt/
564
     * @param mixed|self $expression1
565
     * @param mixed|self $expression2
566
     * @return $this
567
     */
568 3
    public function gt($expression1, $expression2)
569
    {
570 3
        return $this->operator('$gt', [$expression1, $expression2]);
571
    }
572
573
    /**
574
     * Compares two values and returns:
575
     * true when the first value is greater than or equivalent to the second
576
     * value.
577
     * false when the first value is less than the second value.
578
     *
579
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/gte/
580
     * @param mixed|self $expression1
581
     * @param mixed|self $expression2
582
     * @return $this
583
     */
584 6
    public function gte($expression1, $expression2)
585
    {
586 6
        return $this->operator('$gte', [$expression1, $expression2]);
587
    }
588
589
    /**
590
     * Returns the hour portion of a date as a number between 0 and 23.
591
     *
592
     * The argument can be any expression as long as it resolves to a date.
593
     *
594
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/hour/
595
     * @param mixed|self $expression
596
     * @return $this
597
     */
598 3
    public function hour($expression)
599
    {
600 3
        return $this->operator('$hour', $expression);
601
    }
602
603
    /**
604
     * Evaluates an expression and returns the value of the expression if the
605
     * expression evaluates to a non-null value. If the expression evaluates to
606
     * a null value, including instances of undefined values or missing fields,
607
     * returns the value of the replacement expression.
608
     *
609
     * The arguments can be any valid expression.
610
     *
611
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/ifNull/
612
     * @param mixed|self $expression
613
     * @param mixed|self $replacementExpression
614
     * @return $this
615
     */
616 3
    public function ifNull($expression, $replacementExpression)
617
    {
618 3
        return $this->operator('$ifNull', [$expression, $replacementExpression]);
619
    }
620
621
    /**
622
     * Returns a boolean indicating whether a specified value is in an array.
623
     *
624
     * Unlike the $in query operator, the aggregation $in operator does not
625
     * support matching by regular expressions.
626
     *
627
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/in/
628
     * @param mixed|self $expression
629
     * @param mixed|self $arrayExpression
630
     * @return $this
631
     */
632 3
    public function in($expression, $arrayExpression)
633
    {
634 3
        return $this->operator('$in', [$expression, $arrayExpression]);
635
    }
636
637
    /**
638
     * Searches an array for an occurrence of a specified value and returns the
639
     * array index (zero-based) of the first occurrence. If the value is not
640
     * found, returns -1.
641
     *
642
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfArray/
643
     * @param mixed|self $arrayExpression  Can be any valid expression as long as it resolves to an array.
644
     * @param mixed|self $searchExpression Can be any valid expression.
645
     * @param mixed|self $start            Optional. An integer, or a number that can be represented as integers (such as 2.0), that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
646
     * @param mixed|self $end              An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
647
     * @return $this
648
     */
649 12 View Code Duplication
    public function indexOfArray($arrayExpression, $searchExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
650
    {
651 12
        $args = [$arrayExpression, $searchExpression];
652 12
        if ($start !== null) {
653 6
            $args[] = $start;
654
655 6
            if ($end !== null) {
656 3
                $args[] = $end;
657
            }
658
        }
659
660 12
        return $this->operator('$indexOfArray', $args);
661
    }
662
663
    /**
664
     * Searches a string for an occurrence of a substring and returns the UTF-8
665
     * byte index (zero-based) of the first occurrence. If the substring is not
666
     * found, returns -1.
667
     *
668
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfBytes/
669
     * @param mixed|self $stringExpression    Can be any valid expression as long as it resolves to a string.
670
     * @param mixed|self $substringExpression Can be any valid expression as long as it resolves to a string.
671
     * @param int|null   $start               An integral number that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
672
     * @param int|null   $end                 An integral number that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
673
     *
674
     * @return $this
675
     */
676 12 View Code Duplication
    public function indexOfBytes($stringExpression, $substringExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
677
    {
678 12
        $args = [$stringExpression, $substringExpression];
679 12
        if ($start !== null) {
680 6
            $args[] = $start;
681
682 6
            if ($end !== null) {
683 3
                $args[] = $end;
684
            }
685
        }
686
687 12
        return $this->operator('$indexOfBytes', $args);
688
    }
689
690
    /**
691
     * Searches a string for an occurrence of a substring and returns the UTF-8
692
     * code point index (zero-based) of the first occurrence. If the substring is
693
     * not found, returns -1.
694
     *
695
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfCP/
696
     * @param mixed|self $stringExpression    Can be any valid expression as long as it resolves to a string.
697
     * @param mixed|self $substringExpression Can be any valid expression as long as it resolves to a string.
698
     * @param int|null   $start               An integral number that specifies the starting index position for the search. Can be any valid expression that resolves to a non-negative integral number.
699
     * @param int|null   $end                 An integral number that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number.
700
     *
701
     * @return $this
702
     */
703 12 View Code Duplication
    public function indexOfCP($stringExpression, $substringExpression, $start = null, $end = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
704
    {
705 12
        $args = [$stringExpression, $substringExpression];
706 12
        if ($start !== null) {
707 6
            $args[] = $start;
708
709 6
            if ($end !== null) {
710 3
                $args[] = $end;
711
            }
712
        }
713
714 12
        return $this->operator('$indexOfCP', $args);
715
    }
716
717
    /**
718
     * Determines if the operand is an array. Returns a boolean.
719
     *
720
     * The <expression> can be any valid expression.
721
     *
722
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/isArray/
723
     * @param mixed|self $expression
724
     * @return $this
725
     *
726
     */
727 3
    public function isArray($expression)
728
    {
729 3
        return $this->operator('$isArray', $expression);
730
    }
731
732
    /**
733
     * Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday)
734
     * to 7 (for Sunday).
735
     *
736
     * The argument can be any expression as long as it resolves to a date.
737
     *
738
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoDayOfWeek/
739
     * @param mixed|self $expression
740
     * @return $this
741
     */
742 3
    public function isoDayOfWeek($expression)
743
    {
744 3
        return $this->operator('$isoDayOfWeek', $expression);
745
    }
746
747
    /**
748
     * Returns the week number in ISO 8601 format, ranging from 1 to 53.
749
     *
750
     * Week numbers start at 1 with the week (Monday through Sunday) that
751
     * contains the year’s first Thursday.
752
     *
753
     * The argument can be any expression as long as it resolves to a date.
754
     *
755
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoWeek/
756
     * @param mixed|self $expression
757
     * @return $this
758
     */
759 3
    public function isoWeek($expression)
760
    {
761 3
        return $this->operator('$isoWeek', $expression);
762
    }
763
764
    /**
765
     * Returns the year number in ISO 8601 format.
766
     *
767
     * The year starts with the Monday of week 1 (ISO 8601) and ends with the
768
     * Sunday of the last week (ISO 8601).
769
     *
770
     * The argument can be any expression as long as it resolves to a date.
771
     *
772
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/isoWeek/
773
     * @param mixed|self $expression
774
     * @return $this
775
     */
776 3
    public function isoWeekYear($expression)
777
    {
778 3
        return $this->operator('$isoWeekYear', $expression);
779
    }
780
781
    /**
782
     * Returns the value that results from applying an expression to the last
783
     * document in a group of documents that share the same group by a field.
784
     * Only meaningful when documents are in a defined order.
785
     *
786
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/last/
787
     * @param mixed|self $expression
788
     * @return $this
789
     */
790 2
    public function last($expression)
791
    {
792 2
        return $this->operator('$last', $expression);
793
    }
794
795
    /**
796
     * Binds variables for use in the specified expression, and returns the
797
     * result of the expression.
798
     *
799
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/let/
800
     * @param mixed|self $vars Assignment block for the variables accessible in the in expression. To assign a variable, specify a string for the variable name and assign a valid expression for the value.
801
     * @param mixed|self $in   The expression to evaluate.
802
     * @return $this
803
     */
804 3
    public function let($vars, $in)
805
    {
806 3
        return $this->operator('$let', ['vars' => $vars, 'in' => $in]);
807
    }
808
809
    /**
810
     * Returns a value without parsing. Use for values that the aggregation
811
     * pipeline may interpret as an expression.
812
     *
813
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/literal/
814
     * @param mixed|self $value
815
     * @return $this
816
     */
817 3
    public function literal($value)
818
    {
819 3
        return $this->operator('$literal', $value);
820
    }
821
822
    /**
823
     * Calculates the natural logarithm ln (i.e loge) of a number and returns
824
     * the result as a double.
825
     *
826
     * The <number> expression can be any valid expression as long as it
827
     * resolves to a non-negative number.
828
     *
829
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log/
830
     * @param mixed|self $number
831
     * @return $this
832
     *
833
     */
834 3
    public function ln($number)
835
    {
836 3
        return $this->operator('$ln', $number);
837
    }
838
839
    /**
840
     * Calculates the log of a number in the specified base and returns the
841
     * result as a double.
842
     *
843
     * The <number> expression can be any valid expression as long as it
844
     * resolves to a non-negative number.
845
     * The <base> expression can be any valid expression as long as it resolves
846
     * to a positive number greater than 1.
847
     *
848
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log/
849
     * @param mixed|self $number
850
     * @param mixed|self $base
851
     * @return $this
852
     *
853
     */
854 3
    public function log($number, $base)
855
    {
856 3
        return $this->operator('$log', [$number, $base]);
857
    }
858
859
    /**
860
     * Calculates the log base 10 of a number and returns the result as a double.
861
     *
862
     * The <number> expression can be any valid expression as long as it
863
     * resolves to a non-negative number.
864
     *
865
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/log10/
866
     * @param mixed|self $number
867
     * @return $this
868
     *
869
     */
870 3
    public function log10($number)
871
    {
872 3
        return $this->operator('$log10', $number);
873
    }
874
875
    /**
876
     * Compares two values and returns:
877
     * true when the first value is less than the second value.
878
     * false when the first value is greater than or equivalent to the second
879
     * value.
880
     *
881
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/lt/
882
     * @param mixed|self $expression1
883
     * @param mixed|self $expression2
884
     * @return $this
885
     */
886 4
    public function lt($expression1, $expression2)
887
    {
888 4
        return $this->operator('$lt', [$expression1, $expression2]);
889
    }
890
891
    /**
892
     * Compares two values and returns:
893
     * true when the first value is less than or equivalent to the second value.
894
     * false when the first value is greater than the second value.
895
     *
896
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/lte/
897
     * @param mixed|self $expression1
898
     * @param mixed|self $expression2
899
     * @return $this
900
     */
901 6
    public function lte($expression1, $expression2)
902
    {
903 6
        return $this->operator('$lte', [$expression1, $expression2]);
904
    }
905
906
    /**
907
     * Applies an expression to each item in an array and returns an array with
908
     * the applied results.
909
     *
910
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/map/
911
     * @param mixed|self $input An expression that resolves to an array.
912
     * @param string     $as    The variable name for the items in the input array. The in expression accesses each item in the input array by this variable.
913
     * @param mixed|self $in    The expression to apply to each item in the input array. The expression accesses the item by its variable name.
914
     * @return $this
915
     */
916 3
    public function map($input, $as, $in)
917
    {
918 3
        return $this->operator('$map', ['input' => $input, 'as' => $as, 'in' => $in]);
919
    }
920
921
    /**
922
     * Returns the highest value that results from applying an expression to
923
     * each document in a group of documents that share the same group by key.
924
     *
925
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/max/
926
     * @param mixed|self $expression
927
     * @return $this
928
     */
929 3
    public function max($expression)
930
    {
931 3
        return $this->operator('$max', $expression);
932
    }
933
934
    /**
935
     * Returns the metadata associated with a document in a pipeline operations.
936
     *
937
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/meta/
938
     * @param mixed|self $metaDataKeyword
939
     * @return $this
940
     */
941 3
    public function meta($metaDataKeyword)
942
    {
943 3
        return $this->operator('$meta', $metaDataKeyword);
944
    }
945
946
    /**
947
     * Returns the millisecond portion of a date as an integer between 0 and 999.
948
     *
949
     * The argument can be any expression as long as it resolves to a date.
950
     *
951
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/millisecond/
952
     * @param mixed|self $expression
953
     * @return $this
954
     */
955 3
    public function millisecond($expression)
956
    {
957 3
        return $this->operator('$millisecond', $expression);
958
    }
959
960
    /**
961
     * Returns the lowest value that results from applying an expression to each
962
     * document in a group of documents that share the same group by key.
963
     *
964
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/min/
965
     * @param mixed|self $expression
966
     * @return $this
967
     */
968 3
    public function min($expression)
969
    {
970 3
        return $this->operator('$min', $expression);
971
    }
972
973
    /**
974
     * Returns the minute portion of a date as a number between 0 and 59.
975
     *
976
     * The argument can be any expression as long as it resolves to a date.
977
     *
978
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/minute/
979
     * @param mixed|self $expression
980
     * @return $this
981
     */
982 3
    public function minute($expression)
983
    {
984 3
        return $this->operator('$minute', $expression);
985
    }
986
987
    /**
988
     * Divides one number by another and returns the remainder. The first
989
     * argument is divided by the second argument.
990
     *
991
     * The arguments can be any valid expression as long as they resolve to numbers.
992
     *
993
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/mod/
994
     * @param mixed|self $expression1
995
     * @param mixed|self $expression2
996
     * @return $this
997
     */
998 3
    public function mod($expression1, $expression2)
999
    {
1000 3
        return $this->operator('$mod', [$expression1, $expression2]);
1001
    }
1002
1003
    /**
1004
     * Returns the month of a date as a number between 1 and 12.
1005
     *
1006
     * The argument can be any expression as long as it resolves to a date.
1007
     *
1008
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/month/
1009
     * @param mixed|self $expression
1010
     * @return $this
1011
     */
1012 3
    public function month($expression)
1013
    {
1014 3
        return $this->operator('$month', $expression);
1015
    }
1016
1017
    /**
1018
     * Multiplies numbers together and returns the result.
1019
     *
1020
     * The arguments can be any valid expression as long as they resolve to numbers.
1021
     *
1022
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/multiply/
1023
     * @param mixed|self $expression1
1024
     * @param mixed|self $expression2
1025
     * @param mixed|self ...$expressions Additional expressions
1026
     * @return $this
1027
     */
1028 13
    public function multiply($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
1029
    {
1030 13
        return $this->operator('$multiply', func_get_args());
1031
    }
1032
1033
    /**
1034
     * Compares two values and returns:
1035
     * true when the values are not equivalent.
1036
     * false when the values are equivalent.
1037
     *
1038
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/ne/
1039
     * @param mixed|self $expression1
1040
     * @param mixed|self $expression2
1041
     * @return $this
1042
     */
1043 4
    public function ne($expression1, $expression2)
1044
    {
1045 4
        return $this->operator('$ne', [$expression1, $expression2]);
1046
    }
1047
1048
    /**
1049
     * Evaluates a boolean and returns the opposite boolean value.
1050
     *
1051
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/not/
1052
     * @param mixed|self $expression
1053
     * @return $this
1054
     */
1055 3
    public function not($expression)
1056
    {
1057 3
        return $this->operator('$not', $expression);
1058
    }
1059
1060
    /**
1061
     * Raises a number to the specified exponent and returns the result.
1062
     *
1063
     * The <number> expression can be any valid expression as long as it
1064
     * resolves to a non-negative number.
1065
     * The <exponent> expression can be any valid expression as long as it
1066
     * resolves to a number.
1067
     *
1068
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/pow/
1069
     * @param mixed|self $number
1070
     * @param mixed|self $exponent
1071
     * @return $this
1072
     *
1073
     */
1074 3
    public function pow($number, $exponent)
1075
    {
1076 3
        return $this->operator('$pow', [$number, $exponent]);
1077
    }
1078
1079
    /**
1080
     * Returns an array of all values that result from applying an expression to
1081
     * each document in a group of documents that share the same group by key.
1082
     *
1083
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/push/
1084
     * @param mixed|self $expression
1085
     * @return $this
1086
     */
1087 2
    public function push($expression)
1088
    {
1089 2
        return $this->operator('$push', $expression);
1090
    }
1091
1092
    /**
1093
     * Returns an array whose elements are a generated sequence of numbers.
1094
     *
1095
     * $range generates the sequence from the specified starting number by successively incrementing the starting number by the specified step value up to but not including the end point.
1096
     *
1097
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/range/
1098
     * @param mixed|self $start An integer that specifies the start of the sequence. Can be any valid expression that resolves to an integer.
1099
     * @param mixed|self $end   An integer that specifies the exclusive upper limit of the sequence. Can be any valid expression that resolves to an integer.
1100
     * @param mixed|self $step  Optional. An integer that specifies the increment value. Can be any valid expression that resolves to a non-zero integer. Defaults to 1.
1101
     * @return $this
1102
     */
1103 6
    public function range($start, $end, $step = 1)
1104
    {
1105 6
        return $this->operator('$range', [$start, $end, $step]);
1106
    }
1107
1108
    /**
1109
     * Applies an expression to each element in an array and combines them into
1110
     * a single value.
1111
     *
1112
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/reduce/
1113
     * @param mixed|self $input        Can be any valid expression that resolves to an array.
1114
     * @param mixed|self $initialValue The initial cumulative value set before in is applied to the first element of the input array.
1115
     * @param mixed|self $in           A valid expression that $reduce applies to each element in the input array in left-to-right order. Wrap the input value with $reverseArray to yield the equivalent of applying the combining expression from right-to-left.
1116
     * @return $this
1117
     */
1118 3
    public function reduce($input, $initialValue, $in)
1119
    {
1120 3
        return $this->operator('$reduce', ['input' => $input, 'initialValue' => $initialValue, 'in' => $in]);
1121
    }
1122
1123
    /**
1124
     * Accepts an array expression as an argument and returns an array with the
1125
     * elements in reverse order.
1126
     *
1127
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/reverseArray/
1128
     * @param mixed|self $expression
1129
     * @return $this
1130
     */
1131 3
    public function reverseArray($expression)
1132
    {
1133 3
        return $this->operator('$reverseArray', $expression);
1134
    }
1135
1136
    /**
1137
     * Returns the second portion of a date as a number between 0 and 59, but
1138
     * can be 60 to account for leap seconds.
1139
     *
1140
     * The argument can be any expression as long as it resolves to a date.
1141
     *
1142
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/second/
1143
     * @param mixed|self $expression
1144
     * @return $this
1145
     */
1146 3
    public function second($expression)
1147
    {
1148 3
        return $this->operator('$second', $expression);
1149
    }
1150
1151
    /**
1152
     * Takes two sets and returns an array containing the elements that only
1153
     * exist in the first set.
1154
     *
1155
     * The arguments can be any valid expression as long as they each resolve to an array.
1156
     *
1157
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setDifference/
1158
     * @param mixed|self $expression1
1159
     * @param mixed|self $expression2
1160
     * @return $this
1161
     */
1162 3
    public function setDifference($expression1, $expression2)
1163
    {
1164 3
        return $this->operator('$setDifference', [$expression1, $expression2]);
1165
    }
1166
1167
    /**
1168
     * Compares two or more arrays and returns true if they have the same
1169
     * distinct elements and false otherwise.
1170
     *
1171
     * The arguments can be any valid expression as long as they each resolve to an array.
1172
     *
1173
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setEquals/
1174
     * @param mixed|self $expression1
1175
     * @param mixed|self $expression2
1176
     * @param mixed|self ...$expressions Additional sets
1177
     * @return $this
1178
     */
1179 6
    public function setEquals($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
1180
    {
1181 6
        return $this->operator('$setEquals', func_get_args());
1182
    }
1183
1184
    /**
1185
     * Takes two or more arrays and returns an array that contains the elements
1186
     * that appear in every input array.
1187
     *
1188
     * The arguments can be any valid expression as long as they each resolve to an array.
1189
     *
1190
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setIntersection/
1191
     * @param mixed|self $expression1
1192
     * @param mixed|self $expression2
1193
     * @param mixed|self ...$expressions Additional sets
1194
     * @return $this
1195
     */
1196 6
    public function setIntersection($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
1197
    {
1198 6
        return $this->operator('$setIntersection', func_get_args());
1199
    }
1200
1201
    /**
1202
     * Takes two arrays and returns true when the first array is a subset of the
1203
     * second, including when the first array equals the second array, and false otherwise.
1204
     *
1205
     * The arguments can be any valid expression as long as they each resolve to an array.
1206
     *
1207
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/
1208
     * @param mixed|self $expression1
1209
     * @param mixed|self $expression2
1210
     * @return $this
1211
     */
1212 3
    public function setIsSubset($expression1, $expression2)
1213
    {
1214 3
        return $this->operator('$setIsSubset', [$expression1, $expression2]);
1215
    }
1216
1217
    /**
1218
     * Takes two or more arrays and returns an array containing the elements
1219
     * that appear in any input array.
1220
     *
1221
     * The arguments can be any valid expression as long as they each resolve to an array.
1222
     *
1223
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/setUnion/
1224
     * @param mixed|self $expression1
1225
     * @param mixed|self $expression2
1226
     * @param mixed|self ...$expressions Additional sets
1227
     * @return $this
1228
     */
1229 6
    public function setUnion($expression1, $expression2, ...$expressions)
0 ignored issues
show
Unused Code introduced by
The parameter $expression1 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 $expression2 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...
1230
    {
1231 6
        return $this->operator('$setUnion', func_get_args());
1232
    }
1233
1234
    /**
1235
     * Counts and returns the total the number of items in an array.
1236
     *
1237
     * The argument can be any expression as long as it resolves to an array.
1238
     *
1239
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/size/
1240
     * @param mixed|self $expression
1241
     * @return $this
1242
     */
1243 3
    public function size($expression)
1244
    {
1245 3
        return $this->operator('$size', $expression);
1246
    }
1247
1248
    /**
1249
     * Returns a subset of an array.
1250
     *
1251
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/slice/
1252
     * @param mixed|self      $array
1253
     * @param mixed|self      $n
1254
     * @param mixed|self|null $position
1255
     * @return $this
1256
     *
1257
     */
1258 6
    public function slice($array, $n, $position = null)
1259
    {
1260 6
        if ($position === null) {
1261 3
            return $this->operator('$slice', [$array, $n]);
1262
        }
1263
1264 3
        return $this->operator('$slice', [$array, $position, $n]);
1265
    }
1266
1267
    /**
1268
     * Divides a string into an array of substrings based on a delimiter.
1269
     *
1270
     * $split removes the delimiter and returns the resulting substrings as
1271
     * elements of an array. If the delimiter is not found in the string, $split
1272
     * returns the original string as the only element of an array.
1273
     *
1274
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/split/
1275
     * @param mixed|self $string    The string to be split. Can be any valid expression as long as it resolves to a string.
1276
     * @param mixed|self $delimiter The delimiter to use when splitting the string expression. Can be any valid expression as long as it resolves to a string.
1277
     *
1278
     * @return $this
1279
     */
1280 3
    public function split($string, $delimiter)
1281
    {
1282 3
        return $this->operator('$split', [$string, $delimiter]);
1283
    }
1284
1285
    /**
1286
     * Calculates the square root of a positive number and returns the result as
1287
     * a double.
1288
     *
1289
     * The argument can be any valid expression as long as it resolves to a
1290
     * non-negative number.
1291
     *
1292
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/sqrt/
1293
     * @param mixed|self $expression
1294
     * @return $this
1295
     */
1296 3
    public function sqrt($expression)
1297
    {
1298 3
        return $this->operator('$sqrt', $expression);
1299
    }
1300
1301
    /**
1302
     * Calculates the population standard deviation of the input values.
1303
     *
1304
     * The arguments can be any expression as long as it resolves to an array.
1305
     *
1306
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevPop/
1307
     * @param mixed|self $expression1
1308
     * @param mixed|self ...$expressions Additional samples
1309
     * @return $this
1310
     *
1311
     */
1312 5
    public function stdDevPop($expression1, ...$expressions)
1313
    {
1314 5
        $expression = (empty($expressions)) ? $expression1 : func_get_args();
1315
1316 5
        return $this->operator('$stdDevPop', $expression);
1317
    }
1318
1319
    /**
1320
     * Calculates the sample standard deviation of the input values.
1321
     *
1322
     * The arguments can be any expression as long as it resolves to an array.
1323
     *
1324
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/stdDevSamp/
1325
     * @param mixed|self $expression1
1326
     * @param mixed|self ...$expressions Additional samples
1327
     * @return $this
1328
     *
1329
     */
1330 5
    public function stdDevSamp($expression1, ...$expressions)
1331
    {
1332 5
        $expression = (empty($expressions)) ? $expression1 : func_get_args();
1333
1334 5
        return $this->operator('$stdDevSamp', $expression);
1335
    }
1336
1337
    /**
1338
     * Performs case-insensitive comparison of two strings. Returns
1339
     * 1 if first string is “greater than” the second string.
1340
     * 0 if the two strings are equal.
1341
     * -1 if the first string is “less than” the second string.
1342
     *
1343
     * The arguments can be any valid expression as long as they resolve to strings.
1344
     *
1345
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/strcasecmp/
1346
     * @param mixed|self $expression1
1347
     * @param mixed|self $expression2
1348
     * @return $this
1349
     */
1350 3
    public function strcasecmp($expression1, $expression2)
1351
    {
1352 3
        return $this->operator('$strcasecmp', [$expression1, $expression2]);
1353
    }
1354
1355
    /**
1356
     * Returns the number of UTF-8 encoded bytes in the specified string.
1357
     *
1358
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/strLenBytes/
1359
     * @param mixed|self $string
1360
     *
1361
     * @return $this
1362
     */
1363 3
    public function strLenBytes($string)
1364
    {
1365 3
        return $this->operator('$strLenBytes', $string);
1366
    }
1367
1368
    /**
1369
     * Returns the number of UTF-8 code points in the specified string.
1370
     *
1371
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/strLenCP/
1372
     * @param mixed|self $string
1373
     *
1374
     * @return $this
1375
     */
1376 3
    public function strLenCP($string)
1377
    {
1378 3
        return $this->operator('$strLenCP', $string);
1379
    }
1380
1381
    /**
1382
     * Returns a substring of a string, starting at a specified index position
1383
     * and including the specified number of characters. The index is zero-based.
1384
     *
1385
     * The arguments can be any valid expression as long as long as the first argument resolves to a string, and the second and third arguments resolve to integers.
1386
     *
1387
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/substr/
1388
     * @param mixed|self $string
1389
     * @param mixed|self $start
1390
     * @param mixed|self $length
1391
     * @return $this
1392
     */
1393 3
    public function substr($string, $start, $length)
1394
    {
1395 3
        return $this->operator('$substr', [$string, $start, $length]);
1396
    }
1397
1398
    /**
1399
     * Returns the substring of a string.
1400
     *
1401
     * The substring starts with the character at the specified UTF-8 byte index
1402
     * (zero-based) in the string and continues for the number of bytes
1403
     * specified.
1404
     *
1405
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/substrBytes/
1406
     * @param mixed|self $string The string from which the substring will be extracted. Can be any valid expression as long as it resolves to a string.
1407
     * @param mixed|self $start  Indicates the starting point of the substring. Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1408
     * @param mixed|self $count  Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1409
     *
1410
     * @return $this
1411
     */
1412 3
    public function substrBytes($string, $start, $count)
1413
    {
1414 3
        return $this->operator('$substrBytes', [$string, $start, $count]);
1415
    }
1416
1417
    /**
1418
     * Returns the substring of a string.
1419
     *
1420
     * The substring starts with the character at the specified UTF-8 code point
1421
     * (CP) index (zero-based) in the string for the number of code points
1422
     * specified.
1423
     *
1424
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/substrBytes/
1425
     * @param mixed|self $string The string from which the substring will be extracted. Can be any valid expression as long as it resolves to a string.
1426
     * @param mixed|self $start  Indicates the starting point of the substring. Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1427
     * @param mixed|self $count  Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer.
1428
     *
1429
     * @return $this
1430
     */
1431 3
    public function substrCP($string, $start, $count)
1432
    {
1433 3
        return $this->operator('$substrCP', [$string, $start, $count]);
1434
    }
1435
1436
    /**
1437
     * Subtracts two numbers to return the difference. The second argument is
1438
     * subtracted from the first argument.
1439
     *
1440
     * The arguments can be any valid expression as long as they resolve to numbers and/or dates.
1441
     *
1442
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/subtract/
1443
     * @param mixed|self $expression1
1444
     * @param mixed|self $expression2
1445
     * @return $this
1446
     */
1447 3
    public function subtract($expression1, $expression2)
1448
    {
1449 3
        return $this->operator('$subtract', [$expression1, $expression2]);
1450
    }
1451
1452
    /**
1453
     * Calculates and returns the sum of all the numeric values that result from
1454
     * applying a specified expression to each document in a group of documents
1455
     * that share the same group by key. Ignores nun-numeric values.
1456
     *
1457
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sum/
1458
     * @param mixed|self $expression
1459
     * @return $this
1460
     */
1461 9
    public function sum($expression)
1462
    {
1463 9
        return $this->operator('$sum', $expression);
1464
    }
1465
1466
    /**
1467
     * Converts a string to lowercase, returning the result.
1468
     *
1469
     * The argument can be any expression as long as it resolves to a string.
1470
     *
1471
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/toLower/
1472
     * @param mixed|self $expression
1473
     * @return $this
1474
     */
1475 3
    public function toLower($expression)
1476
    {
1477 3
        return $this->operator('$toLower', $expression);
1478
    }
1479
1480
    /**
1481
     * Converts a string to uppercase, returning the result.
1482
     *
1483
     * The argument can be any expression as long as it resolves to a string.
1484
     *
1485
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/toUpper/
1486
     * @param mixed|self $expression
1487
     * @return $this
1488
     */
1489 3
    public function toUpper($expression)
1490
    {
1491 3
        return $this->operator('$toUpper', $expression);
1492
    }
1493
1494
    /**
1495
     * Truncates a number to its integer.
1496
     *
1497
     * The <number> expression can be any valid expression as long as it
1498
     * resolves to a number.
1499
     *
1500
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/trunc/
1501
     * @param mixed|self $number
1502
     * @return $this
1503
     *
1504
     */
1505 3
    public function trunc($number)
1506
    {
1507 3
        return $this->operator('$trunc', $number);
1508
    }
1509
1510
    /**
1511
     * Returns a string that specifies the BSON type of the argument.
1512
     *
1513
     * The argument can be any valid expression.
1514
     *
1515
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/type/
1516
     * @param mixed|self $expression
1517
     *
1518
     * @return $this
1519
     */
1520 3
    public function type($expression)
1521
    {
1522 3
        return $this->operator('$type', $expression);
1523
    }
1524
1525
    /**
1526
     * Returns the week of the year for a date as a number between 0 and 53.
1527
     *
1528
     * The argument can be any expression as long as it resolves to a date.
1529
     *
1530
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/week/
1531
     * @param mixed|self $expression
1532
     * @return $this
1533
     */
1534 3
    public function week($expression)
1535
    {
1536 3
        return $this->operator('$week', $expression);
1537
    }
1538
1539
    /**
1540
     * Returns the year portion of a date.
1541
     *
1542
     * The argument can be any expression as long as it resolves to a date.
1543
     *
1544
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/year/
1545
     * @param mixed|self $expression
1546
     * @return $this
1547
     */
1548 4
    public function year($expression)
1549
    {
1550 4
        return $this->operator('$year', $expression);
1551
    }
1552
1553
    /**
1554
     * Transposes an array of input arrays so that the first element of the
1555
     * output array would be an array containing, the first element of the first
1556
     * input array, the first element of the second input array, etc.
1557
     *
1558
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/zip/
1559
     * @param mixed|self      $inputs           An array of expressions that resolve to arrays. The elements of these input arrays combine to form the arrays of the output array.
1560
     * @param bool|null       $useLongestLength A boolean which specifies whether the length of the longest array determines the number of arrays in the output array.
1561
     * @param mixed|self|null $defaults         An array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error.
1562
     * @return $this
1563
     */
1564 9
    public function zip($inputs, $useLongestLength = null, $defaults = null)
1565
    {
1566 9
        $args = ['inputs' => $inputs];
1567 9
        if ($useLongestLength !== null) {
1568 6
            $args['useLongestLength'] = $useLongestLength;
1569
        }
1570 9
        if ($defaults !== null) {
1571 3
            $args['defaults'] = $defaults;
1572
        }
1573
1574 9
        return $this->operator('$zip', $args);
1575
    }
1576
1577
1578
    /**
1579
     * @param mixed|self $expression
1580
     * @return mixed
1581
     */
1582 343
    private function ensureArray($expression)
1583
    {
1584 343
        if (is_string($expression) && substr($expression, 0, 1) === '$') {
1585 341
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
1586 217
        } elseif (is_array($expression)) {
1587 209
            return array_map([$this, 'ensureArray'], $expression);
1588 87
        } elseif ($expression instanceof self) {
1589 20
            return $expression->getExpression();
1590
        }
1591
1592
        // Convert PHP types to MongoDB types for everything else
1593 85
        return Type::convertPHPToDatabaseValue($expression);
1594
    }
1595
1596
    /**
1597
     * @return DocumentPersister
1598
     */
1599 343
    private function getDocumentPersister()
1600
    {
1601 343
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
1602
    }
1603
1604
    /**
1605
     * Defines an operator and value on the expression.
1606
     *
1607
     * If there is a current field, the operator will be set on it; otherwise,
1608
     * the operator is set at the top level of the query.
1609
     *
1610
     * @param string            $operator
1611
     * @param array|self[]|self $expression
1612
     * @return $this
1613
     */
1614 341
    private function operator($operator, $expression)
1615
    {
1616 341
        if ($this->currentField) {
1617 137
            $this->expr[$this->currentField][$operator] = $this->ensureArray($expression);
1618
        } else {
1619 212
            $this->expr[$operator] = $this->ensureArray($expression);
1620
        }
1621
1622 341
        return $this;
1623
    }
1624
1625
    /**
1626
     * Ensure that a current field has been set.
1627
     *
1628
     * @param string $method
1629
     *
1630
     * @throws \LogicException If a current field has not been set.
1631
     */
1632 12
    private function requiresCurrentField($method = null)
1633
    {
1634 12
        if (! $this->currentField) {
1635 1
            throw new \LogicException(($method ?: 'This method') . ' requires you set a current field using field().');
1636
        }
1637 11
    }
1638
1639
    /**
1640
     * @param string $method
1641
     *
1642
     * @throws \BadMethodCallException If there is no current switch operator.
1643
     */
1644 8
    private function requiresSwitchStatement($method = null)
1645
    {
1646 8
        $message = ($method ?: 'This method') . ' requires a valid switch statement (call switch() first).';
1647
1648 8
        if ($this->currentField) {
1649
            if (! isset($this->expr[$this->currentField]['$switch'])) {
1650
                throw new \BadMethodCallException($message);
1651
            }
1652 8
        } elseif (! isset($this->expr['$switch'])) {
1653 4
            throw new \BadMethodCallException($message);
1654
        }
1655 4
    }
1656
1657
    /**
1658
     * Evaluates a series of case expressions. When it finds an expression which
1659
     * evaluates to true, $switch executes a specified expression and breaks out
1660
     * of the control flow.
1661
     *
1662
     * To add statements, use the {@link case()}, {@link then()} and
1663
     * {@link default()} methods.
1664
     *
1665
     * @return $this
1666
     */
1667 4
    public function switch()
1668
    {
1669 4
        $this->operator('$switch', []);
1670
1671 4
        return $this;
1672
    }
1673
1674
    /**
1675
     * Adds a case statement for the current branch of the $switch operator.
1676
     *
1677
     * Requires {@link case()} to be called first. The argument can be any valid
1678
     * expression.
1679
     *
1680
     * @param mixed|self $expression
1681
     *
1682
     * @return $this
1683
     */
1684 6
    public function then($expression)
1685
    {
1686 6
        if (! is_array($this->switchBranch)) {
1687 4
            throw new \BadMethodCallException(static::class . '::then requires a valid case statement (call case() first).');
1688
        }
1689
1690 4
        $this->switchBranch['then'] = $expression;
1691
1692 4
        if ($this->currentField) {
1693
            $this->expr[$this->currentField]['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
1694
        } else {
1695 4
            $this->expr['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
1696
        }
1697
1698 4
        $this->switchBranch = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $switchBranch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1699
1700 4
        return $this;
1701
    }
1702
}
1703