FieldHelper::isNull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BenTools\Where\Helper;
5
6
use BenTools\Where\Expression\Expression;
7
use function BenTools\Where\placeholders;
8
use function BenTools\Where\random_placeholders;
9
use function BenTools\Where\random_string;
10
use function BenTools\Where\where;
11
12
/**
13
 * @internal
14
 */
15
final class FieldHelper
16
{
17
    /**
18
     * @var string
19
     */
20
    private $field;
21
22
    /**
23
     * Field constructor.
24
     */
25
    public function __construct(string $field)
26
    {
27
        $this->field = $field;
28
    }
29
30
    /**
31
     * @return Expression
32
     * @throws \InvalidArgumentException
33
     */
34
    public function isNull(): Expression
35
    {
36
        return where(\sprintf('%s IS NULL', $this->field));
37
    }
38
39
    /**
40
     * @return Expression
41
     * @throws \InvalidArgumentException
42
     */
43
    public function isNotNull(): Expression
44
    {
45
        return where(\sprintf('%s IS NOT NULL', $this->field));
46
    }
47
48
    /**
49
     * @return Expression
50
     * @throws \InvalidArgumentException
51
     */
52
    public function isTrue(): Expression
53
    {
54
        return where(\sprintf('%s = TRUE', $this->field));
55
    }
56
57
    /**
58
     * @return Expression
59
     * @throws \InvalidArgumentException
60
     */
61
    public function isFalse(): Expression
62
    {
63
        return where(\sprintf('%s = FALSE', $this->field));
64
    }
65
66
    /**
67
     * @param array       $values
68
     * @param null|string $placeholder
69
     * @param string      $glue
70
     * @return Expression
71
     * @throws \InvalidArgumentException
72
     */
73
    public function in(array $values, ?string $placeholder = '?', string $glue = ', '): Expression
74
    {
75
        $expression = '%s IN (%s)';
76
77
        if ('?' === $placeholder) {
78
            return where(\sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...\array_values($values));
79
        }
80
81
        if ('??' === $placeholder) {
82
            $placeholders = random_placeholders($values);
83
            $stringified = \sprintf(
84
                $expression,
85
                $this->field,
86
                \implode(
87
                    ', ',
88
                    \array_map(
89
                        function (string $placeholder) {
90
                            return ':'.$placeholder;
91
                        },
92
                        $placeholders
93
                    )
94
                )
95
            );
96
97
            return where($stringified, \array_combine($placeholders, $values));
98
        }
99
100
        if (null !== $placeholder) {
101
            throw new \InvalidArgumentException(\sprintf('Expected "?", "??" or null, got %s', $placeholder));
102
        }
103
104
        return where(\sprintf($expression, $this->field, \implode(', ', $values)));
105
    }
106
107
    /**
108
     * @param array  $values
109
     * @param string $placeholder
110
     * @param string $glue
111
     * @return Expression
112
     * @throws \InvalidArgumentException
113
     */
114
    public function notIn(array $values, ?string $placeholder = '?', string $glue = ', '): Expression
115
    {
116
        $expression = '%s NOT IN (%s)';
117
118
        if ('?' === $placeholder) {
119
            return where(\sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...\array_values($values));
120
        }
121
122
        if ('??' === $placeholder) {
123
            $placeholders = random_placeholders($values);
124
            $stringified = \sprintf(
125
                $expression,
126
                $this->field,
127
                \implode(
128
                    ', ',
129
                    \array_map(
130
                        function (string $placeholder) {
131
                            return ':'.$placeholder;
132
                        },
133
                        $placeholders
134
                    )
135
                )
136
            );
137
138
            return where($stringified, \array_combine($placeholders, $values));
139
        }
140
141
        if (null !== $placeholder) {
142
            throw new \InvalidArgumentException(\sprintf('Expected "?", "??" or null, got %s', $placeholder));
143
        }
144
145
        return where(\sprintf($expression, $this->field, \implode(', ', $values)));
146
    }
147
148
    /**
149
     * @param        $value
150
     * @param string $placeholder
151
     * @return Expression
152
     * @throws \InvalidArgumentException
153
     */
154
    public function equals($value, ?string $placeholder = '?'): Expression
155
    {
156
        $expression = '%s = %s';
157
158
        switch ($placeholder) {
159
            case '?':
160
                return where(\sprintf($expression, $this->field, $placeholder), $value);
161
            case '??':
162
                $random = random_string();
163
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
164
            case null:
165
                return where(\sprintf($expression, $this->field, $value));
166
        }
167
168
        $placeholder = \ltrim($placeholder, ':');
169
170
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
171
    }
172
173
    /**
174
     * @param        $value
175
     * @param string $placeholder
176
     * @return Expression
177
     * @throws \InvalidArgumentException
178
     */
179
    public function notEquals($value, ?string $placeholder = '?'): Expression
180
    {
181
        $expression = '%s <> %s';
182
183
        switch ($placeholder) {
184
            case '?':
185
                return where(\sprintf($expression, $this->field, $placeholder), $value);
186
            case '??':
187
                $random = random_string();
188
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
189
            case null:
190
                return where(\sprintf($expression, $this->field, $value));
191
        }
192
193
        $placeholder = \ltrim($placeholder, ':');
194
195
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
196
    }
197
198
    /**
199
     * @param        $value
200
     * @param string $placeholder
201
     * @return Expression
202
     * @throws \InvalidArgumentException
203
     */
204
    public function lt($value, ?string $placeholder = '?'): Expression
205
    {
206
        $expression = '%s < %s';
207
208
        switch ($placeholder) {
209
            case '?':
210
                return where(\sprintf($expression, $this->field, $placeholder), $value);
211
            case '??':
212
                $random = random_string();
213
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
214
            case null:
215
                return where(\sprintf($expression, $this->field, $value));
216
        }
217
218
        $placeholder = \ltrim($placeholder, ':');
219
220
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
221
    }
222
223
    /**
224
     * @param        $value
225
     * @param string $placeholder
226
     * @return Expression
227
     * @throws \InvalidArgumentException
228
     */
229
    public function lte($value, ?string $placeholder = '?'): Expression
230
    {
231
        $expression = '%s <= %s';
232
233
        switch ($placeholder) {
234
            case '?':
235
                return where(\sprintf($expression, $this->field, $placeholder), $value);
236
            case '??':
237
                $random = random_string();
238
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
239
            case null:
240
                return where(\sprintf($expression, $this->field, $value));
241
        }
242
243
        $placeholder = \ltrim($placeholder, ':');
244
245
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
246
    }
247
248
    /**
249
     * @param        $value
250
     * @param string $placeholder
251
     * @return Expression
252
     * @throws \InvalidArgumentException
253
     */
254
    public function gt($value, ?string $placeholder = '?'): Expression
255
    {
256
        $expression = '%s > %s';
257
258
        switch ($placeholder) {
259
            case '?':
260
                return where(\sprintf($expression, $this->field, $placeholder), $value);
261
            case '??':
262
                $random = random_string();
263
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
264
            case null:
265
                return where(\sprintf($expression, $this->field, $value));
266
        }
267
268
        $placeholder = \ltrim($placeholder, ':');
269
270
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
271
    }
272
273
    /**
274
     * @param        $value
275
     * @param string $placeholder
276
     * @return Expression
277
     * @throws \InvalidArgumentException
278
     */
279
    public function gte($value, ?string $placeholder = '?'): Expression
280
    {
281
        $expression = '%s >= %s';
282
283
        switch ($placeholder) {
284
            case '?':
285
                return where(\sprintf($expression, $this->field, $placeholder), $value);
286
            case '??':
287
                $random = random_string();
288
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
289
            case null:
290
                return where(\sprintf($expression, $this->field, $value));
291
        }
292
293
        $placeholder = \ltrim($placeholder, ':');
294
295
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
296
    }
297
298
    /**
299
     * @param        $start
300
     * @param        $end
301
     * @param string $placeholder
302
     * @return Expression
303
     * @throws \InvalidArgumentException
304
     */
305
    public function between($start, $end, ?string $placeholder = '?'): Expression
306
    {
307
        $expression = '%s BETWEEN %s AND %s';
308
309
        if ('?' === $placeholder) {
310
            return where(\sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end);
311
        }
312
313
        if ('??' === $placeholder) {
314
            $placeholders = random_placeholders([$start, $end]);
315
            $stringified = \sprintf(
316
                $expression,
317
                $this->field,
318
                ':'.$placeholders[0],
319
                ':'.$placeholders[1]
320
            );
321
322
            return where($stringified, \array_combine($placeholders, [$start, $end]));
323
        }
324
325
        if (null !== $placeholder) {
326
            throw new \InvalidArgumentException(\sprintf('Expected "?", "??" or null, got %s', $placeholder));
327
        }
328
329
        return where(\sprintf($expression, $this->field, $start, $end));
330
    }
331
332
    /**
333
     * @param        $start
334
     * @param        $end
335
     * @param string $placeholder
336
     * @return Expression
337
     * @throws \InvalidArgumentException
338
     */
339
    public function notBetween($start, $end, ?string $placeholder = '?'): Expression
340
    {
341
        $expression = '%s NOT BETWEEN %s AND %s';
342
343
        if ('?' === $placeholder) {
344
            return where(\sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end);
345
        }
346
347
        if ('??' === $placeholder) {
348
            $placeholders = random_placeholders([$start, $end]);
349
            $stringified = \sprintf(
350
                $expression,
351
                $this->field,
352
                ':'.$placeholders[0],
353
                ':'.$placeholders[1]
354
            );
355
356
            return where($stringified, \array_combine($placeholders, [$start, $end]));
357
        }
358
359
        if (null !== $placeholder) {
360
            throw new \InvalidArgumentException(\sprintf('Expected "?", "??" or null, got %s', $placeholder));
361
        }
362
363
        return where(\sprintf($expression, $this->field, $start, $end));
364
    }
365
366
    /**
367
     * @param string $value
368
     * @param string $placeholder
369
     * @param string $surroundWith
370
     * @return Expression
371
     * @throws \InvalidArgumentException
372
     */
373
    public function like(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
374
    {
375
        $expression = '%s LIKE %s';
376
        $value = $surroundWith . $value . $surroundWith;
377
378
        switch ($placeholder) {
379
            case '?':
380
                return where(\sprintf($expression, $this->field, $placeholder), $value);
381
            case '??':
382
                $random = random_string();
383
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
384
            case null:
385
                return where(\sprintf($expression, $this->field, $value));
386
        }
387
388
        $placeholder = \ltrim($placeholder, ':');
389
390
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
391
    }
392
393
    /**
394
     * @param string $value
395
     * @param string $placeholder
396
     * @param string $surroundWith
397
     * @return Expression
398
     * @throws \InvalidArgumentException
399
     */
400
    public function notLike(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
401
    {
402
        $expression = '%s NOT LIKE %s';
403
        $value = $surroundWith . $value . $surroundWith;
404
405
        switch ($placeholder) {
406
            case '?':
407
                return where(\sprintf($expression, $this->field, $placeholder), $value);
408
            case '??':
409
                $random = random_string();
410
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
411
            case null:
412
                return where(\sprintf($expression, $this->field, $value));
413
        }
414
415
        $placeholder = \ltrim($placeholder, ':');
416
417
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
418
    }
419
420
    /**
421
     * @param string $value
422
     * @param string $placeholder
423
     * @param string $surroundWith
424
     * @return Expression
425
     * @throws \InvalidArgumentException
426
     */
427
    public function startsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
428
    {
429
        $expression = '%s LIKE %s';
430
        $value = $value . $surroundWith;
431
432
        switch ($placeholder) {
433
            case '?':
434
                return where(\sprintf($expression, $this->field, $placeholder), $value);
435
            case '??':
436
                $random = random_string();
437
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
438
            case null:
439
                return where(\sprintf($expression, $this->field, $value));
440
        }
441
442
        $placeholder = \ltrim($placeholder, ':');
443
444
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
445
    }
446
447
    /**
448
     * @param string $value
449
     * @param string $placeholder
450
     * @param string $surroundWith
451
     * @return Expression
452
     * @throws \InvalidArgumentException
453
     */
454
    public function notStartsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
455
    {
456
        $expression = '%s NOT LIKE %s';
457
        $value = $value . $surroundWith;
458
459
        switch ($placeholder) {
460
            case '?':
461
                return where(\sprintf($expression, $this->field, $placeholder), $value);
462
            case '??':
463
                $random = random_string();
464
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
465
            case null:
466
                return where(\sprintf($expression, $this->field, $value));
467
        }
468
469
        $placeholder = \ltrim($placeholder, ':');
470
471
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
472
    }
473
474
    /**
475
     * @param string $value
476
     * @param string $placeholder
477
     * @param string $surroundWith
478
     * @return Expression
479
     * @throws \InvalidArgumentException
480
     */
481
    public function endsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
482
    {
483
        $expression = '%s LIKE %s';
484
        $value = $surroundWith . $value;
485
486
        switch ($placeholder) {
487
            case '?':
488
                return where(\sprintf($expression, $this->field, $placeholder), $value);
489
            case '??':
490
                $random = random_string();
491
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
492
            case null:
493
                return where(\sprintf($expression, $this->field, $value));
494
        }
495
496
        $placeholder = \ltrim($placeholder, ':');
497
498
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
499
    }
500
501
    /**
502
     * @param string $value
503
     * @param string $placeholder
504
     * @param string $surroundWith
505
     * @return Expression
506
     * @throws \InvalidArgumentException
507
     */
508
    public function notEndsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression
509
    {
510
        $expression = '%s NOT LIKE %s';
511
        $value = $surroundWith . $value;
512
513
        switch ($placeholder) {
514
            case '?':
515
                return where(\sprintf($expression, $this->field, $placeholder), $value);
516
            case '??':
517
                $random = random_string();
518
                return where(\sprintf($expression, $this->field, ':'.$random), [$random => $value]);
519
            case null:
520
                return where(\sprintf($expression, $this->field, $value));
521
        }
522
523
        $placeholder = \ltrim($placeholder, ':');
524
525
        return where(\sprintf($expression, $this->field, ':'.$placeholder), [$placeholder => $value]);
526
    }
527
}
528