Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
90 / 90 |
Conditions | |
100.00% |
1 / 1 |
|
100.00% |
10 / 10 |
34 | |
100.00% |
90 / 90 |
check | |
100.00% |
1 / 1 |
2 | |
100.00% |
2 / 2 |
|||
isConditionsFormattedCorrectly | |
100.00% |
1 / 1 |
10 | |
100.00% |
19 / 19 |
|||
notAnArray | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
nbOfParamsIsNotEnough | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
firstParamIsStringError | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
secondParamIsOnListError | |
100.00% |
1 / 1 |
2 | |
100.00% |
17 / 17 |
|||
thirdParamIsNeededError | |
100.00% |
1 / 1 |
3 | |
100.00% |
15 / 15 |
|||
stringIfNeededError | |
100.00% |
1 / 1 |
4 | |
100.00% |
13 / 13 |
|||
arrayIfNeededError | |
100.00% |
1 / 1 |
4 | |
100.00% |
6 / 6 |
|||
andOrError | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ApiFilterRequest\Filters; |
4 | |
5 | use Qmp\Laravel\ApiFilterRequest\Filters; |
6 | |
7 | class Conditions extends AbstractFilter |
8 | { |
9 | const EQUAL = '='; |
10 | const NOT_EQUAL = '!='; |
11 | const GREATER_THAN = '>'; |
12 | const GREATER_OR_EQUAL = '>='; |
13 | const LESS_THAN = '<'; |
14 | const LESS_OR_EQUAL = '<='; |
15 | const BEGIN_BY = 'begin by'; |
16 | const END_BY = 'end by'; |
17 | const CONTAIN = 'contain'; |
18 | const IS_NULL = 'is null'; |
19 | const NOT_NULL = 'not null'; |
20 | const IN = 'in'; |
21 | const NOT_IN = 'not in'; |
22 | |
23 | const AND_REQUEST = 'and'; |
24 | const OR_REQUEST = 'or'; |
25 | |
26 | /** |
27 | * @param $data |
28 | * @return bool |
29 | */ |
30 | public function check($data) |
31 | { |
32 | return $this->isArray($data) |
33 | && $this->isConditionsFormattedCorrectly($data); |
34 | } |
35 | |
36 | /** |
37 | * @param array $filters |
38 | * @return bool |
39 | */ |
40 | private function isConditionsFormattedCorrectly(array $filters) |
41 | { |
42 | foreach ($filters as $filter) { |
43 | if ($this->notAnArray($filter)) { |
44 | return false; |
45 | } |
46 | |
47 | $filter = array_values($filter); |
48 | if ($this->nbOfParamsIsNotEnough($filter)) { |
49 | return false; |
50 | } |
51 | |
52 | if ($this->firstParamIsStringError($filter)) { |
53 | return false; |
54 | } |
55 | |
56 | if ($this->secondParamIsOnListError($filter)) { |
57 | return false; |
58 | } |
59 | |
60 | if ($this->thirdParamIsNeededError($filter)) { |
61 | return false; |
62 | } |
63 | |
64 | if ($this->stringIfNeededError($filter)) { |
65 | return false; |
66 | } |
67 | |
68 | if ($this->arrayIfNeededError($filter)) { |
69 | return false; |
70 | } |
71 | |
72 | if ($this->andOrError($filter)) { |
73 | return false; |
74 | } |
75 | } |
76 | |
77 | return true; |
78 | } |
79 | |
80 | private function notAnArray($filter) |
81 | { |
82 | if (!is_array($filter)) { |
83 | $this->addError('fitler must be an array : ' . var_export($filter, true)); |
84 | return true; |
85 | } |
86 | |
87 | return false; |
88 | } |
89 | |
90 | /** |
91 | * @param $filter |
92 | * @return bool |
93 | */ |
94 | private function nbOfParamsIsNotEnough($filter) |
95 | { |
96 | if (count($filter) < 2) { |
97 | $this->addError('nb of parameters is not enough (min:2) : ' . var_export($filter, true)); |
98 | return true; |
99 | } |
100 | |
101 | return false; |
102 | } |
103 | |
104 | /** |
105 | * @param $filter |
106 | * @return bool |
107 | */ |
108 | private function firstParamIsStringError($filter) |
109 | { |
110 | if (!is_string($filter[0])) { |
111 | $this->addError('First parameter must be a string ' . var_export($filter[0], true)); |
112 | return true; |
113 | } |
114 | |
115 | return false; |
116 | } |
117 | |
118 | /** |
119 | * @param $filter |
120 | * @return bool |
121 | */ |
122 | private function secondParamIsOnListError($filter) |
123 | { |
124 | if (!in_array(strtolower($filter[1]), [ |
125 | self::EQUAL, |
126 | self::NOT_EQUAL, |
127 | self::GREATER_THAN, |
128 | self::GREATER_OR_EQUAL, |
129 | self::LESS_THAN, |
130 | self::LESS_OR_EQUAL, |
131 | self::BEGIN_BY, |
132 | self::END_BY, |
133 | self::CONTAIN, |
134 | self::IS_NULL, |
135 | self::NOT_NULL, |
136 | self::IN, |
137 | self::NOT_IN |
138 | ])) { |
139 | $this->addError('String not allowed : ' . var_export($filter[1], true)); |
140 | return true; |
141 | } |
142 | |
143 | return false; |
144 | } |
145 | |
146 | /** |
147 | * @param $filter |
148 | * @return bool |
149 | */ |
150 | private function thirdParamIsNeededError($filter) |
151 | { |
152 | if (!isset($filter[2]) && in_array(strtolower($filter[1]), [ |
153 | self::EQUAL, |
154 | self::NOT_EQUAL, |
155 | self::GREATER_THAN, |
156 | self::GREATER_OR_EQUAL, |
157 | self::LESS_THAN, |
158 | self::LESS_OR_EQUAL, |
159 | self::BEGIN_BY, |
160 | self::END_BY, |
161 | self::CONTAIN, |
162 | self::IN, |
163 | self::NOT_IN |
164 | ])) { |
165 | $this->addError('3rd parameter is needed for : ' . var_export($filter[1], true)); |
166 | return true; |
167 | } |
168 | |
169 | return false; |
170 | } |
171 | |
172 | /** |
173 | * @param $filter |
174 | * @return bool |
175 | */ |
176 | private function stringIfNeededError($filter) |
177 | { |
178 | if ((!isset($filter[2]) || is_array($filter[2])) && in_array(strtolower($filter[1]), [ |
179 | self::EQUAL, |
180 | self::NOT_EQUAL, |
181 | self::GREATER_THAN, |
182 | self::GREATER_OR_EQUAL, |
183 | self::LESS_THAN, |
184 | self::LESS_OR_EQUAL, |
185 | self::BEGIN_BY, |
186 | self::END_BY, |
187 | self::CONTAIN |
188 | ])) { |
189 | $this->addError('the third parameter does not have to be an array : ' . var_export($filter[0], true)); |
190 | return true; |
191 | } |
192 | |
193 | return false; |
194 | } |
195 | |
196 | /** |
197 | * @param $filter |
198 | * @return bool |
199 | */ |
200 | private function arrayIfNeededError($filter) |
201 | { |
202 | if ((!isset($filter[2]) || !is_array($filter[2])) && in_array(strtolower($filter[1]), [ |
203 | self::IN, |
204 | self::NOT_IN |
205 | ])) { |
206 | $this->addError('the third parameter must be an array : ' . var_export($filter[0], true)); |
207 | return true; |
208 | } |
209 | |
210 | return false; |
211 | } |
212 | |
213 | /** |
214 | * @param $filter |
215 | * @return bool |
216 | */ |
217 | private function andOrError($filter) |
218 | { |
219 | if (isset($filter[3]) && !in_array(strtolower($filter[3]), [ |
220 | self::AND_REQUEST, |
221 | self::OR_REQUEST |
222 | ])) { |
223 | $this->addError('the forth parameter must be \'AND\' or \'OR\' string : ' . var_export($filter[3], true)); |
224 | return true; |
225 | } |
226 | |
227 | return false; |
228 | } |
229 | } |