Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add *WhereGeoWithin methods (Mongo > 2.4) #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

All notable changes to `monga` will be documented in this file.

## [Unreleased]

### Added

- *WhereGeoWithin (and+or) $geoWithin (Mongo > 2.4)
- PHPUnit test cover for *WhereGeoWithin

### Deprecated

- *whereWithin, deprected after Mongo 2.4, use $geoWithin instead

## 1.2.4 - 2015-02-29

- Updated license year.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "league/monga",
"name": "devitek/monga",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vendor should stay as "league"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, my bad, I published a new version on my fork, I forget that the PR will update too. I'll fix it right now !

"type": "library",
"description": "MongoDB Abstraction Layer",
"keywords": ["MongoDB"],
Expand Down
52 changes: 50 additions & 2 deletions src/League/Monga/Query/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,8 @@ public function orWhereNear($field, $lon, $lat, $options = [])
/**
* Appends a and-where-within statement
*
* @deprecated since Mongo 2.4, use whereGeoWithin
*
* @param string $field _id field
* @param string $shape shape
* @param array $options options
Expand All @@ -946,6 +948,8 @@ public function whereWithin($field, $shape, $options = [])
/**
* Appends a and-where-within statement
*
* @deprecated since Mongo 2.4, use whereGeoWithin
*
* @param string $field _id field
* @param string $shape shape
* @param array $options options
Expand All @@ -958,7 +962,9 @@ public function andWhereWithin($field, $shape, $options = [])
}

/**
* Appends a and-where-near statement
* Appends a and-where-within statement
*
* @deprecated since Mongo 2.4, use whereGeoWithin
*
* @param string $field _id field
* @param string $shape shape
Expand All @@ -971,11 +977,53 @@ public function orWhereWithin($field, $shape, $options = [])
return $this->formatWhere('$or', $field, ['$within' => $shape] + $options);
}

/**
* Appends a and-where-geoWithin statement
*
* @param string $field _id field
* @param string $shape shape
* @param array $options options
*
* @return object $this
*/
public function whereGeoWithin($field, $shape, $options = [])
{
return $this->formatWhere('$and', $field, ['$geoWithin' => $shape] + $options);
}

/**
* Appends a and-where-geoWithin statement
*
* @param string $field _id field
* @param string $shape shape
* @param array $options options
*
* @return object $this
*/
public function andWhereGeoWithin($field, $shape, $options = [])
{
return call_user_func_array([$this, 'whereGeoWithin'], [$field, $shape, $options]);
}

/**
* Appends a and-where-geoWithin statement
*
* @param string $field _id field
* @param string $shape shape
* @param array $options options
*
* @return object $this
*/
public function orWhereGeoWithin($field, $shape, $options = [])
{
return $this->formatWhere('$or', $field, ['$geoWithin' => $shape] + $options);
}

/**
* Appends a and-nor-where-clause
*
* @param array|closure $clause nor where clause
* @param integer $type chain type
* @param string $type chain type
*
* @return object $this
*/
Expand Down
59 changes: 58 additions & 1 deletion tests/QueryWhereTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,23 @@ public function testWhereWithin()
$this->assertEquals($expected, $this->getProperty('where'));
}

public function testWhereGeoWithin()
{
$this->query->whereGeoWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);

$expected = [
'$or' => [
[
'$and' => [
['location' => ['$within' => ['$box' => [[0,0],[10,10]]], '$maxDistance' => 5]],
],
],
],
];

$this->assertEquals($expected, $this->getProperty('where'));
}

public function testAndWhereWithin()
{
$this->query->andWhereWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);
Expand All @@ -1066,10 +1083,50 @@ public function testAndWhereWithin()
$this->assertEquals($expected, $this->getProperty('where'));
}

public function testAndWhereGeoWithin()
{
$this->query->andWhereGeoWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);

$expected = [
'$or' => [
[
'$and' => [
['location' => ['$within' => ['$box' => [[0,0],[10,10]]], '$maxDistance' => 5]],
],
],
],
];

$this->assertEquals($expected, $this->getProperty('where'));
}

public function testOrWhereWithin()
{
$this->query->whereWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5])
->orWhereWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);
->orWhereWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);

$expected = [
'$or' => [
[
'$and' => [
['location' => ['$within' => ['$box' => [[0,0],[10,10]]], '$maxDistance' => 5]],
],
],
[
'$and' => [
['location' => ['$within' => ['$box' => [[0,0],[10,10]]], '$maxDistance' => 5]],
],
],
],
];

$this->assertEquals($expected, $this->getProperty('where'));
}

public function testOrWhereGeoWithin()
{
$this->query->whereGeoWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5])
->orWhereGeoWithin('location', ['$box' => [[0, 0], [10, 10]]], ['$maxDistance' => 5]);

$expected = [
'$or' => [
Expand Down