Skip to content
This repository was archived by the owner on Nov 11, 2020. It is now read-only.

Commit 1c22128

Browse files
authored
Merge pull request #279 from alcaeus/variadic-logical-operators
Allow passing multiple arguments to add* methods
2 parents 15275b2 + 3ec08fe commit 1c22128

File tree

5 files changed

+74
-45
lines changed

5 files changed

+74
-45
lines changed

lib/Doctrine/MongoDB/Aggregation/Expr.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,38 @@ public function add($expression1, $expression2 /* , $expression3, ... */)
7878
}
7979

8080
/**
81-
* Add an $and clause to the current expression.
81+
* Adds one or more $and clauses to the current expression.
8282
*
8383
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/and/
8484
* @param array|self $expression
8585
* @return $this
8686
*/
87-
public function addAnd($expression)
87+
public function addAnd($expression /*, $expression2, ... */)
8888
{
89-
if ($this->currentField) {
90-
$this->expr[$this->currentField]['$and'][] = $this->ensureArray($expression);
91-
} else {
92-
$this->expr['$and'][] = $this->ensureArray($expression);
89+
if (! isset($this->expr['$and'])) {
90+
$this->expr['$and'] = [];
9391
}
9492

93+
$this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'ensureArray'], func_get_args()));
94+
9595
return $this;
9696
}
9797

9898
/**
99-
* Add an $or clause to the current expression.
99+
* Adds one or more $or clause to the current expression.
100100
*
101101
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/or/
102102
* @param array|self $expression
103103
* @return $this
104104
*/
105-
public function addOr($expression)
105+
public function addOr($expression /*, $expression2, ... */)
106106
{
107-
if ($this->currentField) {
108-
$this->expr[$this->currentField]['$or'][] = $this->ensureArray($expression);
109-
} else {
110-
$this->expr['$or'][] = $this->ensureArray($expression);
107+
if (! isset($this->expr['$or'])) {
108+
$this->expr['$or'] = [];
111109
}
112110

111+
$this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'ensureArray'], func_get_args()));
112+
113113
return $this;
114114
}
115115

lib/Doctrine/MongoDB/Aggregation/Stage/Match.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getExpression()
5858
}
5959

6060
/**
61-
* Add an $and clause to the current query.
61+
* Add one or more $and clauses to the current query.
6262
*
6363
* You can create a new expression using the {@link Builder::matchExpr()}
6464
* method.
@@ -68,15 +68,15 @@ public function getExpression()
6868
* @param array|Expr $expression
6969
* @return $this
7070
*/
71-
public function addAnd($expression)
71+
public function addAnd($expression /* , $expression2, ... */)
7272
{
73-
$this->query->addAnd($expression);
73+
$this->query->addAnd(...func_get_args());
7474

7575
return $this;
7676
}
7777

7878
/**
79-
* Add a $nor clause to the current query.
79+
* Add one or more $nor clauses to the current query.
8080
*
8181
* You can create a new expression using the {@link Builder::matchExpr()}
8282
* method.
@@ -86,15 +86,15 @@ public function addAnd($expression)
8686
* @param array|Expr $expression
8787
* @return $this
8888
*/
89-
public function addNor($expression)
89+
public function addNor($expression /*, $expression2, ... */)
9090
{
91-
$this->query->addNor($expression);
91+
$this->query->addNor(...func_get_args());
9292

9393
return $this;
9494
}
9595

9696
/**
97-
* Add an $or clause to the current query.
97+
* Add one or more $or clauses to the current query.
9898
*
9999
* You can create a new expression using the {@link Builder::matchExpr()}
100100
* method.
@@ -104,9 +104,9 @@ public function addNor($expression)
104104
* @param array|Expr $expression
105105
* @return $this
106106
*/
107-
public function addOr($expression)
107+
public function addOr($expression /* , $expression2, ... */)
108108
{
109-
$this->query->addOr($expression);
109+
$this->query->addOr(...func_get_args());
110110

111111
return $this;
112112
}

lib/Doctrine/MongoDB/Aggregation/Stage/Operator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ public function add($expression1, $expression2 /* , $expression3, ... */)
8888
}
8989

9090
/**
91-
* Add an $and clause to the current expression.
91+
* Add one or more $and clauses to the current expression.
9292
*
9393
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/and/
9494
* @see Expr::addAnd
9595
* @param array|Expr $expression
9696
* @return $this
9797
*/
98-
public function addAnd($expression)
98+
public function addAnd($expression /* , $expression2, ... */)
9999
{
100-
$this->expr->addAnd($expression);
100+
$this->expr->addAnd(...func_get_args());
101101

102102
return $this;
103103
}
104104

105105
/**
106-
* Add an $or clause to the current expression.
106+
* Add one or more $or clauses to the current expression.
107107
*
108108
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/or/
109109
* @see Expr::addOr
110110
* @param array|Expr $expression
111111
* @return $this
112112
*/
113-
public function addOr($expression)
113+
public function addOr($expression /* , $expression2, ... */)
114114
{
115-
$this->expr->addOr($expression);
115+
$this->expr->addOr(...func_get_args());
116116

117117
return $this;
118118
}

lib/Doctrine/MongoDB/Query/Builder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct(Collection $collection)
6868
}
6969

7070
/**
71-
* Add an $and clause to the current query.
71+
* Add one or more $and clauses to the current query.
7272
*
7373
* You can create a new expression using the {@link Builder::expr()} method.
7474
*
@@ -77,9 +77,9 @@ public function __construct(Collection $collection)
7777
* @param array|Expr $expression
7878
* @return $this
7979
*/
80-
public function addAnd($expression)
80+
public function addAnd($expression /* , $expression2, ... */)
8181
{
82-
$this->expr->addAnd($expression);
82+
$this->expr->addAnd(...func_get_args());
8383
return $this;
8484
}
8585

@@ -105,7 +105,7 @@ public function addManyToSet(array $values)
105105
}
106106

107107
/**
108-
* Add a $nor clause to the current query.
108+
* Add one or more $nor clauses to the current query.
109109
*
110110
* You can create a new expression using the {@link Builder::expr()} method.
111111
*
@@ -114,14 +114,14 @@ public function addManyToSet(array $values)
114114
* @param array|Expr $expression
115115
* @return $this
116116
*/
117-
public function addNor($expression)
117+
public function addNor($expression /* , $expression2, ... */)
118118
{
119-
$this->expr->addNor($expression);
119+
$this->expr->addNor(...func_get_args());
120120
return $this;
121121
}
122122

123123
/**
124-
* Add an $or clause to the current query.
124+
* Add one or more $or clauses to the current query.
125125
*
126126
* You can create a new expression using the {@link Builder::expr()} method.
127127
*
@@ -130,9 +130,9 @@ public function addNor($expression)
130130
* @param array|Expr $expression
131131
* @return $this
132132
*/
133-
public function addOr($expression)
133+
public function addOr($expression /* , $expression2, ... */)
134134
{
135-
$this->expr->addOr($expression);
135+
$this->expr->addOr(...func_get_args());
136136
return $this;
137137
}
138138

lib/Doctrine/MongoDB/Query/Expr.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,29 @@ class Expr
5757
protected $currentField;
5858

5959
/**
60-
* Add an $and clause to the current query.
60+
* Add one or more $and clauses to the current query.
6161
*
6262
* @see Builder::addAnd()
6363
* @see http://docs.mongodb.org/manual/reference/operator/and/
6464
* @param array|Expr $expression
6565
* @return $this
6666
*/
67-
public function addAnd($expression)
67+
public function addAnd($expression /*, $expression2, ... */)
6868
{
69-
$this->query['$and'][] = $expression instanceof Expr ? $expression->getQuery() : $expression;
69+
if (! isset($this->query['$and'])) {
70+
$this->query['$and'] = [];
71+
}
72+
73+
$this->query['$and'] = array_merge(
74+
$this->query['$and'],
75+
array_map(
76+
function ($expression) {
77+
return $expression instanceof Expr ? $expression->getQuery() : $expression;
78+
},
79+
func_get_args()
80+
)
81+
);
82+
7083
return $this;
7184
}
7285

@@ -93,30 +106,46 @@ public function addManyToSet(array $values)
93106
}
94107

95108
/**
96-
* Add a $nor clause to the current query.
109+
* Add one or more $nor clauses to the current query.
97110
*
98111
* @see Builder::addNor()
99112
* @see http://docs.mongodb.org/manual/reference/operator/nor/
100113
* @param array|Expr $expression
101114
* @return $this
102115
*/
103-
public function addNor($expression)
116+
public function addNor($expression /* , $expression2, ... */)
104117
{
105-
$this->query['$nor'][] = $expression instanceof Expr ? $expression->getQuery() : $expression;
118+
if (! isset($this->query['$nor'])) {
119+
$this->query['$nor'] = [];
120+
}
121+
122+
$this->query['$nor'] = array_merge(
123+
$this->query['$nor'],
124+
array_map(function ($expression) { return $expression instanceof Expr ? $expression->getQuery() : $expression; }, func_get_args())
125+
);
126+
106127
return $this;
107128
}
108129

109130
/**
110-
* Add an $or clause to the current query.
131+
* Add one or more $or clauses to the current query.
111132
*
112133
* @see Builder::addOr()
113134
* @see http://docs.mongodb.org/manual/reference/operator/or/
114135
* @param array|Expr $expression
115136
* @return $this
116137
*/
117-
public function addOr($expression)
138+
public function addOr($expression /* , $expression2, ... */)
118139
{
119-
$this->query['$or'][] = $expression instanceof Expr ? $expression->getQuery() : $expression;
140+
if (! isset($this->query['$or'])) {
141+
$this->query['$or'] = [];
142+
}
143+
144+
$this->query['$or'] = array_merge(
145+
$this->query['$or'],
146+
array_map(function ($expression) { return $expression instanceof Expr ? $expression->getQuery() : $expression; }, func_get_args())
147+
);
148+
120149
return $this;
121150
}
122151

0 commit comments

Comments
 (0)