Commit f328d654 authored by borisson_'s avatar borisson_

Issue #2600402 by mr.baileys, borisson_: Split ResultInterface::getValue()...

Issue #2600402 by mr.baileys, borisson_: Split ResultInterface::getValue() into getRawValue() and getDisplayValue()
parent deeb1499
......@@ -356,7 +356,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
// set the results which are active to active.
if (count($this->active_values)) {
foreach ($this->results as $result) {
if (in_array($result->getValue(), $this->active_values)) {
if (in_array($result->getRawValue(), $this->active_values)) {
$result->setActiveState(TRUE);
}
}
......
......@@ -33,11 +33,11 @@ class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements
}
protected static function sortDisplayValueAsc(Result $a, Result $b) {
return strnatcasecmp($a->getValue(), $b->getValue());
return strnatcasecmp($a->getDisplayValue(), $b->getDisplayValue());
}
protected static function sortDisplayValueDesc(Result $a, Result $b) {
return strnatcasecmp($b->getValue(), $a->getValue());
return strnatcasecmp($b->getDisplayValue(), $a->getDisplayValue());
}
}
......@@ -67,7 +67,7 @@ class QueryTypeTerm extends QueryTypePluginBase {
$facet_results = array();
foreach ($this->results as $result) {
if ($result['count']) {
$facet_results[] = new Result(trim($result['filter'], '"'), $result['count']);
$facet_results[] = new Result($result['filter'], trim($result['filter'], '"'), $result['count']);
}
}
$this->facet->setResults($facet_results);
......
......@@ -19,6 +19,8 @@ interface BuildProcessorInterface extends ProcessorInterface {
*
* @param \Drupal\facetapi\FacetInterface $facet
* @param \Drupal\facetapi\Result\Result[] $results
*
* @return \Drupal\facetapi\Result\Result[] $results
*/
public function build(FacetInterface $facet, array $results);
......
......@@ -8,14 +8,17 @@ namespace Drupal\facetapi\Result;
use Drupal\Core\Url;
class Result implements ResultInterface{
class Result implements ResultInterface {
/**
* The facet value
*
* @var
*/
protected $value;
protected $display_value;
/**
* The raw facet value
*/
protected $raw_value;
/**
* The facet count.
......@@ -32,48 +35,55 @@ class Result implements ResultInterface{
protected $url;
/**
* Wether the value is active (selected) or not.
* Is this a selected value or not.
*
* @var bool
*/
protected $active = FALSE;
function __construct($value, $count) {
$this->value = $value;
/**
* Construct a new instance of the value object.
*
* @param $raw_value
* @param $display_value
* @param $count
*/
function __construct($raw_value, $display_value, $count) {
$this->raw_value = $raw_value;
$this->display_value = $display_value;
$this->count = $count;
}
/**
* Gets the value.
*
* @return mixed
* {@inheritdoc}
*/
public function getValue() {
return $this->value;
public function getDisplayValue() {
return $this->display_value;
}
/**
* Gets the count.
*
* @return int
* {@inheritdoc}
*/
public function getRawValue() {
return $this->raw_value;
}
/**
* {@inheritdoc}
*/
public function getCount() {
return $this->count;
}
/**
* Gets the url.
*
* @return \Drupal\Core\Url
* {@inheritdoc}
*/
public function getUrl() {
return $this->url;
}
/**
* Sets the url.
*
* @param \Drupal\Core\Url $url
* {@inheritdoc}
*/
public function setUrl(Url $url) {
$this->url = $url;
......@@ -86,7 +96,6 @@ class Result implements ResultInterface{
$this->active = $active;
}
/**
* @inheritdoc
*/
......@@ -94,5 +103,4 @@ class Result implements ResultInterface{
return $this->active;
}
}
\ No newline at end of file
}
......@@ -16,7 +16,14 @@ interface ResultInterface {
*
* @return string
*/
public function getValue();
public function getRawValue();
/**
* Get the display value as present in the index.
*
* @return string
*/
public function getDisplayValue();
/**
* Get the count for the result.
......@@ -53,4 +60,4 @@ interface ResultInterface {
*/
public function isActive();
}
\ No newline at end of file
}
......@@ -38,9 +38,9 @@ class CountWidgetOrderProcessorTest extends UnitTestCase {
parent::setUp();
$this->original_results = [
new Result('llama', 10),
new Result('badger', 5),
new Result('duck', 15),
new Result('llama', 'llama', 10),
new Result('badger', 'badger', 5),
new Result('duck', 'duck', 15),
];
$this->processor = new CountWidgetOrderProcessor([], 'count_widget_order', []);
......@@ -54,11 +54,11 @@ class CountWidgetOrderProcessorTest extends UnitTestCase {
$sorted_results = $this->processor->sortResults($this->original_results, 'ASC');
$this->assertEquals(5, $sorted_results[0]->getCount());
$this->assertEquals('badger', $sorted_results[0]->getValue());
$this->assertEquals('badger', $sorted_results[0]->getDisplayValue());
$this->assertEquals(10, $sorted_results[1]->getCount());
$this->assertEquals('llama', $sorted_results[1]->getValue());
$this->assertEquals('llama', $sorted_results[1]->getDisplayValue());
$this->assertEquals(15, $sorted_results[2]->getCount());
$this->assertEquals('duck', $sorted_results[2]->getValue());
$this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
}
/**
......@@ -69,11 +69,11 @@ class CountWidgetOrderProcessorTest extends UnitTestCase {
$sorted_results = $this->processor->sortResults($this->original_results, 'DESC');
$this->assertEquals(15, $sorted_results[0]->getCount());
$this->assertEquals('duck', $sorted_results[0]->getValue());
$this->assertEquals('duck', $sorted_results[0]->getDisplayValue());
$this->assertEquals(10, $sorted_results[1]->getCount());
$this->assertEquals('llama', $sorted_results[1]->getValue());
$this->assertEquals('llama', $sorted_results[1]->getDisplayValue());
$this->assertEquals(5, $sorted_results[2]->getCount());
$this->assertEquals('badger', $sorted_results[2]->getValue());
$this->assertEquals('badger', $sorted_results[2]->getDisplayValue());
}
}
......@@ -38,13 +38,13 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
parent::setUp();
$this->original_results = [
new Result('thetans', 10),
new Result('xenu', 5),
new Result('Tom', 15),
new Result('Hubbard', 666),
new Result('FALSE', 1),
new Result('1977', 20),
new Result('2', 22),
new Result('thetans', 'thetans', 10),
new Result('xenu', 'xenu', 5),
new Result('Tom', 'Tom', 15),
new Result('Hubbard', 'Hubbard', 666),
new Result('FALSE', 'FALSE', 1),
new Result('1977', '1977', 20),
new Result('2', '2', 22),
];
$this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', []);
......@@ -57,7 +57,7 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
$sorted_results = $this->processor->sortResults($this->original_results, 'ASC');
$expected_values = array('2', '1977', 'FALSE', 'Hubbard', 'thetans', 'Tom', 'xenu');
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->getValue());
$this->assertEquals($value, $sorted_results[$index]->getDisplayValue());
}
}
......@@ -68,8 +68,21 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase {
$sorted_results = $this->processor->sortResults($this->original_results, 'DESC');
$expected_values = array_reverse(array('2', '1977', 'FALSE', 'Hubbard', 'thetans', 'Tom', 'xenu'));
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->getValue());
$this->assertEquals($value, $sorted_results[$index]->getDisplayValue());
}
}
public function testUseActualDisplayValue() {
$original = [
new Result('bb_test', 'Test AA', 10),
new Result('aa_test', 'Test BB', 10),
];
$sorted_results = $this->processor->sortResults($original, 'DESC');
$this->assertEquals('Test BB', $sorted_results[0]->getDisplayValue());
$this->assertEquals('Test AA', $sorted_results[1]->getDisplayValue());
}
}
......@@ -39,9 +39,9 @@ class MinimumCountProcessorTest extends UnitTestCase {
parent::setUp();
$this->original_results = [
new Result('llama', 10),
new Result('badger', 5),
new Result('duck', 15),
new Result('llama', 'llama', 10),
new Result('badger', 'badger', 5),
new Result('duck', 'duck', 15),
];
$this->processor = new MinimumCountProcessor([], 'minimum_count', []);
......@@ -65,9 +65,9 @@ class MinimumCountProcessorTest extends UnitTestCase {
$this->assertCount(2, $sorted_results);
$this->assertEquals(10, $sorted_results[0]->getCount());
$this->assertEquals('llama', $sorted_results[0]->getValue());
$this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
$this->assertEquals(15, $sorted_results[2]->getCount());
$this->assertEquals('duck', $sorted_results[2]->getValue());
$this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
}
}
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