Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

CompilesWheres::whereDay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
6
use Illuminate\Database\Query\Expression;
7
use LaravelFreelancerNL\Aranguent\Query\Builder;
8
9
trait CompilesWheres
10
{
11
    /**
12
     * Format the where clause statements into one string.
13
     *
14
     * @param IlluminateQueryBuilder $query
15
     * @param  array  $sql
16
     * @return string
17
     */
18
    protected function concatenateWhereClauses($query, $sql)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    protected function concatenateWhereClauses(/** @scrutinizer ignore-unused */ $query, $sql)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        return 'FILTER ' . $this->removeLeadingBoolean(implode(' ', $sql));
0 ignored issues
show
Bug introduced by
It seems like removeLeadingBoolean() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return 'FILTER ' . $this->/** @scrutinizer ignore-call */ removeLeadingBoolean(implode(' ', $sql));
Loading history...
21
    }
22
23
    protected function getOperatorByWhereType($type)
24
    {
25
        if (isset($this->whereTypeOperators[$type])) {
26
            return $this->whereTypeOperators[$type];
27
        }
28
29
        return '==';
30
    }
31
32
    /**
33
     * Determine if the given value is a raw expression.
34
     *
35
     * @param  mixed  $value
36
     * @return bool
37
     */
38
    public function isExpression($value)
39
    {
40
        return $value instanceof Expression;
41
    }
42
43
    protected function normalizeOperator($where)
44
    {
45
        if (isset($where['operator'])) {
46
            $where['operator'] = $this->translateOperator($where['operator']);
47
        }
48
        if (!isset($where['operator'])) {
49
            $where['operator'] = $this->getOperatorByWhereType($where['type']);
50
        }
51
52
        return $where;
53
    }
54
55
    /**
56
     * Translate sql operators to their AQL equivalent where possible.
57
     *
58
     * @param string $operator
59
     *
60
     * @return mixed|string
61
     */
62
    private function translateOperator(string $operator)
63
    {
64
        if (isset($this->operatorTranslations[strtolower($operator)])) {
65
            $operator = $this->operatorTranslations[$operator];
66
        }
67
68
        return $operator;
69
    }
70
71
72
    /**
73
     * Compile a basic where clause.
74
     *
75
     * @param  IlluminateQueryBuilder  $query
76
     * @param  array  $where
77
     * @return array
78
     */
79
    protected function whereBasic(IlluminateQueryBuilder $query, $where)
80
    {
81
        $predicate = [];
82
83
        $where = $this->normalizeOperator($where);
84
85
        if ($where['column'] instanceof expression) {
86
            $column = 'FIRST(' . $where['column']->getValue($this) . ')';
87
        } else {
88
            $column = $this->normalizeColumn($query, $where['column']);
0 ignored issues
show
Bug introduced by
It seems like normalizeColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            /** @scrutinizer ignore-call */ 
89
            $column = $this->normalizeColumn($query, $where['column']);
Loading history...
89
        }
90
91
        $predicate[0] = $column;
92
        $predicate[1] = $where['operator'];
93
        $predicate[2] = $this->parameter($where['value']);
0 ignored issues
show
Bug introduced by
It seems like parameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        /** @scrutinizer ignore-call */ 
94
        $predicate[2] = $this->parameter($where['value']);
Loading history...
94
95
        return implode(" ", $predicate);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $predicate) returns the type string which is incompatible with the documented return type array.
Loading history...
96
    }
97
98
    /**
99
     * Compile a "between" where clause.
100
     *
101
     * @param IlluminateQueryBuilder $query
102
     * @param array $where
103
     * @return string
104
     * @throws \Exception
105
     */
106
    protected function whereBetween(IlluminateQueryBuilder $query, $where)
107
    {
108
        $predicate = [];
109
110
        [$minOperator, $maxOperator, $boolean] = $this->getBetweenOperators($where['not']);
111
112
        $min = $this->parameter(reset($where['values']));
113
114
        $max = $this->parameter(end($where['values']));
115
116
        $predicate[0][0] = $this->normalizeColumn($query, $where['column']);
117
        $predicate[0][1] = $minOperator;
118
        $predicate[0][2] = $min;
119
120
        $predicate[1][0] = $this->normalizeColumn($query, $where['column']);
121
        $predicate[1][1] = $maxOperator;
122
        $predicate[1][2] = $max;
123
124
        return implode(" ", $predicate[0]) . " " . $boolean . " " . implode(" ", $predicate[1]);
125
    }
