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

Commit

Permalink
Merge pull request #298 from alcaeus/fix-aggregation-expression-conve…
Browse files Browse the repository at this point in the history
…rsion

Fix BC break when converting expression objects
  • Loading branch information
alcaeus authored Aug 9, 2017
2 parents 485ccf0 + 0f7764c commit 9ad5ca3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG-1.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CHANGELOG for 1.6.x
===================

This changelog references the relevant changes (bug and security fixes) done
in 1.6.x patch versions.

1.6.0 (2017-08-09)
------------------

* [#298](https://github.com/doctrine/mongodb/pull/298): Fix BC break when converting expression objects
* [#301](https://github.com/doctrine/mongodb/pull/301): Test against PHP 7.2
21 changes: 21 additions & 0 deletions UPGRADE-1.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Upgrade from 1.5 to 1.6
=======================

Aggregation builder
-------------------

* Fixes a BC break when converting expression objects in the
`Doctrine\MongoDB\Aggregation\Expr` class.
* Adds extension points to customize expression conversion in the following stages:
* `$bucket`
* `$bucketAuto`
* `$graphLookup`
* `$replaceRoot`

Version support
---------------
* Support for MongoDB server versions below 3.0 has been dropped. These are no
longer supported by MongoDB. We recommend you upgrade to a recent version
* The 1.5 version of the legacy driver is no longer supported. This library now
requires at least version 1.6.7 of the legacy driver.
* PHP 7.2 is also supported using [mongo-php-adapter](https://github.com/alcaeus/mongo-php-adapter)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.5.x-dev"
"dev-master": "1.6.x-dev"
}
}
}
26 changes: 16 additions & 10 deletions lib/Doctrine/MongoDB/Aggregation/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function addAnd($expression /*, $expression2, ... */)
$this->expr['$and'] = [];
}

$this->expr['$and'] = array_merge($this->expr['$and'], array_map(['static', 'convertExpression'], func_get_args()));
$this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'ensureArray'], func_get_args()));

return $this;
}
Expand All @@ -133,7 +133,7 @@ public function addOr($expression /*, $expression2, ... */)
$this->expr['$or'] = [];
}

$this->expr['$or'] = array_merge($this->expr['$or'], array_map(['static', 'convertExpression'], func_get_args()));
$this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'ensureArray'], func_get_args()));

return $this;
}
Expand Down Expand Up @@ -359,7 +359,13 @@ public static function convertExpression($expression)
*/
protected function ensureArray($expression)
{
return static::convertExpression($expression);
if (is_array($expression)) {
return array_map([$this, 'ensureArray'], $expression);
} elseif ($expression instanceof self) {
return $expression->getExpression();
}

return $expression;
}

/**
Expand Down Expand Up @@ -442,9 +448,9 @@ protected function defaultInternal($expression)
$this->requiresSwitchStatement(static::class . '::default');

if ($this->currentField) {
$this->expr[$this->currentField]['$switch']['default'] = static::convertExpression($expression);
$this->expr[$this->currentField]['$switch']['default'] = $this->ensureArray($expression);
} else {
$this->expr['$switch']['default'] = static::convertExpression($expression);
$this->expr['$switch']['default'] = $this->ensureArray($expression);
}

return $this;
Expand Down Expand Up @@ -518,7 +524,7 @@ public function expr()
public function expression($value)
{
$this->requiresCurrentField(__METHOD__);
$this->expr[$this->currentField] = static::convertExpression($value);
$this->expr[$this->currentField] = $this->ensureArray($value);

return $this;
}
Expand Down Expand Up @@ -1121,9 +1127,9 @@ public function not($expression)
private function operator($operator, $expression)
{
if ($this->currentField) {
$this->expr[$this->currentField][$operator] = static::convertExpression($expression);
$this->expr[$this->currentField][$operator] = $this->ensureArray($expression);
} else {
$this->expr[$operator] = static::convertExpression($expression);
$this->expr[$operator] = $this->ensureArray($expression);
}

return $this;
Expand Down Expand Up @@ -1619,9 +1625,9 @@ protected function thenInternal($expression)
$this->switchBranch['then'] = $expression;

if ($this->currentField) {
$this->expr[$this->currentField]['$switch']['branches'][] = static::convertExpression($this->switchBranch);
$this->expr[$this->currentField]['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
} else {
$this->expr['$switch']['branches'][] = static::convertExpression($this->switchBranch);
$this->expr['$switch']['branches'][] = $this->ensureArray($this->switchBranch);
}

$this->switchBranch = null;
Expand Down
17 changes: 16 additions & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/AbstractBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getExpression()
{
$stage = [
'$bucket' => [
'groupBy' => Expr::convertExpression($this->groupBy),
'groupBy' => $this->convertExpression($this->groupBy),
] + $this->getExtraPipelineFields(),
];

Expand All @@ -56,6 +56,21 @@ public function getExpression()
return $stage;
}

/**
* Converts an expression object into an array, recursing into nested items
*
* This method is meant to be overwritten by extending classes to apply
* custom conversions (e.g. field name translation in MongoDB ODM) to the
* expression object.
*
* @param mixed|self $expression
* @return string|array
*/
protected function convertExpression($expression)
{
return Expr::convertExpression($expression);
}

/**
* @return array
*/
Expand Down
17 changes: 16 additions & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/GraphLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getExpression()
{
$graphLookup = [
'from' => $this->from,
'startWith' => Expr::convertExpression($this->startWith),
'startWith' => $this->convertExpression($this->startWith),
'connectFromField' => $this->connectFromField,
'connectToField' => $this->connectToField,
'as' => $this->as,
Expand All @@ -246,4 +246,19 @@ public function getExpression()

return ['$graphLookup' => $graphLookup];
}

/**
* Converts an expression object into an array, recursing into nested items
*
* This method is meant to be overwritten by extending classes to apply
* custom conversions (e.g. field name translation in MongoDB ODM) to the
* expression object.
*
* @param mixed|self $expression
* @return string|array
*/
protected function convertExpression($expression)
{
return Expr::convertExpression($expression);
}
}
17 changes: 16 additions & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ public function __construct(Builder $builder, $expression = null)
public function getExpression()
{
return [
'$replaceRoot' => $this->expression !== null ? Expr::convertExpression($this->expression) : $this->expr->getExpression()
'$replaceRoot' => $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression()
];
}

/**
* Converts an expression object into an array, recursing into nested items
*
* This method is meant to be overwritten by extending classes to apply
* custom conversions (e.g. field name translation in MongoDB ODM) to the
* expression object.
*
* @param mixed|self $expression
* @return string|array
*/
protected function convertExpression($expression)
{
return Expr::convertExpression($expression);
}
}

0 comments on commit 9ad5ca3

Please sign in to comment.