Skip to content
Snippets Groups Projects
Commit ad00a26f authored by Sebastian Böttger's avatar Sebastian Böttger Committed by Demian Katz
Browse files

Make Solr\Backend::terms() more generic (#888)

- It is now easier to override hard-coded assumptions, and an alternate parameter format (with ParamBag first) is supported.
parent 1c62f446
No related merge requests found
......@@ -235,20 +235,43 @@ class Backend extends AbstractBackend
*
* @return Terms
*/
public function terms($field, $start, $limit, ParamBag $params = null)
{
public function terms($field = null, $start = null, $limit = null,
ParamBag $params = null
) {
// Support alternate syntax with ParamBag as first parameter:
if ($field instanceof ParamBag && $params === null) {
$params = $field;
$field = null;
}
// Create empty ParamBag if none provided:
$params = $params ?: new ParamBag();
$this->injectResponseWriter($params);
// Always enable terms:
$params->set('terms', 'true');
$params->set('terms.fl', $field);
$params->set('terms.lower', $start);
$params->set('terms.limit', $limit);
$params->set('terms.lower.incl', 'false');
$params->set('terms.sort', 'index');
// Use parameters if provided:
if (null !== $field) {
$params->set('terms.fl', $field);
}
if (null !== $start) {
$params->set('terms.lower', $start);
}
if (null !== $limit) {
$params->set('terms.limit', $limit);
}
// Set defaults unless overridden:
if (!$params->hasParam('terms.lower.incl')) {
$params->set('terms.lower.incl', 'false');
}
if (!$params->hasParam('terms.sort')) {
$params->set('terms.sort', 'index');
}
$response = $this->connector->terms($params);
$terms = new Terms($this->deserialize($response));
$terms = new Terms($this->deserialize($response));
return $terms;
}
......
......@@ -76,6 +76,18 @@ class ParamBag
return isset($this->params[$name]) ? $this->params[$name] : null;
}
/**
* Return true if the bag contains any value(s) for the specified parameter.
*
* @param string $name Parameter name
*
* @return bool
*/
public function hasParam($name)
{
return isset($this->params[$name]);
}
/**
* Return true if the bag contains a parameter-value-pair.
*
......
......@@ -143,6 +143,29 @@ class BackendTest extends PHPUnit_Framework_TestCase
$this->assertCount(10, $terms->getFieldTerms('author'));
}
/**
* Test terms component (using ParamBag as first param).
*
* @return void
*/
public function testTermsWithParamBagAsFirstParameter()
{
$resp = $this->loadResponse('terms');
$conn = $this->getConnectorMock(['query']);
$conn->expects($this->once())
->method('query')
->will($this->returnValue($resp->getBody()));
$back = new Backend($conn);
$back->setIdentifier('test');
$bag = new ParamBag();
$bag->set('terms.fl', 'author');
$bag->set('terms.lower', '');
$bag->set('terms.limit', '-1');
$terms = $back->terms($bag);
$this->assertTrue($terms->hasFieldTerms('author'));
$this->assertCount(10, $terms->getFieldTerms('author'));
}
/**
* Test handling of a bad JSON response.
*
......
......@@ -43,6 +43,33 @@ use PHPUnit_Framework_TestCase as TestCase;
*/
class ParamBagTest extends TestCase
{
/**
* Test "contains"
*
* @return void
*/
public function testContains()
{
$bag = new ParamBag();
$bag->set('foo', 'bar');
$this->assertTrue($bag->contains('foo', 'bar'));
$this->assertFalse($bag->contains('bar', 'foo'));
$this->assertFalse($bag->contains('foo', 'baz'));
}
/**
* Test "hasParam"
*
* @return void
*/
public function testHasParam()
{
$bag = new ParamBag();
$bag->set('foo', 'bar');
$this->assertTrue($bag->hasParam('foo'));
$this->assertFalse($bag->hasParam('bar'));
}
/**
* Test "remove"
*
......
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