1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\FilterManagerBundle\Filter\Widget\Range; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\StatsAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Search; |
16
|
|
|
use ONGR\ElasticsearchBundle\Result\DocumentIterator; |
17
|
|
|
use ONGR\FilterManagerBundle\Filter\FilterState; |
18
|
|
|
use ONGR\FilterManagerBundle\Filter\ViewData; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Range filter, selects documents from lower limit to upper limit. |
23
|
|
|
*/ |
24
|
|
|
class Range extends AbstractRange |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function getState(Request $request) |
30
|
|
|
{ |
31
|
|
|
$state = parent::getState($request); |
32
|
|
|
|
33
|
|
|
if (!$state->isActive()) { |
34
|
|
|
return $state; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$values = explode(';', $state->getValue(), 2); |
38
|
|
|
|
39
|
|
|
if (count($values) < 2) { |
40
|
|
|
$state->setActive(false); |
41
|
|
|
|
42
|
|
|
return $state; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$gt = $this->isInclusive() ? 'gte' : 'gt'; |
46
|
|
|
$lt = $this->isInclusive() ? 'lte' : 'lt'; |
47
|
|
|
|
48
|
|
|
$normalized = []; |
49
|
|
|
$values[0] == '-' ? : $normalized[$gt] = floatval($values[0]); |
50
|
|
|
$values[1] == '-' ? : $normalized[$lt] = floatval($values[1]); |
51
|
|
|
|
52
|
|
|
if (empty($normalized)) { |
53
|
|
|
$state->setActive(false); |
54
|
|
|
|
55
|
|
|
return $state; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$state->setValue($normalized); |
59
|
|
|
|
60
|
|
|
return $state; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null) |
67
|
|
|
{ |
68
|
|
|
$stateAgg = new StatsAggregation($state->getName()); |
|
|
|
|
69
|
|
|
$stateAgg->setField($this->getField()); |
70
|
|
|
$search->addAggregation($stateAgg); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getViewData(DocumentIterator $result, ViewData $data) |
77
|
|
|
{ |
78
|
|
|
$name = $data->getState()->getName(); |
79
|
|
|
/** @var $data ViewData\RangeAwareViewData */ |
80
|
|
|
$data->setMinBounds($result->getAggregation($name)['min']); |
81
|
|
|
$data->setMaxBounds($result->getAggregation($name)['max']); |
82
|
|
|
|
83
|
|
|
return $data; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: