Skip to content

Commit 0b458de

Browse files
Vincent Le Henaffvincent-le-henaff
authored andcommitted
+ 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 5987555 commit 0b458de

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
@@ -160,6 +160,14 @@ public function count(string $fieldName): Stage\Count
160160
return $stage;
161161
}
162162

163+
/**
164+
* Create a new Expr instance that can be used as an expression with the Builder
165+
*/
166+
public function createExpr(): Expr
167+
{
168+
return new Expr($this->dm, $this->class);
169+
}
170+
163171
/**
164172
* Executes the aggregation pipeline
165173
*
@@ -180,9 +188,14 @@ public function execute(array $options = []): Iterator
180188
return $this->getAggregation($options)->getIterator();
181189
}
182190

191+
/**
192+
* @return Expr
193+
*
194+
* @deprecated use createExpr instead
195+
*/
183196
public function expr(): Expr
184197
{
185-
return new Expr($this->dm, $this->class);
198+
return $this->createExpr();
186199
}
187200

188201
/**

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ public static function convertExpression($expression)
324324
return $expression;
325325
}
326326

327+
/**
328+
* Returns a new expression object
329+
*/
330+
public function createExpr(): self
331+
{
332+
return new static($this->dm, $this->class);
333+
}
334+
327335
/**
328336
* Converts a date object to a string according to a user-specified format.
329337
*
@@ -453,10 +461,25 @@ public function exp($exponent): self
453461

454462
/**
455463
* Returns a new expression object
464+
*
465+
* @deprecated use createExpr instead
456466
*/
457467
public function expr(): self
458468
{
459-
return new static($this->dm, $this->class);
469+
return $this->createExpr();
470+
}
471+
472+
/**
473+
* Specify $expr criteria for the current field.
474+
*
475+
* @see Builder::expr()
476+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
477+
*
478+
* @param array|\Doctrine\ODM\MongoDB\Query\Expr $expression
479+
*/
480+
public function exprOp($expression): self
481+
{
482+
return $this->operator('$expr', $expression);
460483
}
461484

462485
/**

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

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

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

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

177217
/**

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ public function count(): self
356356
return $this;
357357
}
358358

359+
/**
360+
* Create a new Expr instance that can be used as an expression with the Builder
361+
*/
362+
public function createExpr(): Expr
363+
{
364+
$expr = new Expr($this->dm);
365+
$expr->setClassMetadata($this->class);
366+
367+
return $expr;
368+
}
369+
359370
/**
360371
* Sets the value of the current field to the current date, either as a date or a timestamp.
361372
*
@@ -483,13 +494,25 @@ public function exists(bool $bool): self
483494

484495
/**
485496
* Create a new Expr instance that can be used as an expression with the Builder
497+
*
498+
* @deprecated use createExpr instead
486499
*/
487500
public function expr(): Expr
488501
{
489-
$expr = new Expr($this->dm);
490-
$expr->setClassMetadata($this->class);
502+
return $this->createExpr();
503+
}
491504

492-
return $expr;
505+
/**
506+
* Specify $expr criteria for the current field.
507+
*
508+
* @see Expr::exprOp()
509+
* @see https://docs.mongodb.com/manual/reference/operator/query/expr/
510+
*/
511+
public function exprOp($expression): self
512+
{
513+
$this->expr->exprOp($expression);
514+
515+
return $this;
493516
}
494517

495518
/**

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)