Skip to content

Commit 70837fc

Browse files
committed
Move columnSearch code to a seperate private function.
1 parent 2395726 commit 70837fc

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

src/OpenSkill/Datatable/Queries/Parser/Datatable110QueryParser.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,7 @@ public function parse(Request $request, array $columnConfiguration)
4949

5050
$this->getOrder($query, $builder, $columnConfiguration);
5151

52-
// for each column we need to see if there is a search value
53-
foreach ($columnConfiguration as $i => $c) {
54-
// check if there is something search related
55-
if ($c->getSearch()->isSearchable() &&
56-
!$this->isEmpty($query->get("columns[" . $i . "][search][value]", null, true))) {
57-
// search for this column is available
58-
$builder->columnSearch($c->getName(), $query->get("columns[" . $i . "][search][value]", null, true));
59-
}
60-
}
52+
$this->getSearchColumns($query, $builder, $columnConfiguration);
6153

6254
return $builder->build();
6355
}
@@ -72,6 +64,25 @@ private function isEmpty($string)
7264
return empty($string);
7365
}
7466

67+
/**
68+
* Helper function that will check if an array key exists
69+
* @param mixed $array
70+
* @param string $key key to check
71+
* @return bool true if array & exists, false otherwise
72+
*/
73+
private function isArrayAndHasKey($array, $key)
74+
{
75+
if (!is_array($array)) {
76+
return false;
77+
}
78+
79+
if (array_key_exists($key, $array)) {
80+
return true;
81+
}
82+
83+
return false;
84+
}
85+
7586
/**
7687
* @param ParameterBag $query
7788
* @param QueryConfigurationBuilder $builder
@@ -111,8 +122,32 @@ public function getLength($query, $builder)
111122
*/
112123
public function getSearch($query, $builder)
113124
{
114-
if (!$this->isEmpty($query->get('search[value]', null, true))) {
115-
$builder->searchValue($query->get('search[value]', null, true));
125+
$search = $query->get('search');
126+
127+
if ($this->isArrayAndHasKey($search, 'value')) {
128+
$builder->searchValue($search['value'], null, true);
129+
}
130+
}
131+
132+
public function getSearchColumns($query, $builder, array $columnConfiguration)
133+
{
134+
// for each column we need to see if there is a search value
135+
$columns = $query->get('columns');
136+
137+
foreach ($columnConfiguration as $i => $c) {
138+
// check if there is something search related
139+
if (!isset($columns[$i])) {
140+
continue;
141+
}
142+
143+
if ($c->getSearch()->isSearchable()) {
144+
// search for this column is available
145+
$value = $columns[$i]['search']['value'];
146+
147+
if (!$this->isEmpty($value)) {
148+
$builder->columnSearch($c->getName(), $columns[$i]['search']['value'], null, true);
149+
}
150+
}
116151
}
117152
}
118153

@@ -122,8 +157,10 @@ public function getSearch($query, $builder)
122157
*/
123158
public function getRegex($query, $builder)
124159
{
125-
if (!$this->isEmpty($query->get('search[regex]', null, true))) {
126-
$builder->searchRegex($query->get('search[regex]', null, true));
160+
$search = $query->get('search');
161+
162+
if ($this->isArrayAndHasKey($search, 'regex')) {
163+
$builder->searchRegex($search['regex'], null, true);
127164
}
128165
}
129166

0 commit comments

Comments
 (0)