Skip to content

Commit

Permalink
Merge pull request solariumphp#453 from MyHammer/spatial_component
Browse files Browse the repository at this point in the history
add spatial component
  • Loading branch information
basdenooijer authored Oct 18, 2016
2 parents 87641c8 + 5bb0dae commit 47b03a1
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 0 deletions.
96 changes: 96 additions & 0 deletions library/Solarium/QueryType/Select/Query/Component/Spatial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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');
}
}
18 changes: 18 additions & 0 deletions library/Solarium/QueryType/Select/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class Query extends BaseQuery
*/
const COMPONENT_DEBUG = 'debug';

/**
* Query component spatial.
*/
const COMPONENT_SPATIAL = 'spatial';

/**
* Default options.
*
Expand Down Expand Up @@ -166,6 +171,7 @@ class Query extends BaseQuery
self::COMPONENT_DISTRIBUTEDSEARCH => 'Solarium\QueryType\Select\Query\Component\DistributedSearch',
self::COMPONENT_STATS => 'Solarium\QueryType\Select\Query\Component\Stats\Stats',
self::COMPONENT_DEBUG => 'Solarium\QueryType\Select\Query\Component\Debug',
self::COMPONENT_SPATIAL => 'Solarium\QueryType\Select\Query\Component\Spatial',
);

/**
Expand Down Expand Up @@ -1057,6 +1063,18 @@ public function setTags($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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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;
}
}
10 changes: 10 additions & 0 deletions tests/Solarium/Tests/QueryType/Select/Query/AbstractQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,14 @@ public function testSetTags()
$this->query->setTags(array('t3', 't4'));
$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)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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()
);

}
}

0 comments on commit 47b03a1

Please sign in to comment.