Commit 47b03a15 authored by Bas de Nooijer's avatar Bas de Nooijer Committed by GitHub

Merge pull request #453 from MyHammer/spatial_component

add spatial component
parents 87641c84 5bb0dae1
<?php
namespace Solarium\QueryType\Select\Query\Component;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
use Solarium\QueryType\Select\RequestBuilder\Component\Spatial as RequestBuilder;
/**
* Spatial component.
*
* @link https://cwiki.apache.org/confluence/display/solr/Spatial+Search
*/
class Spatial extends AbstractComponent
{
/**
* Get component type.
*
* @return string
*/
public function getType()
{
return SelectQuery::COMPONENT_SPATIAL;
}
/**
* Get a requestbuilder for this query.
*
* @return RequestBuilder
*/
public function getRequestBuilder()
{
return new RequestBuilder();
}
/**
* This component has no response parser...
*/
public function getResponseParser()
{
return;
}
/**
* @param string $sfield
*/
public function setField($sfield)
{
$this->setOption('sfield', $sfield);
}
/**
* @param int $distance
*/
public function setDistance($distance)
{
$this->setOption('d', $distance);
}
/**
* @param string $point
*/
public function setPoint($point)
{
$this->setOption('pt', $point);
}
/**
* Get sfield option.
*
* @return string|null
*/
public function getField()
{
return $this->getOption('sfield');
}
/**
* Get d option.
*
* @return int|null
*/
public function getDistance()
{
return $this->getOption('d');
}
/**
* Get pt option.
*
* @return int|null
*/
public function getPoint()
{
return $this->getOption('pt');
}
}
...@@ -127,6 +127,11 @@ class Query extends BaseQuery ...@@ -127,6 +127,11 @@ class Query extends BaseQuery
*/ */
const COMPONENT_DEBUG = 'debug'; const COMPONENT_DEBUG = 'debug';
/**
* Query component spatial.
*/
const COMPONENT_SPATIAL = 'spatial';
/** /**
* Default options. * Default options.
* *
...@@ -166,6 +171,7 @@ class Query extends BaseQuery ...@@ -166,6 +171,7 @@ class Query extends BaseQuery
self::COMPONENT_DISTRIBUTEDSEARCH => 'Solarium\QueryType\Select\Query\Component\DistributedSearch', self::COMPONENT_DISTRIBUTEDSEARCH => 'Solarium\QueryType\Select\Query\Component\DistributedSearch',
self::COMPONENT_STATS => 'Solarium\QueryType\Select\Query\Component\Stats\Stats', self::COMPONENT_STATS => 'Solarium\QueryType\Select\Query\Component\Stats\Stats',
self::COMPONENT_DEBUG => 'Solarium\QueryType\Select\Query\Component\Debug', self::COMPONENT_DEBUG => 'Solarium\QueryType\Select\Query\Component\Debug',
self::COMPONENT_SPATIAL => 'Solarium\QueryType\Select\Query\Component\Spatial',
); );
/** /**
...@@ -1057,6 +1063,18 @@ class Query extends BaseQuery ...@@ -1057,6 +1063,18 @@ class Query extends BaseQuery
return $this->addTags($tags); return $this->addTags($tags);
} }
/**
* Get a Spatial component instance.
*
* This is a convenience method that maps presets to getComponent
*
* @return \Solarium\QueryType\Select\Query\Component\Spatial
*/
public function getSpatial()
{
return $this->getComponent(self::COMPONENT_SPATIAL, true);
}
/** /**
* Initialize options. * Initialize options.
* *
......
<?php
namespace Solarium\QueryType\Select\RequestBuilder\Component;
use Solarium\QueryType\Select\Query\Component\Spatial as SpatialComponent;
use Solarium\Core\Client\Request;
/**
* Add select component spatial to the request.
*/
class Spatial implements ComponentRequestBuilderInterface
{
/**
* Add request settings for Spatial.
*
* @param SpatialComponent $component
* @param Request $request
*
* @return Request
*/
public function buildComponent($component, $request)
{
$request->addParam('sfield', $component->getField());
$request->addParam('pt', $component->getPoint());
$request->addParam('d', $component->getDistance());
return $request;
}
}
...@@ -708,4 +708,14 @@ abstract class AbstractQueryTest extends \PHPUnit_Framework_TestCase ...@@ -708,4 +708,14 @@ abstract class AbstractQueryTest extends \PHPUnit_Framework_TestCase
$this->query->setTags(array('t3', 't4')); $this->query->setTags(array('t3', 't4'));
$this->assertEquals(array('t3', 't4'), $this->query->getTags()); $this->assertEquals(array('t3', 't4'), $this->query->getTags());
} }
public function testGetSpatial()
{
$spatial = $this->query->getSpatial();
$this->assertEquals(
'Solarium\QueryType\Select\Query\Component\Spatial',
get_class($spatial)
);
}
} }
<?php
namespace Solarium\Tests\QueryType\Select\Query\Component;
use Solarium\QueryType\Select\Query\Component\Spatial;
use Solarium\QueryType\Select\Query\Query;
class SpatialTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Spatial
*/
protected $spatial;
public function setUp()
{
$this->spatial = new Spatial;
}
public function testConfigMode()
{
$options = array(
'sfield' => 'geo',
'd' => 50,
'pt' => '48.2233,16.3161',
);
$this->spatial->setOptions($options);
$this->assertEquals($options['sfield'], $this->spatial->getField());
$this->assertEquals($options['d'], $this->spatial->getDistance());
$this->assertEquals($options['pt'], $this->spatial->getPoint());
}
public function testGetType()
{
$this->assertEquals(
Query::COMPONENT_SPATIAL,
$this->spatial->getType()
);
}
public function testGetResponseParser()
{
$this->assertEquals(null, $this->spatial->getResponseParser());
}
public function testGetRequestBuilder()
{
$this->assertInstanceOf(
'Solarium\QueryType\Select\RequestBuilder\Component\Spatial',
$this->spatial->getRequestBuilder()
);
}
public function testSetAndGetField()
{
$value = 'geo';
$this->spatial->setField($value);
$this->assertEquals(
$value,
$this->spatial->getField()
);
}
public function testSetAndGetDistance()
{
$value = 'distance';
$this->spatial->setDistance($value);
$this->assertEquals(
$value,
$this->spatial->getDistance()
);
}
public function testSetAndGetPoint()
{
$value = '52,13';
$this->spatial->setPoint($value);
$this->assertEquals(
$value,
$this->spatial->getPoint()
);
}
}
<?php
namespace Solarium\Tests\QueryType\Select\RequestBuilder\Component;
use Solarium\QueryType\Select\RequestBuilder\Component\Spatial as RequestBuilder;
use Solarium\QueryType\Select\Query\Component\Spatial as Component;
use Solarium\Core\Client\Request;
class SpatialTest extends \PHPUnit_Framework_TestCase
{
public function testBuildComponent()
{
$builder = new RequestBuilder();
$request = new Request();
$component = new Component();
$component->setField('geo');
$component->setDistance(50);
$component->setPoint('48.2233,16.3161');
$request = $builder->buildComponent($component, $request);
$this->assertEquals(
array(
'pt' => '48.2233,16.3161',
'sfield' => 'geo',
'd' => 50,
),
$request->getParams()
);
}
}
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