Browse files

Implement polygon bound queries

  • Loading branch information...
1 parent 9114efe commit 60a99e5ce36aa34a5ae9f5fe9623e84bfddc99c4 @jmikola jmikola committed Jun 20, 2012
View
11 lib/Doctrine/MongoDB/Query/Builder.php
@@ -588,6 +588,17 @@ public function withinCenter($x, $y, $radius)
return $this;
}
+ /**
+ * Add where $within $polygon query.
+ *
+ * @param array $point,... Three or more point coordinate tuples
+ * @return Builder
+ */
+ public function withinPolygon(/* array($x1, $y1), array($x2, $y2), ... */)
+ {
+ call_user_func_array(array($this->expr, 'withinPolygon'), func_get_args());
+ return $this;
+ }
/**
* Set sort.
View
14 lib/Doctrine/MongoDB/Query/Expr.php
@@ -227,6 +227,20 @@ public function withinCenter($x, $y, $radius)
return $this;
}
+ public function withinPolygon(/* array($x1, $y1), array($x2, $y2), ... */)
+ {
+ if (func_num_args() < 3) {
+ throw new \InvalidArgumentException('Polygon must be defined by three points or more.');
+ }
+
+ if ($this->currentField) {
+ $this->query[$this->currentField][$this->cmd . 'within'][$this->cmd . 'polygon'] = func_get_args();
+ } else {
+ $this->query[$this->cmd . 'within'][$this->cmd . 'polygon'] = func_get_args();
+ }
+ return $this;
+ }
+
public function set($value, $atomic = true)
{
$this->requiresCurrentField();
View
24 tests/Doctrine/MongoDB/Tests/Query/BuilderTest.php
@@ -366,6 +366,30 @@ public function testDeepClone()
$this->assertCount(1, $qb->getQueryArray());
}
+ public function testWithinPolygon()
+ {
+ $qb = $this->getTestQueryBuilder()
+ ->field('loc')->withinPolygon(array(0, 0), array(2, 0), array(0, 2));
+
+ $expected = array(
+ 'loc' => array(
+ '$within' => array(
+ '$polygon' => array(array(0, 0), array(2, 0), array(0, 2)),
+ ),
+ ),
+ );
+ $this->assertEquals($expected, $qb->getQueryArray());
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testWithinPolygonRequiresAtLeastThreePoints()
+ {
+ $qb = $this->getTestQueryBuilder()
+ ->field('loc')->withinPolygon(array(0, 0), array(1, 1));
+ }
+
private function getTestQueryBuilder()
{
return $this->conn->selectCollection('db', 'users')->createQueryBuilder();

1 comment on commit 60a99e5

@flo-sch

I didn't test it for moment, I'm going to see that !
Thank you a lot for this !

Please sign in to comment.