Commit 3c35919d authored by nchar's avatar nchar Committed by StryKaizer

Issue #2929185 by nchar, borisson_, StryKaizer: Add minimum and maximum values...

Issue #2929185 by nchar, borisson_, StryKaizer: Add minimum and maximum values to granularity processor
parent fa65edc7
......@@ -131,3 +131,9 @@ plugin.plugin_configuration.facets_processor.granularity_item:
granularity:
type: integer
label: 'Granularity'
min_value:
type: integer
label: 'Minimum value'
max_value:
type: integer
label: 'Maximum value'
......@@ -34,6 +34,8 @@ class GranularItemProcessor extends ProcessorPluginBase implements BuildProcesso
public function defaultConfiguration() {
return [
'granularity' => 1,
'min_value' => '',
'max_value' => '',
] + parent::defaultConfiguration();
}
......@@ -51,6 +53,22 @@ class GranularItemProcessor extends ProcessorPluginBase implements BuildProcesso
'#description' => $this->t('The numeric size of the steps to group the result facets in.'),
];
$build['min_value'] = [
'#type' => 'number',
'#title' => $this->t('Minimum value'),
'#default_value' => $configuration['min_value'],
'#description' => $this->t('If the minimum value is left empty it will be calculated by the search result'),
'#size' => 10,
];
$build['max_value'] = [
'#type' => 'number',
'#title' => $this->t('Maximum value'),
'#default_value' => $configuration['max_value'],
'#description' => $this->t('If the maximum value is left empty it will be calculated by the search result'),
'#size' => 10,
];
return $build;
}
......
......@@ -32,9 +32,18 @@ class SearchApiGranular extends QueryTypeRangeBase {
*/
public function calculateResultFilter($value) {
assert($this->getGranularity() > 0);
$min_value = (int) $this->getMinValue();
$max_value = $this->getMaxValue();
$granularity = $this->getGranularity();
if ($value < $min_value || (!empty($max_value) && $value > ($max_value + $granularity - 1))) {
return FALSE;
}
return [
'display' => $value - ($value % $this->getGranularity()),
'raw' => $value - ($value % $this->getGranularity()) ,
'display' => $value - fmod($value - $min_value, $this->getGranularity()),
'raw' => $value - fmod($value - $min_value, $this->getGranularity()),
];
}
......@@ -59,4 +68,28 @@ class SearchApiGranular extends QueryTypeRangeBase {
return $this->facet->getProcessors()['granularity_item']->getConfiguration()['granularity'];
}
/**
* Looks at the configuration for this facet to determine the min value.
*
* Default behaviour an integer for the minimum value of the facets.
*
* @return mixed
* It can be a number or an empty value.
*/
protected function getMinValue() {
return $this->facet->getProcessors()['granularity_item']->getConfiguration()['min_value'];
}
/**
* Looks at the configuration for this facet to determine the max value.
*
* Default behaviour an integer for the maximum value of the facets.
*
* @return mixed
* It can be a number or an empty value.
*/
protected function getMaxValue() {
return $this->facet->getProcessors()['granularity_item']->getConfiguration()['max_value'];
}
}
......@@ -95,14 +95,15 @@ abstract class QueryTypeRangeBase extends QueryTypePluginBase {
// defined by self::calculateResultFilter().
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
$result_filter = $this->calculateResultFilter(trim($result['filter'], '"'));
if (isset($facet_results[$result_filter['raw']])) {
$facet_results[$result_filter['raw']]->setCount(
$facet_results[$result_filter['raw']]->getCount() + $count
);
}
else {
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
if ($result_filter = $this->calculateResultFilter(trim($result['filter'], '"'))) {
if (isset($facet_results[$result_filter['raw']])) {
$facet_results[$result_filter['raw']]->setCount(
$facet_results[$result_filter['raw']]->getCount() + $count
);
}
else {
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
}
}
}
}
......
......@@ -133,4 +133,53 @@ class SearchApiGranularTest extends UnitTestCase {
$this->assertEmpty($results);
}
/**
* Tests the calculateResultFilter method.
*
* @dataProvider provideDataForCalculateResultFilter
*/
public function testCalculateResultFilter($input, $expected_result) {
$query = new SearchApiQuery([], 'search_api_query', []);
$facet = new Facet(
['query_operator' => 'AND', 'widget' => 'links'],
'facets_facet'
);
$facet->addProcessor([
'processor_id' => 'granularity_item',
'weights' => [],
'settings' => [],
]);
$facet->getProcessors()['granularity_item']->setConfiguration([
'granularity' => 3,
'min_value' => 5,
'max_value' => 15,
]);
$query_type = new SearchApiGranular(
[
'facet' => $facet,
'query' => $query,
],
'search_api_string',
[]
);
$result = $query_type->calculateResultFilter($input);
$this->assertSame($expected_result, $result);
}
/**
* Provides testdata.
*
* @return array
* Test data.
*/
public function provideDataForCalculateResultFilter() {
return [
'normal' => [10, ['display' => 8.0, 'raw' => 8.0]],
'under_min' => [4, FALSE],
'over_max' => [20, FALSE],
];
}
}
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