Passed
Push — master ( feac44...0272f5 )
by Aleksandr
33:21
created

QB_OPERATOR::isBetween()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\Enum;
11
12
use Drobotik\Eav\Exception\QueryBuilderException;
13
14
class QB_OPERATOR
15
{
16
    public const EQUAL = 'equal';
17
    public const NOT_EQUAL = 'not_equal';
18
    public const IN = 'in';
19
    public const NOT_IN = 'not_in';
20
    public const LESS = 'less';
21
    public const LESS_OR_EQUAL = 'less_or_equal';
22
    public const GREATER = 'greater';
23
    public const GREATER_OR_EQUAL = 'greater_or_equal';
24
    public const BETWEEN = 'between';
25
    public const NOT_BETWEEN = 'not_between';
26
    public const BEGINS_WITH = 'begins_with';
27
    public const NOT_BEGINS_WITH = 'not_begins_with';
28
    public const CONTAINS = 'contains';
29
    public const NOT_CONTAINS = 'not_contains';
30
    public const ENDS_WITH = 'ends_with';
31
    public const NOT_ENDS_WITH = 'not_ends_with';
32
    public const IS_EMPTY = 'is_empty';
33
    public const IS_NOT_EMPTY = 'is_not_empty';
34
    public const IS_NULL = 'is_null';
35
    public const IS_NOT_NULL = 'is_not_null';
36
37
    /**
38 1
     * @throws QueryBuilderException
39 1
     */
40 1
    public static function getCase(string $case) : string
41 1
    {
42 1
        switch ($case) {
43 1
            case self::EQUAL:
44 1
                return self::EQUAL;
45 1
            case self::NOT_EQUAL:
46 1
                return self::NOT_EQUAL;
47 1
            case self::IN:
48 1
                return self::IN;
49 1
            case self::NOT_IN:
50 1
                return self::NOT_IN;
51 1
            case self::LESS:
52 1
                return self::LESS;
53 1
            case self::LESS_OR_EQUAL:
54 1
                return self::LESS_OR_EQUAL;
55 1
            case self::GREATER:
56 1
                return self::GREATER;
57 1
            case self::GREATER_OR_EQUAL:
58 1
                return self::GREATER_OR_EQUAL;
59 1
            case self::BETWEEN:
60 1
                return self::BETWEEN;
61
            case self::NOT_BETWEEN:
62
                return self::NOT_BETWEEN;
63
            case self::BEGINS_WITH:
64
                return self::BEGINS_WITH;
65 2
            case self::NOT_BEGINS_WITH:
66
                return self::NOT_BEGINS_WITH;
67 2
            case self::CONTAINS:
68 2
                return self::CONTAINS;
69 2
            case self::NOT_CONTAINS:
70 2
                return self::NOT_CONTAINS;
71 2
            case self::ENDS_WITH:
72 2
                return self::ENDS_WITH;
73 2
            case self::NOT_ENDS_WITH:
74 2
                return self::NOT_ENDS_WITH;
75 2
            case self::IS_EMPTY:
76 2
                return self::IS_EMPTY;
77 2
            case self::IS_NOT_EMPTY:
78 2
                return self::IS_NOT_EMPTY;
79 2
            case self::IS_NULL:
80 2
                return self::IS_NULL;
81 2
            case self::IS_NOT_NULL:
82 2
                return self::IS_NOT_NULL;
83 2
            default:
84 2
                throw QueryBuilderException::unsupportedOperator($case);
85 2
        }
86 2
    }
87 2
    public static function isValueRequired(string $name): bool {
88 2
        switch ($name) {
89 2
            case self::EQUAL:
90
            case self::NOT_EQUAL:
91
            case self::IN:
92 1
            case self::NOT_IN:
93 1
            case self::LESS:
94 1
            case self::LESS_OR_EQUAL:
95 1
            case self::GREATER:
96 1
            case self::GREATER_OR_EQUAL:
97 1
            case self::BETWEEN:
98 1
            case self::NOT_BETWEEN:
99 1
            case self::BEGINS_WITH:
100 1
            case self::NOT_BEGINS_WITH:
101 1
            case self::CONTAINS:
102 1
            case self::NOT_CONTAINS:
103 1
            case self::ENDS_WITH:
104 1
            case self::NOT_ENDS_WITH:
105 1
                return true;
106 1
107 1
            case self::IS_EMPTY:
108 1
            case self::IS_NOT_EMPTY:
109 1
            case self::IS_NULL:
110 1
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return boolean. Consider adding a default case to the switch.
Loading history...
111 1
                return false;
112 1
        }
113 1
    }
114 1
115
116
    public static function applyTo(string $name): array {
117 1
        switch ($name) {
118 1
            case self::EQUAL:
119 1
            case self::NOT_EQUAL:
120 1
            case self::IN:
121 1
            case self::NOT_IN:
122 1
            case self::LESS:
123 1
            case self::LESS_OR_EQUAL:
124 1
            case self::GREATER:
125
            case self::GREATER_OR_EQUAL:
126 1
            case self::BETWEEN:
127 1
            case self::NOT_BETWEEN:
128 1
                return [
129 1
                    ATTR_TYPE::INTEGER,
130
                    ATTR_TYPE::DECIMAL,
131 1
                    ATTR_TYPE::DATETIME
132 1
                ];
133 1
134 1
            case self::BEGINS_WITH:
135 1
            case self::NOT_BEGINS_WITH:
136 1
            case self::CONTAINS:
137 1
            case self::NOT_CONTAINS:
138
            case self::ENDS_WITH:
139 1
            case self::NOT_ENDS_WITH:
140 1
            case self::IS_EMPTY:
141 1
            case self::IS_NOT_EMPTY:
142 1
                return [
143 1
                    ATTR_TYPE::STRING,
144 1
                    ATTR_TYPE::TEXT
145 1
                ];
146 1
147 1
            case self::IS_NULL:
148 1
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return array. Consider adding a default case to the switch.
Loading history...
149 1
                return [
150 1
                    ATTR_TYPE::STRING,
151 1
                    ATTR_TYPE::TEXT,
152 1
                    ATTR_TYPE::INTEGER,
153 1
                    ATTR_TYPE::DECIMAL,
154 1
                    ATTR_TYPE::DATETIME
155 1
                ];
156 1
        }
157 1
    }
158 1
159 1
    public static function sql(string $name): string {
160 1
        switch ($name) {
161 1
            case self::EQUAL:
162
            case self::IS_EMPTY:
163
                return '=';
164 1
165 1
            case self::NOT_EQUAL:
166 1
            case self::IS_NOT_EMPTY:
167 1
                return '!=';
168 1
169 1
            case self::IN:
170 1
                return 'IN';
171 1
172
            case self::NOT_IN:
173
                return 'NOT IN';
174 1
175 1
            case self::LESS:
176 1
                return '<';
177 1
178 1
            case self::LESS_OR_EQUAL:
179 1
                return '<=';
180 1
181 1
            case self::GREATER:
182
                return '>';
183
184 1
            case self::GREATER_OR_EQUAL:
185
                return '>=';
186 1
187 1
            case self::BETWEEN:
188 1
                return 'BETWEEN';
189 1
190 1
            case self::NOT_BETWEEN:
191 1
                return 'NOT BETWEEN';
192 1
193
            case self::BEGINS_WITH:
194
            case self::CONTAINS:
195 1
            case self::ENDS_WITH:
196
                return 'LIKE';
197 1
198 1
            case self::NOT_BEGINS_WITH:
199 1
            case self::NOT_CONTAINS:
200 1
            case self::NOT_ENDS_WITH:
201 1
                return 'NOT LIKE';
202
203
            case self::IS_NULL:
204 1
                return 'NULL';
205
206 1
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return string. Consider adding a default case to the switch.
Loading history...
207 1
                return 'NOT NULL';
208 1
        }
209 1
    }
210 1
211
    public static function prepend(string $name): string|bool {
212
        switch ($name) {
213
            case self::ENDS_WITH:
214 1
            case self::NOT_ENDS_WITH:
215
            case self::CONTAINS:
216 1
            case self::NOT_CONTAINS:
217 1
                return '%';
218 1
219 1
            default:
220 1
                return false;
221
        }
222
    }
223 1
224
    public static function append(string $name): string|bool {
225 1
        switch ($name) {
226 1
            case self::BEGINS_WITH:
227 1
            case self::NOT_BEGINS_WITH:
228 1
            case self::CONTAINS:
229 1
            case self::NOT_CONTAINS:
230 1
                return '%';
231 1
232 1
            default:
233 1
                return false;
234
        }
235
    }
236
237 1
    public static function isNeedsArray(string $name): bool {
238
        switch ($name) {
239 1
            case self::NOT_IN:
240 1
            case self::IN:
241 1
            case self::NOT_BETWEEN:
242 1
            case self::BETWEEN:
243 1
                return true;
244 1
245 1
            default:
246 1
                return false;
247 1
        }
248 1
    }
249 1
250 1
    public static function isNull(string $name): bool {
251 1
        switch ($name) {
252 1
            case self::IS_NOT_NULL:
253 1
            case self::IS_NULL:
254 1
                return true;
255
256
            default:
257
                return false;
258
        }
259
    }
260
261
    public static function isEmpty(string $name): bool {
262
        switch ($name) {
263
            case self::IS_EMPTY:
264
            case self::IS_NOT_EMPTY:
265
                return true;
266
267
            default:
268
                return false;
269
        }
270
    }
271
272
    public static function isBetween(string $name): bool {
273
        switch ($name) {
274
            case self::BETWEEN:
275
            case self::NOT_BETWEEN:
276
                return true;
277
278
            default:
279
                return false;
280
        }
281
    }
282
283
    public static function isLike(string $name): bool {
284
        switch ($name) {
285
            case self::BEGINS_WITH:
286
            case self::CONTAINS:
287
            case self::ENDS_WITH:
288
            case self::NOT_BEGINS_WITH:
289
            case self::NOT_ENDS_WITH:
290
            case self::NOT_CONTAINS:
291
                return true;
292
293
            default:
294
                return false;
295
        }
296
    }
297
298
    public static function expr(string $name): string {
299
        switch ($name) {
300
            case self::EQUAL:
301
            case self::IS_EMPTY:
302
                return 'eq';
303
304
            case self::NOT_EQUAL:
305
            case self::IS_NOT_EMPTY:
306
                return 'neq';
307
308
            case self::IN:
309
                return 'in';
310
311
            case self::NOT_IN:
312
                return 'notIn';
313
314
            case self::LESS:
315
                return 'lt';
316
317
            case self::LESS_OR_EQUAL:
318
                return 'lte';
319
320
            case self::GREATER:
321
                return 'gt';
322
323
            case self::GREATER_OR_EQUAL:
324
                return 'gte';
325
326
            case self::BETWEEN:
327
                return 'between';
328
329
            case self::NOT_BETWEEN:
330
                return 'notBetween';
331
332
            case self::BEGINS_WITH:
333
            case self::CONTAINS:
334
            case self::ENDS_WITH:
335
                return 'like';
336
337
            case self::NOT_BEGINS_WITH:
338
            case self::NOT_CONTAINS:
339
            case self::NOT_ENDS_WITH:
340
                return 'notLike';
341
342
            case self::IS_NULL:
343
                return 'isNull';
344
345
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return string. Consider adding a default case to the switch.
Loading history...
346
                return 'isNotNull';
347
        }
348
    }
349
}