Commit acfbc18e authored by Albert Casademont's avatar Albert Casademont

Add multiple collations to the Spellcheck Response Parser

parent 7b57f3e2
...@@ -41,13 +41,16 @@ foreach($spellcheckResult as $suggestion) { ...@@ -41,13 +41,16 @@ foreach($spellcheckResult as $suggestion) {
echo '<hr/>'; echo '<hr/>';
} }
$collation = $spellcheckResult->getCollation(); $collations = $spellcheckResult->getCollations();
echo '<h1>Collation</h1>'; echo '<h1>Collations</h1>';
echo 'Query: '.$collation->getQuery().'<br/>'; foreach($collations as $collation) {
echo 'Hits: '.$collation->getHits().'<br/>'; echo 'Query: '.$collation->getQuery().'<br/>';
echo 'Hits: '.$collation->getHits().'<br/>';
echo '<hr/>';
}
echo 'Corrections:<br/>'; echo 'Corrections:<br/>';
foreach($collation->getCorrections() as $input => $correction) { foreach($collation->getCorrections() as $input => $correction) {
echo $input . ' => ' . $correction .'<br/>'; echo $input . ' => ' . $correction .'<br/>';
} }
htmlFooter(); htmlFooter();
\ No newline at end of file
...@@ -66,7 +66,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck ...@@ -66,7 +66,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
$suggestions = array(); $suggestions = array();
$correctlySpelled = null; $correctlySpelled = null;
$collation = null; $collations = array();
$index = 0; $index = 0;
while (isset($spellcheckResults[$index]) && isset($spellcheckResults[$index+1])) { while (isset($spellcheckResults[$index]) && isset($spellcheckResults[$index+1])) {
...@@ -78,7 +78,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck ...@@ -78,7 +78,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
$correctlySpelled = $value; $correctlySpelled = $value;
break; break;
case 'collation': case 'collation':
$collation = $this->_parseCollation($value); $collations[] = $this->_parseCollation($value);
break; break;
default: default:
$suggestions[] = $this->_parseSuggestion($key, $value); $suggestions[] = $this->_parseSuggestion($key, $value);
...@@ -87,7 +87,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck ...@@ -87,7 +87,7 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
$index +=2; $index +=2;
} }
return new Solarium_Result_Select_Spellcheck($suggestions, $collation, $correctlySpelled); return new Solarium_Result_Select_Spellcheck($suggestions, $collations, $correctlySpelled);
} else { } else {
return null; return null;
} }
...@@ -173,4 +173,4 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck ...@@ -173,4 +173,4 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
$numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency $numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency
); );
} }
} }
\ No newline at end of file
...@@ -53,11 +53,11 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -53,11 +53,11 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
protected $_suggestions; protected $_suggestions;
/** /**
* Collation object * Collations array
* *
* @var Solarium_Result_Select_Spellcheck_Collation * @var array
*/ */
protected $_collation; protected $_collations;
/** /**
* Correctly spelled? * Correctly spelled?
...@@ -70,25 +70,35 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -70,25 +70,35 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
* Constructor * Constructor
* *
* @param array $suggestions * @param array $suggestions
* @param Solarium_Result_Select_Spellcheck_Collation $collation * @param array $collations
* @param boolean $correctlySpelled * @param boolean $correctlySpelled
* @return void * @return void
*/ */
public function __construct($suggestions, $collation, $correctlySpelled) public function __construct($suggestions, $collations, $correctlySpelled)
{ {
$this->_suggestions = $suggestions; $this->_suggestions = $suggestions;
$this->_collation = $collation; $this->_collations = $collations;
$this->_correctlySpelled = $correctlySpelled; $this->_correctlySpelled = $correctlySpelled;
} }
/** /**
* Get the collation result * Get the first collation result
* *
* @return Solarium_Result_Select_Spellcheck_Collation * @return Solarium_Result_Select_Spellcheck_Collation
*/ */
public function getCollation() public function getCollation()
{ {
return $this->_collation; return empty($this->_collations) ? null : reset($this->_collations);
}
/**
* Get all collations
*
* @return array
*/
public function getCollations()
{
return $this->_collations;
} }
/** /**
...@@ -147,4 +157,4 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -147,4 +157,4 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
{ {
return count($this->_suggestions); return count($this->_suggestions);
} }
} }
\ No newline at end of file
...@@ -85,6 +85,21 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP ...@@ -85,6 +85,21 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP
3 => 'ultrasharp' 3 => 'ultrasharp'
), ),
), ),
8 => 'collation',
9 => array (
0 => 'collationQuery',
1 => 'dell ultrasharp new',
2 => 'hits',
3 => 0,
4 => 'misspellingsAndCorrections',
5 => array (
0 => 'delll',
1 => 'dell',
2 => 'ultrashar',
3 => 'ultrasharp'
),
),
) )
) )
); );
...@@ -95,6 +110,9 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP ...@@ -95,6 +110,9 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP
$this->assertEquals(false, $result->getCorrectlySpelled()); $this->assertEquals(false, $result->getCorrectlySpelled());
$this->assertEquals('dell', $suggestions[0]->getWord()); $this->assertEquals('dell', $suggestions[0]->getWord());
$this->assertEquals('dell ultrasharp', $result->getCollation()->getQuery()); $this->assertEquals('dell ultrasharp', $result->getCollation()->getQuery());
$collations = $result->getCollations();
$this->assertEquals('dell ultrasharp', $collations[0]->getQuery());
$this->assertEquals('dell ultrasharp new', $collations[1]->getQuery());
} }
public function testParse() public function testParse()
...@@ -129,6 +147,8 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP ...@@ -129,6 +147,8 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP
5 => false, 5 => false,
6 => 'collation', 6 => 'collation',
7 => 'dell ultrasharp', 7 => 'dell ultrasharp',
8 => 'collation',
9 => 'dell ultrasharp new',
) )
) )
); );
...@@ -139,6 +159,10 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP ...@@ -139,6 +159,10 @@ class Solarium_Client_ResponseParser_Select_Component_SpellcheckTest extends PHP
$this->assertEquals(false, $result->getCorrectlySpelled()); $this->assertEquals(false, $result->getCorrectlySpelled());
$this->assertEquals('dell', $suggestions[0]->getWord()); $this->assertEquals('dell', $suggestions[0]->getWord());
$this->assertEquals('dell ultrasharp', $result->getCollation()->getQuery()); $this->assertEquals('dell ultrasharp', $result->getCollation()->getQuery());
$collations = $result->getCollations();
$this->assertEquals('dell ultrasharp', $collations[0]->getQuery());
$this->assertEquals('dell ultrasharp new', $collations[1]->getQuery());
} }
public function testParseNoData() public function testParseNoData()
......
...@@ -37,7 +37,7 @@ class Solarium_Result_Select_SpellcheckTest extends PHPUnit_Framework_TestCase ...@@ -37,7 +37,7 @@ class Solarium_Result_Select_SpellcheckTest extends PHPUnit_Framework_TestCase
*/ */
protected $_result; protected $_result;
protected $_suggestions, $_collation, $_correctlySpelled; protected $_suggestions, $_collations, $_correctlySpelled;
public function setUp() public function setUp()
{ {
...@@ -45,15 +45,19 @@ class Solarium_Result_Select_SpellcheckTest extends PHPUnit_Framework_TestCase ...@@ -45,15 +45,19 @@ class Solarium_Result_Select_SpellcheckTest extends PHPUnit_Framework_TestCase
'key1' => 'content1', 'key1' => 'content1',
'key2' => 'content2', 'key2' => 'content2',
); );
$this->_collation = 'dummy1'; $this->_collations = array(
'dummy1',
'dummy2',
);
$this->_correctlySpelled = false; $this->_correctlySpelled = false;
$this->_result = new Solarium_Result_Select_Spellcheck($this->_suggestions, $this->_collation, $this->_correctlySpelled); $this->_result = new Solarium_Result_Select_Spellcheck($this->_suggestions, $this->_collations, $this->_correctlySpelled);
} }
public function testGetCollation() public function testGetCollation()
{ {
$this->assertEquals($this->_collation, $this->_result->getCollation()); $this->assertEquals($this->_collations, $this->_result->getCollations());
$this->assertEquals(reset($this->_collations), $this->_result->getCollation());
} }
public function testGetCorrectlySpelled() public function testGetCorrectlySpelled()
......
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