1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\AdminList; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminListBundle\AdminList\FilterType\FilterTypeInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* AdminListFilter |
10
|
|
|
*/ |
11
|
|
|
class FilterBuilder |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $filterDefinitions = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Filter[] |
20
|
|
|
*/ |
21
|
|
|
private $currentFilters = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $currentParameters = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $columnName The column name |
30
|
|
|
* @param FilterTypeInterface $type The filter type |
|
|
|
|
31
|
|
|
* @param string $filterName The name of the filter |
|
|
|
|
32
|
|
|
* @param array $options Options |
33
|
|
|
* |
34
|
|
|
* @return FilterBuilder |
35
|
|
|
*/ |
36
|
10 |
|
public function add($columnName, FilterTypeInterface $type = null, $filterName = null, array $options = array()) |
37
|
|
|
{ |
38
|
10 |
|
$this->filterDefinitions[$columnName] = array( |
39
|
10 |
|
'type' => $type, |
40
|
10 |
|
'options' => $options, |
41
|
10 |
|
'filtername' => $filterName, |
42
|
|
|
); |
43
|
|
|
|
44
|
10 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $columnName |
49
|
|
|
* |
50
|
|
|
* @return mixed|null |
51
|
|
|
*/ |
52
|
2 |
|
public function get($columnName) |
53
|
|
|
{ |
54
|
2 |
|
if (isset($this->filterDefinitions[$columnName])) { |
55
|
2 |
|
return $this->filterDefinitions[$columnName]; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $columnName |
63
|
|
|
* |
64
|
|
|
* @return FilterBuilder |
65
|
|
|
*/ |
66
|
1 |
|
public function remove($columnName) |
67
|
|
|
{ |
68
|
1 |
|
if (isset($this->filterDefinitions[$columnName])) { |
69
|
1 |
|
unset($this->filterDefinitions[$columnName]); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $columnName |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
1 |
|
public function has($columnName) |
81
|
|
|
{ |
82
|
1 |
|
return isset($this->filterDefinitions[$columnName]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
4 |
|
public function getFilterDefinitions() |
89
|
|
|
{ |
90
|
4 |
|
return $this->filterDefinitions; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Request $request |
95
|
|
|
*/ |
96
|
1 |
|
public function bindRequest(Request $request) |
97
|
|
|
{ |
98
|
1 |
|
$filterBuilderName = 'filter_' . $request->get('_route'); |
99
|
|
|
|
100
|
1 |
|
$this->currentParameters = $request->query->all(); |
101
|
1 |
|
if (\count($this->currentParameters) === 0) { |
102
|
1 |
|
if (!$request->query->has('filter') && $request->getSession()->has($filterBuilderName)) { |
103
|
|
|
$savedQuery = $request->getSession()->get($filterBuilderName); |
104
|
|
|
$request->query->replace($savedQuery); |
105
|
1 |
|
$this->currentParameters = $savedQuery; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$request->getSession()->set($filterBuilderName, $this->currentParameters); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$filterColumnNames = $request->query->get('filter_columnname'); |
112
|
1 |
|
if (isset($filterColumnNames)) { |
113
|
|
|
$uniqueIds = $request->query->get('filter_uniquefilterid'); |
114
|
|
|
$index = 0; |
115
|
|
|
foreach ($filterColumnNames as $filterColumnName) { |
116
|
|
|
$uniqueId = $uniqueIds[$index]; |
117
|
|
|
$filter = new Filter($filterColumnName, $this->get($filterColumnName), $uniqueId); |
118
|
|
|
$this->currentFilters[] = $filter; |
119
|
|
|
$filter->bindRequest($request); |
120
|
|
|
++$index; |
121
|
|
|
} |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
1 |
|
public function getCurrentParameters() |
129
|
|
|
{ |
130
|
1 |
|
return $this->currentParameters; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Filter[] |
135
|
|
|
*/ |
136
|
5 |
|
public function getCurrentFilters() |
137
|
|
|
{ |
138
|
5 |
|
return $this->currentFilters; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.