1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable\Queries\Parser; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfiguration; |
7
|
|
|
use OpenSkill\Datatable\Queries\QueryConfiguration; |
8
|
|
|
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder; |
9
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
class Datatable110QueryParser extends QueryParser |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Method to determine if this parser can handle the query parameters. If so then the parser should return true |
17
|
|
|
* and be able to return a DTQueryConfiguration |
18
|
|
|
* |
19
|
|
|
* @param Request $request The current request, that should be investigated |
20
|
|
|
* @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration |
21
|
|
|
*/ |
22
|
|
|
public function canParse(Request $request) |
23
|
|
|
{ |
24
|
|
|
return $request->query->has("draw"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Method that should parse the request and return a DTQueryConfiguration |
29
|
|
|
* |
30
|
|
|
* @param Request $request The current request that should be investigated |
31
|
|
|
* @param ColumnConfiguration[] $columnConfiguration The configuration of the columns |
32
|
|
|
* @return QueryConfiguration the configuration the provider can use to prepare the data |
33
|
|
|
*/ |
34
|
|
|
public function parse(Request $request, array $columnConfiguration) |
35
|
|
|
{ |
36
|
|
|
$query = $request->query; |
37
|
|
|
$builder = QueryConfigurationBuilder::create(); |
38
|
|
|
|
39
|
|
|
$this->getDrawCall($query, $builder); |
40
|
|
|
|
41
|
|
|
$this->getStart($query, $builder); |
42
|
|
|
|
43
|
|
|
$this->getLength($query, $builder); |
44
|
|
|
|
45
|
|
|
$this->getSearch($query, $builder); |
46
|
|
|
|
47
|
|
|
$this->getRegex($query, $builder); |
48
|
|
|
|
49
|
|
|
$this->getOrder($query, $builder, $columnConfiguration); |
50
|
|
|
|
51
|
|
|
$this->getSearchColumns($query, $builder, $columnConfiguration); |
52
|
|
|
|
53
|
|
|
return $builder->build(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Helper function that will check if a variable is empty |
58
|
|
|
* @param mixed $string |
59
|
|
|
* @return bool true if empty, false otherwise |
60
|
|
|
*/ |
61
|
|
|
private function isEmpty($string) |
62
|
|
|
{ |
63
|
|
|
return empty($string); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Helper function that will check if an array key exists |
68
|
|
|
* @param mixed $array |
69
|
|
|
* @param string $key key to check |
70
|
|
|
* @return bool true if array & exists, false otherwise |
71
|
|
|
*/ |
72
|
|
|
private function isArrayAndHasKey($array, $key) |
73
|
|
|
{ |
74
|
|
|
if (!is_array($array)) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (array_key_exists($key, $array)) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ParameterBag $query |
87
|
|
|
* @param QueryConfigurationBuilder $builder |
88
|
|
|
*/ |
89
|
|
|
public function getDrawCall($query, $builder) |
90
|
|
|
{ |
91
|
|
|
if ($query->has('draw')) { |
92
|
|
|
$builder->drawCall($query->get('draw')); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ParameterBag $query |
98
|
|
|
* @param QueryConfigurationBuilder $builder |
99
|
|
|
*/ |
100
|
|
|
public function getStart($query, $builder) |
101
|
|
|
{ |
102
|
|
|
if ($query->has('start')) { |
103
|
|
|
$builder->start($query->get('start')); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param ParameterBag $query |
109
|
|
|
* @param QueryConfigurationBuilder $builder |
110
|
|
|
*/ |
111
|
|
|
public function getLength($query, $builder) |
112
|
|
|
{ |
113
|
|
|
if ($query->has('length')) { |
114
|
|
|
$builder->length($query->get('length')); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param ParameterBag $query |
120
|
|
|
* @param QueryConfigurationBuilder $builder |
121
|
|
|
*/ |
122
|
|
|
public function getSearch($query, $builder) |
123
|
|
|
{ |
124
|
|
|
$search = $query->get('search'); |
125
|
|
|
|
126
|
|
|
if ($this->isArrayAndHasKey($search, 'value')) { |
127
|
|
|
$builder->searchValue($search['value']); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param ParameterBag $query |
133
|
|
|
* @param QueryConfigurationBuilder $builder |
134
|
|
|
*/ |
135
|
|
|
public function getSearchColumns($query, $builder, array $columnConfiguration) |
136
|
|
|
{ |
137
|
|
|
// for each column we need to see if there is a search value |
138
|
|
|
$columns = $query->get('columns'); |
139
|
|
|
|
140
|
|
|
foreach ($columnConfiguration as $i => $c) { |
141
|
|
|
// check if there is something search related |
142
|
|
|
if (!isset($columns[$i])) { |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->addColumnSearchToBuilderIfRequested($columns, $builder, $c, $i); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array $columns incoming column request |
152
|
|
|
* @param QueryConfigurationBuilder $builder |
153
|
|
|
* @param ColumnConfiguration $column |
154
|
|
|
* @param integer $position position of the column in the columnConfiguration loop |
155
|
|
|
*/ |
156
|
|
|
private function addColumnSearchToBuilderIfRequested($columns, $builder, $column, $position) |
157
|
|
|
{ |
158
|
|
|
if ($column->getSearch()->isSearchable()) { |
159
|
|
|
// search for this column is available |
160
|
|
|
$value = $columns[$position]['search']['value']; |
161
|
|
|
|
162
|
|
|
if (!$this->isEmpty($value)) { |
163
|
|
|
$builder->columnSearch($column->getName(), $value); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param ParameterBag $query |
170
|
|
|
* @param QueryConfigurationBuilder $builder |
171
|
|
|
*/ |
172
|
|
|
public function getRegex($query, $builder) |
173
|
|
|
{ |
174
|
|
|
$search = $query->get('search'); |
175
|
|
|
|
176
|
|
|
if ($this->isArrayAndHasKey($search, 'regex')) { |
177
|
|
|
$builder->searchRegex($search['regex']); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param ParameterBag $query |
183
|
|
|
* @param QueryConfigurationBuilder $builder |
184
|
|
|
* @param ColumnConfiguration[] $columnConfiguration |
185
|
|
|
*/ |
186
|
|
|
private function getOrder(ParameterBag $query, QueryConfigurationBuilder $builder, array $columnConfiguration) |
187
|
|
|
{ |
188
|
|
|
//loop over the order |
189
|
|
|
if(($query->has('order'))) { |
190
|
|
|
$order = $query->get('order'); |
191
|
|
|
foreach($order as $i => $config) { |
192
|
|
|
if(array_key_exists($config['column'], $columnConfiguration)) { |
193
|
|
|
$column = $columnConfiguration[$config['column']]; |
194
|
|
|
if($column->getOrder()->isOrderable()) { |
195
|
|
|
$builder->columnOrder($column->getName(), $config['dir']); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|