1
|
|
|
<?php |
2
|
|
|
namespace Netdudes\DataSourceryBundle\Query; |
3
|
|
|
|
4
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field; |
5
|
|
|
|
6
|
|
|
class SearchTextFilterReducer |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var Field[] |
10
|
|
|
*/ |
11
|
|
|
private $dataSourceFields; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param Field[] $dataSourceFields |
15
|
|
|
*/ |
16
|
|
|
public function __construct(array $dataSourceFields) |
17
|
|
|
{ |
18
|
|
|
$this->dataSourceFields = $dataSourceFields; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Filter $filterToReduce |
23
|
|
|
* |
24
|
|
|
* @return Filter |
25
|
|
|
*/ |
26
|
|
|
public function reduceToFilterCondition(Filter $filterToReduce) |
27
|
|
|
{ |
28
|
|
|
$reducedFilter = new Filter(); |
29
|
|
|
$reducedFilter->setConditionType($filterToReduce->getConditionType()); |
30
|
|
|
foreach ($filterToReduce as $filterOrFilterCondition) { |
31
|
|
|
if ($filterOrFilterCondition instanceof Filter) { |
32
|
|
|
$reducedFilter[] = $this->reduceToFilterCondition($filterOrFilterCondition); |
33
|
|
|
} elseif ($filterOrFilterCondition instanceof FilterCondition) { |
34
|
|
|
if ($filterOrFilterCondition->isSearchText()) { |
35
|
|
|
$filterOrFilterCondition = $this->reduceSearchTextFilterCondition($filterOrFilterCondition); |
36
|
|
|
} |
37
|
|
|
$reducedFilter[] = $filterOrFilterCondition; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $reducedFilter; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param FilterCondition $filterCondition |
46
|
|
|
* |
47
|
|
|
* @return Filter |
48
|
|
|
*/ |
49
|
|
|
private function reduceSearchTextFilterCondition(FilterCondition $filterCondition) |
50
|
|
|
{ |
51
|
|
|
$method = $filterCondition->getMethod(); |
52
|
|
|
$searchTerm = $filterCondition->getValue(); |
53
|
|
|
if ($method === FilterCondition::METHOD_STRING_LIKE) { |
54
|
|
|
$searchTerm = "%$searchTerm%"; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$filter = new Filter(); |
58
|
|
|
$filter->setConditionType(Filter::CONDITION_TYPE_OR); |
59
|
|
|
foreach ($this->dataSourceFields as $dataSourceField) { |
60
|
|
|
if (!$dataSourceField->getDataType()->supports($method)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
if (!$dataSourceField->getDatabaseFilterQueryField()) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
if (is_array($dataSourceField->getDatabaseSelectAlias())) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$filter[] = new FilterCondition($dataSourceField->getUniqueName(), $method, $searchTerm, $searchTerm); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $filter; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|