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

Add query operators introduced with MongoDB 3.2 #241

Merged
merged 2 commits into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions lib/Doctrine/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,66 @@ public function bitOr($value)
return $this;
}

/**
* Matches documents where all of the bit positions given by the query are
* clear.
*
* @see Expr::bitsAllClear()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllClear/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAllClear($value)
{
$this->expr->bitsAllClear($value);
return $this;
}

/**
* Matches documents where all of the bit positions given by the query are
* set.
*
* @see Expr::bitsAllSet()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllSet/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAllSet($value)
{
$this->expr->bitsAllSet($value);
return $this;
}

/**
* Matches documents where any of the bit positions given by the query are
* clear.
*
* @see Expr::bitsAnyClear()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnyClear/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAnyClear($value)
{
$this->expr->bitsAnyClear($value);
return $this;
}

/**
* Matches documents where any of the bit positions given by the query are
* set.
*
* @see Expr::bitsAnySet()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnySet/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAnySet($value)
{
$this->expr->bitsAnySet($value);
return $this;
}

/**
* Apply a bitwise xor operation on the current field.
*
Expand All @@ -216,6 +276,26 @@ public function bitXor($value)
return $this;
}

/**
* A boolean flag to enable or disable case sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Expr::caseSensitive()
* @see http://docs.mongodb.org/manual/reference/operator/text/
* @param bool $caseSensitive
* @return self
* @throws BadMethodCallException if the query does not already have $text criteria
*
* @since 1.3
*/
public function caseSensitive($caseSensitive)
{
$this->expr->caseSensitive($caseSensitive);
return $this;
}

/**
* Associates a comment to any expression taking a query predicate.
*
Expand Down Expand Up @@ -270,6 +350,26 @@ public function debug($name = null)
return $name !== null ? $this->query[$name] : $this->query;
}

/**
* A boolean flag to enable or disable diacritic sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Builder::diacriticSensitive()
* @see http://docs.mongodb.org/manual/reference/operator/text/
* @param bool $diacriticSensitive
* @return self
* @throws BadMethodCallException if the query does not already have $text criteria
*
* @since 1.3
*/
public function diacriticSensitive($diacriticSensitive)
{
$this->expr->diacriticSensitive($diacriticSensitive);
return $this;
}

/**
* Set the "distanceMultiplier" option for a geoNear command query.
*
Expand Down
120 changes: 120 additions & 0 deletions lib/Doctrine/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,66 @@ public function bitOr($value)
return $this->bit('or', $value);
}

/**
* Matches documents where all of the bit positions given by the query are
* clear.
*
* @see Builder::bitsAllClear()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllClear/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAllClear($value)
{
$this->requiresCurrentField();
return $this->operator('$bitsAllClear', $value);
}

/**
* Matches documents where all of the bit positions given by the query are
* set.
*
* @see Builder::bitsAllSet()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllSet/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAllSet($value)
{
$this->requiresCurrentField();
return $this->operator('$bitsAllSet', $value);
}

/**
* Matches documents where any of the bit positions given by the query are
* clear.
*
* @see Builder::bitsAnyClear()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnyClear/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAnyClear($value)
{
$this->requiresCurrentField();
return $this->operator('$bitsAnyClear', $value);
}

/**
* Matches documents where any of the bit positions given by the query are
* set.
*
* @see Builder::bitsAnySet()
* @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnySet/
* @param int|array|\MongoBinData $value
* @return self
*/
public function bitsAnySet($value)
{
$this->requiresCurrentField();
return $this->operator('$bitsAnySet', $value);
}

/**
* Apply a bitwise xor operation on the current field.
*
Expand All @@ -215,6 +275,36 @@ public function bitXor($value)
return $this->bit('xor', $value);
}

/**
* A boolean flag to enable or disable case sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Builder::caseSensitive()
* @see http://docs.mongodb.org/manual/reference/operator/text/
* @param bool $caseSensitive
* @return self
* @throws BadMethodCallException if the query does not already have $text criteria
*
* @since 1.3
*/
public function caseSensitive($caseSensitive)
{
if ( ! isset($this->query['$text'])) {
throw new BadMethodCallException('This method requires a $text operator (call text() first)');
}

// Remove caseSensitive option to keep support for older database versions
if ($caseSensitive) {
$this->query['$text']['$caseSensitive'] = true;
} elseif (isset($this->query['$text']['$caseSensitive'])) {
unset($this->query['$text']['$caseSensitive']);
}

return $this;
}

