1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: siim |
5
|
|
|
* Date: 28.01.19 |
6
|
|
|
* Time: 19:30 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Sf4\Api\Dto\Filter; |
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\QueryBuilder; |
12
|
|
|
|
13
|
|
|
class FilterQueryBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param QueryBuilder $queryBuilder |
17
|
|
|
* @param FilterItemInterface $filterItem |
18
|
|
|
* @param string $fieldName |
19
|
|
|
*/ |
20
|
|
|
public static function buildQuery(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
21
|
|
|
{ |
22
|
|
|
if(empty($filterItem->getValue())) { |
23
|
|
|
return ; |
24
|
|
|
} |
25
|
|
|
switch ($filterItem->getType()) { |
26
|
|
|
case FilterItemInterface::TYPE_LIKE: |
27
|
|
|
$expression = static::addLikeExpression($queryBuilder, $filterItem, $fieldName); |
28
|
|
|
break; |
29
|
|
|
case FilterItemInterface::TYPE_EQUAL: |
30
|
|
|
$expression = static::addEqualExpression($queryBuilder, $filterItem, $fieldName); |
31
|
|
|
break; |
32
|
|
|
case FilterItemInterface::TYPE_NOT_EQUAL: |
33
|
|
|
$expression = static::addNotEqualExpression($queryBuilder, $filterItem, $fieldName); |
34
|
|
|
break; |
35
|
|
|
case FilterItemInterface::TYPE_IN: |
36
|
|
|
$expression = static::addInExpression($queryBuilder, $filterItem, $fieldName); |
|
|
|
|
37
|
|
|
break; |
38
|
|
|
case FilterItemInterface::TYPE_NOT_IN: |
39
|
|
|
$expression = static::addNotInExpression($queryBuilder, $filterItem, $fieldName); |
|
|
|
|
40
|
|
|
break; |
41
|
|
|
case FilterItemInterface::TYPE_IS_NULL: |
42
|
|
|
$expression = static::addIsNullExpression($queryBuilder, $fieldName); |
43
|
|
|
break; |
44
|
|
|
case FilterItemInterface::TYPE_NOT_NULL: |
45
|
|
|
$expression = static::addIsNotNullExpression($queryBuilder, $fieldName); |
46
|
|
|
break; |
47
|
|
|
default: $expression = null; |
48
|
|
|
} |
49
|
|
|
if($expression) { |
50
|
|
|
$queryBuilder->andWhere( |
51
|
|
|
$expression |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param QueryBuilder $queryBuilder |
58
|
|
|
* @param FilterItemInterface $filterItem |
59
|
|
|
* @param string $fieldName |
60
|
|
|
* @return \Doctrine\ORM\Query\Expr\Comparison |
61
|
|
|
*/ |
62
|
|
|
protected static function addLikeExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
63
|
|
|
{ |
64
|
|
|
$paramKey = uniqid(':like_'); |
65
|
|
|
$queryBuilder->setParameter($paramKey, $filterItem->getValue()); |
66
|
|
|
return $queryBuilder->expr()->like($fieldName, $paramKey); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param QueryBuilder $queryBuilder |
71
|
|
|
* @param FilterItemInterface $filterItem |
72
|
|
|
* @param string $fieldName |
73
|
|
|
* @return \Doctrine\ORM\Query\Expr\Comparison |
74
|
|
|
*/ |
75
|
|
|
protected static function addEqualExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
76
|
|
|
{ |
77
|
|
|
$paramKey = uniqid(':eq_'); |
78
|
|
|
$queryBuilder->setParameter($paramKey, $filterItem->getValue()); |
79
|
|
|
return $queryBuilder->expr()->eq($fieldName, $paramKey); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param QueryBuilder $queryBuilder |
84
|
|
|
* @param FilterItemInterface $filterItem |
85
|
|
|
* @param string $fieldName |
86
|
|
|
* @return \Doctrine\ORM\Query\Expr\Comparison |
87
|
|
|
*/ |
88
|
|
|
protected static function addNotEqualExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
89
|
|
|
{ |
90
|
|
|
$paramKey = uniqid(':neq_'); |
91
|
|
|
$queryBuilder->setParameter($paramKey, $filterItem->getValue()); |
92
|
|
|
return $queryBuilder->expr()->neq($fieldName, $paramKey); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param QueryBuilder $queryBuilder |
97
|
|
|
* @param FilterItemInterface $filterItem |
98
|
|
|
* @param string $fieldName |
99
|
|
|
* @return \Doctrine\ORM\Query\Expr\Func|null |
100
|
|
|
*/ |
101
|
|
|
protected static function addInExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
102
|
|
|
{ |
103
|
|
|
$values = static::convertStringToArray($filterItem->getValue()); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if(true === is_array($values)) { |
|
|
|
|
106
|
|
|
return $queryBuilder->expr()->in($fieldName, $values); |
107
|
|
|
} |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param QueryBuilder $queryBuilder |
113
|
|
|
* @param FilterItemInterface $filterItem |
114
|
|
|
* @param string $fieldName |
115
|
|
|
* @return \Doctrine\ORM\Query\Expr\Func|null |
116
|
|
|
*/ |
117
|
|
|
protected static function addNotInExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName) |
118
|
|
|
{ |
119
|
|
|
$values = static::convertStringToArray($filterItem->getValue()); |
|
|
|
|
120
|
|
|
|
121
|
|
|
if(true === is_array($values)) { |
|
|
|
|
122
|
|
|
return $queryBuilder->expr()->notIn($fieldName, $values); |
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $string |
129
|
|
|
* @return array|null |
130
|
|
|
*/ |
131
|
|
|
protected static function convertStringToArray($string) |
132
|
|
|
{ |
133
|
|
|
if(false === is_array($string) && true === is_scalar($string)) { |
|
|
|
|
134
|
|
|
if(false !== strpos($string, ',')) { |
135
|
|
|
return explode(',', $string); |
136
|
|
|
} else { |
137
|
|
|
return [ |
138
|
|
|
$string |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param QueryBuilder $queryBuilder |
148
|
|
|
* @param string $fieldName |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected static function addIsNullExpression(QueryBuilder $queryBuilder, string $fieldName) |
152
|
|
|
{ |
153
|
|
|
return $queryBuilder->expr()->isNull($fieldName); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param QueryBuilder $queryBuilder |
158
|
|
|
* @param string $fieldName |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
protected static function addIsNotNullExpression(QueryBuilder $queryBuilder, string $fieldName) |
162
|
|
|
{ |
163
|
|
|
return $queryBuilder->expr()->isNotNull($fieldName); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.