Commit 13a7f96c authored by Bas de Nooijer's avatar Bas de Nooijer

Merge pull request #41 from theUniC/release/2.1.0

Now per field highlighting options can be added.
parents 18a6a874 3d0da19a
...@@ -52,7 +52,7 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting ...@@ -52,7 +52,7 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting
* @param Solarium_Client_Request $request * @param Solarium_Client_Request $request
* @return Solarium_Client_Request * @return Solarium_Client_Request
*/ */
public function build($component, $request) public function build(Solarium_Query_Select_Component_Highlighting $component, Solarium_Client_Request $request)
{ {
// enable highlighting // enable highlighting
$request->addParam('hl', 'true'); $request->addParam('hl', 'true');
...@@ -77,7 +77,27 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting ...@@ -77,7 +77,27 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting
$request->addParam('hl.regex.slop', $component->getRegexSlop()); $request->addParam('hl.regex.slop', $component->getRegexSlop());
$request->addParam('hl.regex.pattern', $component->getRegexPattern()); $request->addParam('hl.regex.pattern', $component->getRegexPattern());
$request->addParam('hl.regex.maxAnalyzedChars', $component->getRegexMaxAnalyzedChars()); $request->addParam('hl.regex.maxAnalyzedChars', $component->getRegexMaxAnalyzedChars());
$fieldOptions = $component->getPerFieldOptions();
if (sizeof($fieldOptions)) {
$this->_buildPerField($fieldOptions, $request);
}
return $request; return $request;
} }
/**
* Set the per field highlighting options
*
* @param array $fieldOptions
* @param Solarium_Client_Request $request
*/
protected function _buildPerField(array $fieldOptions, Solarium_Client_Request $request)
{
foreach ($fieldOptions as $field => $options) {
foreach ($options as $option => $value) {
$request->addParam('f.' . $field . '.hl.' . $option, $value, true);
}
}
}
} }
\ No newline at end of file
...@@ -62,6 +62,20 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select ...@@ -62,6 +62,20 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select
* @var string * @var string
*/ */
protected $_type = Solarium_Query_Select::COMPONENT_HIGHLIGHTING; protected $_type = Solarium_Query_Select::COMPONENT_HIGHLIGHTING;
/**
* Specifies the field over the highlight will be set
*
* @var string
*/
protected $_overField;
/**
* The options applied to specific fields
*
* @var array
*/
protected $_fieldOptions = array();
/** /**
* Set fields option * Set fields option
...@@ -505,4 +519,66 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select ...@@ -505,4 +519,66 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select
return $this->getOption('regexmaxanalyzedchars'); return $this->getOption('regexmaxanalyzedchars');
} }
/**
* Stop setting per field options
*/
public function endPerFieldOptions()
{
$this->_overField = null;
}
/**
* Returns the array with all the specific per field options
*
* @return array
*/
public function getPerFieldOptions()
{
return $this->_fieldOptions;
}
/**
* Override the _setOption method to take in account the per field options
*
* @param string $name
* @param mixed $value
*/
protected function _setOption($name, $value)
{
if (null !== $this->_overField) {
$this->_fieldOptions[$this->_overField][$name] = $value;
} else {
parent::_setOption($name, $value);
}
return $this;
}
/**
* Checks if a given field exists on the lists field. If not, it will
* be added
*
* @param string $fieldName
*/
protected function _checkField($fieldName)
{
if(strstr($this->getFields(), $fieldName) === false) {
$this->setFields($this->getFields() . (strlen($this->getFields()) > 0 ? ',' : '') . $fieldName);
}
}
/**
* This magic method implementation, sets the field over the highlight options
* will be applied
*
* @param string $name
* @return Solarium_Query_Select_Component_Highlighting
*/
public function __get($name)
{
$this->_checkField($name);
$this->_overField = $name;
return $this;
}
} }
\ No newline at end of file
...@@ -59,6 +59,11 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P ...@@ -59,6 +59,11 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
$component->setRegexPattern('mypattern'); $component->setRegexPattern('mypattern');
$component->setMaxAnalyzedChars(100); $component->setMaxAnalyzedChars(100);
// Per field
$component->field->setSnippets(20)
->setFragsize(200)
->endPerFieldOptions();
$request = $builder->build($component, $request); $request = $builder->build($component, $request);
$this->assertEquals( $this->assertEquals(
...@@ -83,6 +88,8 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P ...@@ -83,6 +88,8 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
'hl.highlightMultiTerm' => 'true', 'hl.highlightMultiTerm' => 'true',
'hl.regex.slop' => 1.3, 'hl.regex.slop' => 1.3,
'hl.regex.pattern' => 'mypattern', 'hl.regex.pattern' => 'mypattern',
'f.field.hl.snippets' => 20,
'f.field.hl.fragsize' => 200
), ),
$request->getParams() $request->getParams()
); );
......
...@@ -315,4 +315,31 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework ...@@ -315,4 +315,31 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework
); );
} }
public function testNonExistentPropertiesWillReturnTheComponentItself()
{
$this->assertInstanceOf('Solarium_Query_Select_Component', $this->_hlt->field);
}
/**
* @depends testNonExistentPropertiesWillReturnTheComponentItself
*/
public function testNonExistentPropertiesWillBeAddedToTheFieldsList()
{
$this->_hlt->field->endPerFieldOptions();
$this->assertContains('field', $this->_hlt->getFields());
}
public function testMethodsCalledAfterUnexistendPropertySetWillBeAddedAsAFieldSpecificOption()
{
$this->_hlt->field->setSnippets(20)->setFragSize(200)->endPerFieldOptions();
$fieldOptions = $this->_hlt->getPerFieldOptions();
$this->assertArrayHasKey('field', $fieldOptions);
$this->assertInternalType('array', $fieldOptions['field']);
$this->assertArrayHasKey('snippets', $fieldOptions['field']);
$this->assertArrayHasKey('fragsize', $fieldOptions['field']);
$this->assertEquals(20, $fieldOptions['field']['snippets']);
$this->assertEquals(200, $fieldOptions['field']['fragsize']);
}
} }
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