From 4fca0d786cd962a985ae89492aecf93e0c3c57a7 Mon Sep 17 00:00:00 2001 From: Mustafa Navruz Date: Thu, 21 Mar 2013 19:39:23 +0200 Subject: [PATCH] added whereNearSphere methods --- src/LMongo/Query/Builder.php | 76 ++++++++++++++++++++++++++++++++ tests/LMongoQueryBuilderTest.php | 35 +++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/src/LMongo/Query/Builder.php b/src/LMongo/Query/Builder.php index fc51c79..e3ba7bd 100755 --- a/src/LMongo/Query/Builder.php +++ b/src/LMongo/Query/Builder.php @@ -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. * diff --git a/tests/LMongoQueryBuilderTest.php b/tests/LMongoQueryBuilderTest.php index 9dc7d30..18b6e08 100644 --- a/tests/LMongoQueryBuilderTest.php +++ b/tests/LMongoQueryBuilderTest.php @@ -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();