Commit 9e3ae1fa authored by Bas de Nooijer's avatar Bas de Nooijer

Updated geospatial query helper functions to support dereferenced params, fixes issue #114

parent b2612442
......@@ -216,13 +216,14 @@ class Helper
*
* Find all entries within the distance of a certain point.
*
* @param $field
* @param $pointX
* @param $pointY
* @param $field
* @param $distance
* @param boolean $dereferenced
* @return string
*/
public function geofilt($pointX, $pointY, $field, $distance)
public function geofilt($field, $pointX, $pointY, $distance, $dereferenced = false)
{
return $this->qparser(
'geofilt',
......@@ -230,7 +231,8 @@ class Helper
'pt' => $pointX.','.$pointY,
'sfield' => $field,
'd' => $distance
)
),
$dereferenced
);
}
......@@ -242,13 +244,14 @@ class Helper
* guaranteed to encompass all of the points of interest, but it may also
* include other points that are slightly outside of the required distance.
*
* @param string $field
* @param string $pointX
* @param string $pointY
* @param string $field
* @param string $distance
* @param boolean $dereferenced
* @return string
*/
public function bbox($pointX, $pointY, $field, $distance)
public function bbox($field, $pointX, $pointY, $distance, $dereferenced = false)
{
return $this->qparser(
'bbox',
......@@ -256,7 +259,8 @@ class Helper
'pt' => $pointX.','.$pointY,
'sfield' => $field,
'd' => $distance
)
),
$dereferenced
);
}
......@@ -269,16 +273,18 @@ class Helper
* or combining the distance with the relevancy score,
* such as boosting by the inverse of the distance.
*
* @param $field
* @param $pointX
* @param $pointY
* @param $field
* @param boolean $dereferenced
* @return string
*/
public function geodist($pointX, $pointY, $field)
public function geodist($field, $pointX, $pointY, $dereferenced = false)
{
return $this->functionCall(
'geodist',
array($pointX, $pointY, $field)
array('sfield' => $field, 'pt' => $pointX.','.$pointY),
$dereferenced
);
}
......@@ -289,9 +295,10 @@ class Helper
* @param string $name
* @param array $params
* @param boolean $dereferenced
* @param boolean $forceKeys
* @return string
*/
public function qparser($name, $params = array(), $dereferenced = false)
public function qparser($name, $params = array(), $dereferenced = false, $forceKeys = false)
{
if ($dereferenced) {
......@@ -303,8 +310,12 @@ class Helper
}
foreach ($params as $paramKey => $paramValue) {
if (is_int($paramKey) || $forceKeys) {
$this->derefencedParamsLastKey++;
$derefKey = 'deref_' . $this->derefencedParamsLastKey;
} else {
$derefKey = $paramKey;
}
$this->query->addParam($derefKey, $paramValue);
$params[$paramKey] = '$'.$derefKey;
}
......@@ -312,8 +323,10 @@ class Helper
$output = '{!'.$name;
foreach ($params as $key => $value) {
if (!$dereferenced || $forceKeys || is_int($key)) {
$output .= ' ' . $key . '=' . $value;
}
}
$output .= '}';
return $output;
......@@ -324,12 +337,20 @@ class Helper
*
* @param string $name
* @param array $params
* @param boolean $dereferenced
* @return string
*/
public function functionCall($name, $params = array())
public function functionCall($name, $params = array(), $dereferenced = false)
{
if ($dereferenced) {
foreach($params as $key => $value) {
$this->query->addParam($key, $value);
}
return $name . '()';
} else {
return $name . '(' . implode($params, ',') . ')';
}
}
/**
* Assemble a querystring with placeholders
......@@ -408,7 +429,7 @@ class Helper
*/
public function join($from, $to, $dereferenced = false)
{
return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced);
return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced, $dereferenced);
}
}
......@@ -81,7 +81,20 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
'{!geofilt pt=45.15,-93.85 sfield=store d=5}',
$this->helper->geofilt(45.15, -93.85, 'store', 5)
$this->helper->geofilt('store', 45.15, -93.85, 5)
);
}
public function testGeofiltDereferenced()
{
$this->assertEquals(
'{!geofilt}',
$this->helper->geofilt('store', 45.15, -93.85, 5, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85', 'd' => 5),
$this->query->getParams()
);
}
......@@ -89,15 +102,41 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
'{!bbox pt=45.15,-93.85 sfield=store d=5}',
$this->helper->bbox(45.15, -93.85, 'store', 5)
$this->helper->bbox('store', 45.15, -93.85, 5)
);
}
public function testBboxDereferenced()
{
$this->assertEquals(
'{!bbox}',
$this->helper->bbox('store', 45.15, -93.85, 5, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85', 'd' => 5),
$this->query->getParams()
);
}
public function testGeodist()
{
$this->assertEquals(
'geodist(45.15,-93.85,store)',
$this->helper->geodist(45.15, -93.85, 'store')
'geodist(store,45.15,-93.85)',
$this->helper->geodist('store', 45.15, -93.85)
);
}
public function testGeodistDereferenced()
{
$this->assertEquals(
'geodist()',
$this->helper->geodist('store', 45.15, -93.85, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85'),
$this->query->getParams()
);
}
......@@ -128,7 +167,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
'{!join from=$deref_1 to=$deref_2}',
$this->helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true)
$this->helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true, true)
);
$this->assertEquals(
......@@ -139,7 +178,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
// second call, params should have updated counts
$this->assertEquals(
'{!join from=$deref_3 to=$deref_4}',
$this->helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true)
$this->helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true, true)
);
// previous params should also still be there
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment