|
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\Search; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\Search; |
|
16
|
|
|
use ONGR\FilterManagerBundle\Filter\FilterInterface; |
|
17
|
|
|
use ONGR\FilterManagerBundle\Relation\FilterIterator; |
|
18
|
|
|
use ONGR\FilterManagerBundle\Relation\RelationInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This class holds collection of FilterInterface objects labeled by name. |
|
24
|
|
|
*/ |
|
25
|
|
|
class FilterContainer extends ParameterBag |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Cache |
|
29
|
|
|
*/ |
|
30
|
|
|
private $cache; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $lifeTime; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $exclude = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function add(array $parameters = []) |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($parameters as $key => $value) { |
|
48
|
|
|
$this->set($key, $value); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function set($key, $value) |
|
56
|
|
|
{ |
|
57
|
|
|
if ($value instanceof FilterInterface) { |
|
58
|
|
|
parent::set($key, $value); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Sets array of filter names not to be cached |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $exclude |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setExclude(array $exclude) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->exclude = $exclude; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Filters accepted. |
|
74
|
|
|
* |
|
75
|
|
|
* @param RelationInterface $relation |
|
76
|
|
|
* |
|
77
|
|
|
* @return FilterInterface[]|FilterIterator |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getFiltersByRelation(RelationInterface $relation) |
|
80
|
|
|
{ |
|
81
|
|
|
return new FilterIterator($this->getIterator(), $relation); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Builds search request according to given filters. |
|
86
|
|
|
* |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* |
|
89
|
|
|
* @return SearchRequest |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function buildSearchRequest(Request $request) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$search = new SearchRequest(); |
|
94
|
|
|
/** @var FilterInterface[] $filters */ |
|
95
|
|
|
$filters = $this->all(); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($filters as $name => $filter) { |
|
98
|
|
|
$state = $filter->getState($request); |
|
99
|
|
|
$state->setName($name); |
|
100
|
|
|
$search->set($name, $state); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $search; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Builds elastic search query by given SearchRequest and filters. |
|
108
|
|
|
* |
|
109
|
|
|
* @param SearchRequest $request |
|
110
|
|
|
* @param \ArrayIterator|null $filters |
|
111
|
|
|
* |
|
112
|
|
|
* @return Search |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public function buildSearch(SearchRequest $request, $filters = null) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$search = new Search(); |
|
117
|
|
|
|
|
118
|
|
|
/** @var FilterInterface[] $filters */ |
|
119
|
|
|
$filters = $filters ? $filters : $this->all(); |
|
120
|
|
|
foreach ($filters as $name => $filter) { |
|
121
|
|
|
$filter->modifySearch($search, $request->get($name), $request); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $search; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.