Skip to content

Commit 5b6a368

Browse files
author
Vincent Le Henaff
committed
+ implement $expr operator as exprOp method for query and aggregation builders
+ add createExpr and depreciation notice for Builder::expr for further revision of exprOp method naming to expr
1 parent 4cc90c3 commit 5b6a368

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public function count(string $fieldName): Stage\Count
167167
return $stage;
168168
}
169169

170+
/**
171+
* Create a new Expr instance that can be used as an expression with the Builder
172+
*/
173+
public function createExpr(): Expr
174+
{
175+
return new Expr($this->dm, $this->class);
176+
}
177+
170178
/**
171179
* Executes the aggregation pipeline
172180
*
@@ -185,9 +193,14 @@ public function execute(array $options = []): Iterator
185193
return $this->getAggregation($options)->getIterator();
186194
}
187195

196+
/**
197+
* @return Expr
198+
*
199+
* @deprecated use createExpr instead
200+
*/
188201
public function expr(): Expr
189202
{
190-
return new Expr($this->dm, $this->class);
203+
return $this->createExpr();
191204
}
192205

193206
/**

lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ public static function convertExpression($expression)
330330
return $expression;
331331
}
332332

333+
/**
334+
* Returns a new expression object
335+
*/
336+
public function createExpr(): self
337+
{
338+
return new static($this->dm, $this->class);
339+
}
340+
333341
/**
334342
* Converts a date object to a string according to a user-specified format.
335343
*
@@ -459,10 +467,25 @@ public function exp($exponent): self
459467

460468
/**
461469
* Returns a new expression object
470+
*
471+
* @deprecated use createExpr instead
462472
*/
463473
public function expr(): self
464474
{
465-
return new static($this->dm, $this->class);
475+
return $this->createExpr();
476+
}
477+
478+
/**
479+
* Specify $expr criteria for the current field.
480+
*
481+
* @see Builder::expr()
482+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
483+
*
484+
* @param array|\Doctrine\ODM\MongoDB\Query\Expr $expression
485+
*/
486+
public function exprOp($expression): self
487+
{
488+
return $this->operator('$expr', $expression);
466489
}
467490

468491
/**

lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ public function all(array $values): self
112112
return $this;
113113
}
114114

115+
/**
116+
* Specify $expr criteria for the current field.
117+
*
118+
* You can create a new expression using the {@link Builder::matchExpr()}
119+
* method.
120+
*
121+
* @see Expr::expr()
122+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
123+
*
124+
* @param array|Expr $expression
125+
*/
126+
public function createExpr($expression): self
127+
{
128+
$this->query->expr($expression);
129+
130+
return $this;
131+
}
132+
115133
/**
116134
* Specify $elemMatch criteria for the current field.
117135
*
@@ -164,12 +182,34 @@ public function exists(bool $bool): self
164182
}
165183

166184
/**
167-
* Create a new Expr instance that can be used to build partial expressions
168-
* for other operator methods.
185+
* Specify $expr criteria for the current field.
186+
*
187+
* You can create a new expression using the {@link Builder::matchExpr()}
188+
* method.
189+
*
190+
* @see Expr::expr()
191+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
192+
*
193+
* @param array|Expr $expression
194+
*
195+
* @deprecated use createExpr instead
196+
*/
197+
public function expr($expression): self
198+
{
199+
return $this->createExpr($expression);
200+
}
201+
202+
/**
203+
* Specify $expr criteria for the current field.
204+
*
205+
* @see Expr::exprOp()
206+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
169207
*/
170-
public function expr(): Expr
208+
public function exprOp($expression): self
171209
{
172-
return $this->builder->matchExpr();
210+
$this->query->exprOp($expression);
211+
212+
return $this;
173213
}
174214

175215
/**

lib/Doctrine/ODM/MongoDB/Query/Builder.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,17 @@ public function count(): self
366366
return $this;
367367
}
368368

369+
/**
370+
* Create a new Expr instance that can be used as an expression with the Builder
371+
*/
372+
public function createExpr(): Expr
373+
{
374+
$expr = new Expr($this->dm);
375+
$expr->setClassMetadata($this->class);
376+
377+
return $expr;
378+
}
379+
369380
/**
370381
* Sets the value of the current field to the current date, either as a date or a timestamp.
371382
*
@@ -493,13 +504,25 @@ public function exists(bool $bool): self
493504

494505
/**
495506
* Create a new Expr instance that can be used as an expression with the Builder
507+
*
508+
* @deprecated use createExpr instead
496509
*/
497510
public function expr(): Expr
498511
{
499-
$expr = new Expr($this->dm);
500-
$expr->setClassMetadata($this->class);
512+
return $this->createExpr();
513+
}
501514

502-
return $expr;
515+
/**
516+
* Specify $expr criteria for the current field.
517+
*
518+
* @see Expr::exprOp()
519+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
520+
*/
521+
public function exprOp($expression): self
522+
{
523+
$this->expr->exprOp($expression);
524+
525+
return $this;
503526
}
504527

505528
/**

lib/Doctrine/ODM/MongoDB/Query/Expr.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ public function elemMatch($expression): self
404404
return $this->operator('$elemMatch', $expression);
405405
}
406406

407+
/**
408+
* Specify $expr criteria for the current field.
409+
*
410+
* @see Builder::exprOp()
411+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
412+
*
413+
* @param array|Expr $expression
414+
*/
415+
public function exprOp($expression): self
416+
{
417+
return $this->operator('$expr', $expression);
418+
}
419+
407420
/**
408421
* Specify an equality match for the current field.
409422
*

0 commit comments

Comments
 (0)