/**
* Associates a comment to any expression taking a query predicate.
*
Expand Down Expand Up @@ -249,6 +339,36 @@ public function currentDate($type = 'date')
return $this;
}

/**
* A boolean flag to enable or disable diacritic sensitive search for $text
* criteria.
*
* This method must be called after text().
*
* @see Builder::diacriticSensitive()
* @see http://docs.mongodb.org/manual/reference/operator/text/
* @param bool $diacriticSensitive
* @return self
* @throws BadMethodCallException if the query does not already have $text criteria
*
* @since 1.3
*/
public function diacriticSensitive($diacriticSensitive)
{
if ( ! isset($this->query['$text'])) {
throw new BadMethodCallException('This method requires a $text operator (call text() first)');
}

// Remove diacriticSensitive option to keep support for older database versions
if ($diacriticSensitive) {
$this->query['$text']['$diacriticSensitive'] = true;
} elseif (isset($this->query['$text']['$diacriticSensitive'])) {
unset($this->query['$text']['$diacriticSensitive']);
}

return $this;
}

/**
* Add $each criteria to the expression for a $push operation.
*
Expand Down
8 changes: 7 additions & 1 deletion tests/Doctrine/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,16 @@ public function provideProxiedExprMethods()
'elemMatch() Expr' => array('elemMatch', array($this->getMockExpr())),
'not()' => array('not', array($this->getMockExpr())),
'language()' => array('language', array('en')),
'caseSensitive()' => array('caseSensitive', array(true)),
'diacriticSensitive()' => array('diacriticSensitive', array(true)),
'text()' => array('text', array('foo')),
'max()' => array('max', array(1)),
'min()' => array('min', array(1)),
'comment()' => array('comment', array('A comment explaining what the query does'))
'comment()' => array('comment', array('A comment explaining what the query does')),
'bitsAllClear()' => array('bitsAllClear', array(5)),
'bitsAllSet()' => array('bitsAllSet', array(5)),
'bitsAnyClear()' => array('bitsAnyClear', array(5)),
'bitsAnySet()' => array('bitsAnySet', array(5)),
);
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Doctrine/MongoDB/Tests/Query/ExprTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,62 @@ public function testLanguageRequiresTextOperator()
$expr->language('en');
}

public function testCaseSensitiveWithText()
{
$expr = new Expr();
$expr->text('foo');

$this->assertSame($expr, $expr->caseSensitive(true));
$this->assertEquals(array('$text' => array('$search' => 'foo', '$caseSensitive' => true)), $expr->getQuery());
}

public function testCaseSensitiveFalseRemovesOption()
{
$expr = new Expr();
$expr->text('foo');

$expr->caseSensitive(true);
$expr->caseSensitive(false);
$this->assertEquals(array('$text' => array('$search' => 'foo')), $expr->getQuery());
}

/**
* @expectedException BadMethodCallException
*/
public function testCaseSensitiveRequiresTextOperator()
{
$expr = new Expr();
$expr->caseSensitive('en');
}

public function testDiacriticSensitiveWithText()
{
$expr = new Expr();
$expr->text('foo');

$this->assertSame($expr, $expr->diacriticSensitive(true));
$this->assertEquals(array('$text' => array('$search' => 'foo', '$diacriticSensitive' => true)), $expr->getQuery());
}

public function testDiacriticSensitiveFalseRemovesOption()
{
$expr = new Expr();
$expr->text('foo');

$expr->diacriticSensitive(true);
$expr->diacriticSensitive(false);
$this->assertEquals(array('$text' => array('$search' => 'foo')), $expr->getQuery());
}

/**
* @expectedException BadMethodCallException
*/
public function testDiacriticSensitiveRequiresTextOperator()
{
$expr = new Expr();
$expr->diacriticSensitive('en');
}

public function testOperatorWithCurrentField()
{
$expr = new Expr();
Expand Down