Skip to content

Commit

Permalink
Added geospatial helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Nov 16, 2012
1 parent fbfd592 commit f53efcc
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Monga/Query/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,78 @@ public function orWhereId($value, $field = '_id')
return $this->_where('$or', $field, $value);
}

/**
* Appends a and-where-near statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function whereNear($field, $lon, $lat, $options = array())
{
return $this->_where('$and', $field, array('$near' => array($lon, $lat)) + $options);
}

/**
* Appends a and-where-near statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function andWhereNear($field, $lon, $lat, $options = array())
{
return call_user_func_array(array($this, 'whereNear'), array($field, $lon, $lat, $options));
}

/**
* Appends a and-where-near statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function orWhereNear($field, $lon, $lat, $options = array())
{
return $this->_where('$or', $field, array('$near' => array($lon, $lat)) + $options);
}

/**
* Appends a and-where-within statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function whereWithin($field, $shape, $options = array())
{
return $this->_where('$and', $field, array('$within' => $shape) + $options);
}

/**
* Appends a and-where-within statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function andWhereWithin($field, $shape, $options = array())
{
return call_user_func_array(array($this, 'whereWithin'), array($field, $shape, $options));
}

/**
* Appends a and-where-near statement
*
* @param float $lon longitude
* @param float $lat latitude
* @return object $this
*/
public function orWhereWithin($field, $shape, $options = array())
{
return $this->_where('$or', $field, array('$within' => $shape) + $options);
}

/**
* Appends a and-nor-where-clause
*
Expand Down
114 changes: 114 additions & 0 deletions tests/QueryWhereTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,120 @@ public function testOrWhereGt()
$this->assertEquals($expected, $this->getProperty('where'));
}

public function testWhereNear()
{
$this->query->whereNear('location', 10, 10, array('$maxDistance' => 5));

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)),
)
)
)
);

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

public function testAndWhereNear()
{
$this->query->andWhereNear('location', 10, 10, array('$maxDistance' => 5));

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)),
)
)
)
);

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

public function testOrWhereNear()
{
$this->query->whereNear('location', 10, 10, array('$maxDistance' => 5))
->orWhereNear('location', 10, 10, array('$maxDistance' => 5));

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)),
)
),
array(
'$and' => array(
array('location' => array('$near' => array(10, 10), '$maxDistance' => 5)),
)
)
)
);

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

public function testWhereWithin()
{
$this->query->whereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5));

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)),
)
)
)
);

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

public function testAndWhereWithin()
{
$this->query->andWhereWithin('location', array('$box' => array(array(0,0),array(10,10))), array('$maxDistance' => 5));

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)),
)
)
)
);

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

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

$expected = array(
'$or' => array(
array(
'$and' => array(
array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)),
)
),
array(
'$and' => array(
array('location' => array('$within' => array('$box' => array(array(0,0),array(10,10))), '$maxDistance' => 5)),
)
)
)
);

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

public function testWhereBetween()
{
$this->query->whereBetween('name', 10, 15);
Expand Down

0 comments on commit f53efcc

Please sign in to comment.