126
127
    /**
128
     * Generate operators for between and 'not between'
129
     * @param  bool  $notBetween
130
     * @return string[]
131
     */
132
    protected function getBetweenOperators(bool $notBetween)
133
    {
134
        $minOperator = '>=';
135
        $maxOperator = '<=';
136
        $boolean = 'AND';
137
138
        if ($notBetween) {
139
            $minOperator = '<';
140
            $maxOperator = '>';
141
            $boolean = 'OR';
142
        }
143
144
        return [$minOperator, $maxOperator, $boolean];
145
    }
146
147
    /**
148
     * Compile a "between" where clause.
149
     *
150
     * @param IlluminateQueryBuilder $query
151
     * @param array $where
152
     * @return string
153
     * @throws \Exception
154
     */
155
    protected function whereBetweenColumns(IlluminateQueryBuilder $query, $where)
156
    {
157
        $predicate = [];
158
159
        [$minOperator, $maxOperator, $boolean] = $this->getBetweenOperators($where['not']);
160
161
        $column = $this->normalizeColumn($query, $where['column']);
162
163
        $predicate[0][0] = $column;
164
        $predicate[0][1] = $minOperator;
165
        $predicate[0][2] = $this->normalizeColumn($query, reset($where['values']));
166
167
        $predicate[1][0] = $column;
168
        $predicate[1][1] = $maxOperator;
169
        $predicate[1][2] = $this->normalizeColumn($query, end($where['values']));
170
171
        return implode(" ", $predicate[0]) . " " . $boolean . " " . implode(" ", $predicate[1]);
172
173
    }
174
175
176
    /**
177
     * Compile a where clause comparing two columns..
178
     *
179
     * @param  IlluminateQueryBuilder  $query
180
     * @param  array  $where
181
     * @return string
182
     */
183
    protected function whereColumn(IlluminateQueryBuilder $query, $where)
184
    {
185
        $predicate = [];
186
187
        $where = $this->normalizeOperator($where);
188
        $predicate[0] = $this->normalizeColumn($query, $where['first']);
189
        $predicate[1] = $where['operator'];
190
        $predicate[2] = $this->normalizeColumn($query, $where['second']);
191
192
        return implode(" ", $predicate);
193
    }
194
195
    /**
196
     * Compile a "where null" clause.
197
     *
198
     * @param  IlluminateQueryBuilder  $query
199
     * @param  array  $where
200
     * @return string
201
     */
202
    protected function whereNull(IlluminateQueryBuilder $query, $where)
203
    {
204
        $predicate = [];
205
206
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
207
        $predicate[1] = '==';
208
        $predicate[2] = 'null';
209
210
        return implode(" ", $predicate);
211
    }
212
213
    /**
214
     * Compile a "where not null" clause.
215
     *
216
     * @param  IlluminateQueryBuilder  $query
217
     * @param  array  $where
218
     * @return string
219
     */
220
    protected function whereNotNull(IlluminateQueryBuilder $query, $where)
221
    {
222
        $predicate = [];
223
224
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
225
        $predicate[1] = '!=';
226
        $predicate[2] = 'null';
227
228
        return implode(' ', $predicate);
229
    }
230
231
    /**
232
     * Compile a "where in" clause.
233
     *
234
     * @param  IlluminateQueryBuilder  $query
235
     * @param  array  $where
236
     * @return string
237
     */
238
    protected function whereIn(IlluminateQueryBuilder $query, $where)
239
    {
240
        $predicate = [];
241
242
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
243
        $predicate[1] = 'IN';
244
        $predicate[2] = $this->parameter($where['values']);
245
246
        return implode(" ", $predicate);
247
    }
248
249
    /**
250
     * Compile a "where in raw" clause.
251
     *
252
     * For safety, whereIntegerInRaw ensures this method is only used with integer values.
253
     *
254
     * @param  IlluminateQueryBuilder  $query
255
     * @param  array  $where
256
     * @return string
257
     */
258
    protected function whereInRaw(IlluminateQueryBuilder $query, $where)
259
    {
260
        $predicate = [];
261
262
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
263
        $predicate[1] = 'IN';
264
        $predicate[2] = '[' . implode(', ', $where['values']) . ']';
265
266
        return implode(" ", $predicate);
267
    }
268
269
    /**
270
     * Compile a "where not in" clause.
271
     *
272
     * @param  IlluminateQueryBuilder  $query
273
     * @param  array  $where
274
     * @return string
275
     */
276
    protected function whereNotIn(IlluminateQueryBuilder $query, $where)
277
    {
278
        $predicate = [];
279
280
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
281
        $predicate[1] = 'NOT IN';
282
        $predicate[2] = $this->parameter($where['values']);
283
284
        return implode(" ", $predicate);
285
    }
286
287
    /**
288
     * Compile a "where not in raw" clause.
289
     *
290
     * For safety, whereIntegerInRaw ensures this method is only used with integer values.
291
     *
292
     * @param  IlluminateQueryBuilder  $query
293
     * @param  array  $where
294
     * @return string
295
     */
296
    protected function whereNotInRaw(IlluminateQueryBuilder $query, $where)
297
    {
298
        $predicate = [];
299
300
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
301
        $predicate[1] = 'NOT IN';
302
        $predicate[2] = '[' . implode(', ', $where['values']) . ']';
303
304
        return implode(" ", $predicate);
305
    }
306
307
    /**
308
     * Compile a "where JSON contains" clause.
309
     *
310
     * @param IlluminateQueryBuilder $query
311
     * @param  array  $where
312
     * @return string
313
     */
314
    protected function whereJsonContains(IlluminateQueryBuilder $query, $where)
315
    {
316
        $predicate = [];
317
318
        $operator = $where['not'] ? 'NOT IN' : 'IN';
319
320
        //FIXME: bind value earlier
321
        $predicate[0] = $this->parameter($where['value']);
322
        $predicate[1] = $operator;
323
        $predicate[2] = $this->normalizeColumn($query, $where['column']);
324
325
        return implode(" ", $predicate);
326
    }
327
328
    /**
329
     * Compile a "whereJsonContains" clause.
330
     *
331
     * @param  IlluminateQueryBuilder  $query
332
     * @param  array  $where
333
     * @return string
334
     */
335
    protected function whereJsonLength(IlluminateQueryBuilder $query, $where)
336
    {
337
        $predicate = [];
338
339
        $where = $this->normalizeOperator($where);
340
341
        $column = $this->normalizeColumn($query, $where['column']);
342
343
        $predicate[0] = "LENGTH($column)";
344
        $predicate[1] = $where['operator'];
345
        $predicate[2] = $this->parameter($where['value']);
346
347
        return implode(" ", $predicate);
348
    }
349
350
    /**
351
     * Compile a where date clause.
352
     *
353
     * @param  IlluminateQueryBuilder  $query
354
     * @param  array  $where
355
     * @return array
356
     */
357
    protected function whereDate(IlluminateQueryBuilder $query, $where)
358
    {
359
        $predicate = [];
360
361
        $where = $this->normalizeOperator($where);
362
        $predicate[0] = 'DATE_FORMAT(' . $this->normalizeColumn($query, $where['column']) . ', "%yyyy-%mm-%dd")';
363
        $predicate[1] = $where['operator'];
364
        $predicate[2] = $this->parameter($where['value']);
365
366
        return implode(' ', $predicate);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $predicate) returns the type string which is incompatible with the documented return type array.
Loading history...
367
    }
368
369
    /**
370
     * Compile a where year clause.
371
     *
372
     * @param  IlluminateQueryBuilder $query
373
     * @param  array  $where
374
     * @return array
375
     */
376
    protected function whereYear(IlluminateQueryBuilder $query, $where)
377
    {
378
        $predicate = [];
379
380
        $where = $this->normalizeOperator($where);
381
382
        $predicate[0] = 'DATE_YEAR(' . $this->normalizeColumn($query, $where['column']) . ')';
383
        $predicate[1] = $where['operator'];
384
        $predicate[2] = $this->parameter($where['value']);
385
386
        return implode(' ', $predicate);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $predicate) returns the type string which is incompatible with the documented return type array.
Loading history...
387
    }
388
389
    /**
390
     * Compile a where month clause.
391
     *
392
     * @param  IlluminateQueryBuilder  $query
393
     * @param  array  $where
394
     * @return array
395
     */
396
    protected function whereMonth(IlluminateQueryBuilder $query, $where)
397
    {
398
        $predicate = [];
399
400
        $where = $this->normalizeOperator($where);
401
402
        $predicate[0] =  'DATE_MONTH(' . $this->normalizeColumn($query, $where['column']) . ')';
403
        $predicate[1] = $where['operator'];
404
        $predicate[2] = $this->parameter($where['value']);
405
406
        return implode(' ', $predicate);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $predicate) returns the type string which is incompatible with the documented return type array.
Loading history...
407
    }
408
409
410
    /**
411
     * Compile a where day clause.
412
     *
413
     * @param  IlluminateQueryBuilder  $query
414
     * @param  array  $where
415
     * @return string
416
     */
417
    protected function whereDay(IlluminateQueryBuilder $query, $where)
418
    {
419
        $predicate = [];
420
421
        $where = $this->normalizeOperator($where);
422
423
        $predicate[0] = 'DATE_DAY(' . $this->normalizeColumn($query, $where['column']) . ')';
424
        $predicate[1] = $where['operator'];
425
        $predicate[2] = $this->parameter($where['value']);
426
427
        return implode(' ', $predicate);
428
    }
429
430
    /**
431
     * Compile a where time clause.
432
     *
433
     * @param  IlluminateQueryBuilder  $query
434
     * @param  array  $where
435
     * @return string
436
     */
437
    protected function whereTime(IlluminateQueryBuilder $query, $where)
438
    {
439
        $predicate = [];
440
441
        $where = $this->normalizeOperator($where);
442
443
        $predicate[0] = 'DATE_FORMAT(' . $this->normalizeColumn($query, $where['column']) . ", '%hh:%ii:%ss')";
444
        $predicate[1] = $where['operator'];
445
        $predicate[2] = $this->parameter($where['value']);
446
447
        return implode(' ', $predicate);
448
    }
449
450
    /**
451
     * Compile a nested where clause.
452
     *
453
     * @param  Builder  $query
454
     * @param  array  $where
455
     * @return string
456
     */
457
    //    protected function whereNested(Builder $query, $where)
458
    //    {
459
    //        $predicates = [];
460
    //        $predicates = $this->compileWheresToArray($where['query']);
461
    //
462
    //        $query->aqb->binds = array_merge($query->aqb->binds, $where['query']->aqb->binds);
463
    //        $query->aqb->collections = array_merge_recursive($query->aqb->collections, $where['query']->aqb->collections);
464
    //
465
    //        return $predicates;
466
    //    }
467
468
    /**
469
     * Compile a where condition with a sub-select.
470
     *
471
     * @param IlluminateQueryBuilder $query
472
     * @param array $where
473
     * @return string
474
     * @throws \Exception
475
     */
476
    protected function whereSub(IlluminateQueryBuilder $query, $where)
477
    {
478
        $predicate = [];
479
480
        $where = $this->normalizeOperator($where);
481
482
        $predicate[0] = $this->normalizeColumn($query, $where['column']);
483
        $predicate[1] = $where['operator'];
484
        $predicate[2] = $where['subquery'];
485
486
        return implode(' ', $predicate);
487
    }
488
489
    /**
490
     * Compile a where exists clause.
491
     *
492
     * @param  \Illuminate\Database\Query\Builder  $query
493
     * @param  array  $where
494
     * @return string
495
     */
496
    protected function whereExists(IlluminateQueryBuilder $query, $where)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

496
    protected function whereExists(/** @scrutinizer ignore-unused */ IlluminateQueryBuilder $query, $where)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
497
    {
498
        return 'LENGTH(' . $where['subquery'] . ') > 0';
499
    }
500
501
    /**
502
     * Compile a where exists clause.
503
     *
504
     * @param  \Illuminate\Database\Query\Builder  $query
505
     * @param  array  $where
506
     * @return string
507
     */
508
    protected function whereNotExists(IlluminateQueryBuilder $query, $where)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

508
    protected function whereNotExists(/** @scrutinizer ignore-unused */ IlluminateQueryBuilder $query, $where)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
509
    {
510
        return 'LENGTH(' . $where['subquery'] . ') == 0';
511
    }
512
}
513