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 ONGR\FilterManagerBundle\Filter\ViewData\RangeAwareViewData; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Date range filter, selects documents from lower date to upper date. |
24
|
|
|
*/ |
25
|
|
|
class DateRange extends AbstractRange |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getState(Request $request) |
31
|
|
|
{ |
32
|
|
|
$state = parent::getState($request); |
33
|
|
|
|
34
|
|
|
if ($state->getValue()) { |
35
|
|
|
$values = explode(';', $state->getValue(), 2); |
36
|
|
|
$gt = $this->isInclusive() ? 'gte' : 'gt'; |
37
|
|
|
$lt = $this->isInclusive() ? 'lte' : 'lt'; |
38
|
|
|
|
39
|
|
|
$normalized[$gt] = $values[0]; |
|
|
|
|
40
|
|
|
$normalized[$lt] = $values[1]; |
41
|
|
|
|
42
|
|
|
$state->setValue($normalized); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $state; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null) |
52
|
|
|
{ |
53
|
|
|
$stateAgg = new StatsAggregation('date_range_agg'); |
54
|
|
|
$stateAgg->setField($this->getField()); |
55
|
|
|
$search->addAggregation($stateAgg); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getViewData(DocumentIterator $result, ViewData $data) |
62
|
|
|
{ |
63
|
|
|
/** @var $data RangeAwareViewData */ |
64
|
|
|
$data->setMinBounds( |
65
|
|
|
new \DateTime('@' . (int) ($result->getAggregation('date_range_agg')->getValue()['min'] / 1000)) |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$data->setMaxBounds( |
69
|
|
|
new \DateTime('@' . (int) ($result->getAggregation('date_range_agg')->getValue()['max'] / 1000)) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.