Skip to content
This repository has been archived by the owner on May 31, 2018. It is now read-only.

Commit

Permalink
added whereNearSphere methods
Browse files Browse the repository at this point in the history
  • Loading branch information
navruzm committed Mar 21, 2013
1 parent a628159 commit 4fca0d7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/LMongo/Query/Builder.php
Expand Up @@ -940,6 +940,82 @@ public function norWhereNear($column, array $coords, $geometry = null, $maxDista
return $this->whereNear($column, $coords, $geometry, $maxDistance, '$nor');
}

/**
* Add an "$nearSphere geospatial operation" clause to logical operation.
*
* @param string $column
* @param array $coords
* @param mixed $geometry
* @param mixed $maxDistance
* @param string $boolean
* @return LMongo\Query\Builder
*/
public function whereNearSphere($column, array $coords, $geometry = null, $maxDistance = null, $boolean = 'first')
{
if(is_null($geometry))
{
$value = array('$nearSphere' => $coords);

if( ! is_null($maxDistance))
{
$value['$maxDistance'] = $maxDistance;
}
}
else
{
$value = array('$nearSphere' => array('$geometry' => array('type' => $geometry, 'coordinates' => $coords)));

if( ! is_null($maxDistance))
{
$value['$nearSphere']['$geometry']['$maxDistance'] = $maxDistance;
}
}

return $this->where($column, $value, $boolean);
}

/**
* Add an "$nearSphere geospatial operation" clause to logical $and operation.
*
* @param string $column
* @param array $coords
* @param mixed $geometry
* @param mixed $maxDistance
* @return LMongo\Query\Builder
*/
public function andWhereNearSphere($column, array $coords, $geometry = null, $maxDistance = null)
{
return $this->whereNearSphere($column, $coords, $geometry, $maxDistance, '$and');
}

/**
* Add an "$nearSphere geospatial operation" clause to logical $or operation.
*
* @param string $column
* @param array $coords
* @param mixed $geometry
* @param mixed $maxDistance
* @return LMongo\Query\Builder
*/
public function orWhereNearSphere($column, array $coords, $geometry = null, $maxDistance = null)
{
return $this->whereNearSphere($column, $coords, $geometry, $maxDistance, '$or');
}

/**
* Add an "$nearSphere geospatial operation" clause to logical $nor operation.
*
* @param string $column
* @param array $coords
* @param mixed $geometry
* @param mixed $maxDistance
* @return LMongo\Query\Builder
*/
public function norWhereNearSphere($column, array $coords, $geometry = null, $maxDistance = null)
{
return $this->whereNearSphere($column, $coords, $geometry, $maxDistance, '$nor');
}

/**
* Add an "$within geospatial operation" clause to logical operation.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/LMongoQueryBuilderTest.php
Expand Up @@ -406,6 +406,41 @@ public function testWhereNears()
$this->assertEquals(array('$and' => array(array('name' => 'John'),array('location' => array('$near' => array('$geometry' => array('type' => 'Polygon', 'coordinates' => array(5, 3), '$maxDistance' => 10)))))), $builder->compileWheres($builder));
}

public function testWhereNearSpheres()
{
$builder = $this->getBuilder();
$builder->whereNearSphere('location', array(5, 3));
$this->assertEquals(array('$and' => array(array('location' => array('$nearSphere' => array(5, 3))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->whereNearSphere('location', array(5, 3), null, 10);
$this->assertEquals(array('$and' => array(array('location' => array('$nearSphere' => array(5, 3), '$maxDistance' => 10)))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->whereNearSphere('location', array(5, 3), 'Polygon');
$this->assertEquals(array('$and' => array(array('location' => array('$nearSphere' => array('$geometry' => array('type' => 'Polygon', 'coordinates' => array(5, 3))))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->whereNearSphere('location', array(5, 3), 'Polygon', 10);
$this->assertEquals(array('$and' => array(array('location' => array('$nearSphere' => array('$geometry' => array('type' => 'Polygon', 'coordinates' => array(5, 3), '$maxDistance' => 10)))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->where('name', 'John')->andWhereNearSphere('location', array(5, 3));
$this->assertEquals(array('$and' => array(array('name' => 'John'), array('location' => array('$nearSphere' => array(5, 3))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->where('name', 'John')->orWhereNearSphere('location', array(5, 3));
$this->assertEquals(array('$or' => array(array('name' => 'John'), array('location' => array('$nearSphere' => array(5, 3))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->where('name', 'John')->norWhereNearSphere('location', array(5, 3));
$this->assertEquals(array('$nor' => array(array('name' => 'John'), array('location' => array('$nearSphere' => array(5, 3))))), $builder->compileWheres($builder));

$builder = $this->getBuilder();
$builder->where('name', 'John')->andwhereNearSphere('location', array(5, 3), 'Polygon', 10);
$this->assertEquals(array('$and' => array(array('name' => 'John'),array('location' => array('$nearSphere' => array('$geometry' => array('type' => 'Polygon', 'coordinates' => array(5, 3), '$maxDistance' => 10)))))), $builder->compileWheres($builder));
}

public function testWhereWithins()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 4fca0d7

Please sign in to comment.