Completed
Push — master ( a4273b...a41938 )
by Beniamin
03:04
created

ExprBuilder::ifNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Phuria\QueryBuilder;
4
5
use Phuria\QueryBuilder\Expression\AliasExpression;
6
use Phuria\QueryBuilder\Expression\Arithmetic as Arithmetic;
7
use Phuria\QueryBuilder\Expression\AscExpression;
8
use Phuria\QueryBuilder\Expression\Comparison as Comparison;
9
use Phuria\QueryBuilder\Expression\ConjunctionExpression;
10
use Phuria\QueryBuilder\Expression\DescExpression;
11
use Phuria\QueryBuilder\Expression\EmptyExpression;
12
use Phuria\QueryBuilder\Expression\ExpressionInterface;
13
use Phuria\QueryBuilder\Expression\Func as Func;
14
use Phuria\QueryBuilder\Expression\ImplodeExpression;
15
use Phuria\QueryBuilder\Expression\InExpression;
16
use Phuria\QueryBuilder\Expression\RawExpression;
17
use Phuria\QueryBuilder\Expression\UsingExpression;
18
19
/**
20
 * @author Beniamin Jonatan Šimko <[email protected]>
21
 */
22
class ExprBuilder implements ExpressionInterface
23
{
24
    /**
25
     * @var ExpressionInterface $wrappedExpression
26
     */
27
    private $wrappedExpression;
28
29
    /**
30
     * ExprBuilder constructor.
31
     */
32 25
    public function __construct()
33
    {
34 25
        $this->wrappedExpression = static::normalizeExpression(func_get_args());
35 25
    }
36
    /**
37
     * @param mixed $expression
38
     *
39
     * @return ExpressionInterface
40
     */
41 31
    public static function normalizeExpression($expression)
42
    {
43 31
        if ($expression instanceof ExpressionInterface) {
44 24
            return $expression;
45
        }
46
47 31
        if (is_array($expression) && 1 === count($expression)) {
48 30
            return static::normalizeExpression($expression[0]);
49
        }
50
51 28
        if (is_array($expression)) {
52 12
            $normalized = [];
53
54 12
            foreach ($expression as $exp) {
55 12
                $normalized[] = static::normalizeExpression($exp);
56 12
            }
57
58 12
            return new ImplodeExpression($normalized);
59
        }
60
61 28
        if ('' === $expression || null === $expression) {
62
            return new EmptyExpression();
63
        }
64
65 28
        if (is_scalar($expression)) {
66 28
            return new RawExpression($expression);
67
        }
68
69
        throw new \InvalidArgumentException('Invalid argument.');
70
    }
71
72
    /**
73
     * @return ExpressionInterface
74
     */
75 1
    public function getWrappedExpression()
76
    {
77 1
        return $this->wrappedExpression;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 25
    public function compile()
84
    {
85 25
        return $this->wrappedExpression->compile();
86
    }
87
88
    /**
89
     * @param mixed $alias
90
     *
91
     * @return ExprBuilder
92
     */
93 3
    public function alias($alias)
94
    {
95 3
        $alias = static::normalizeExpression($alias);
96
97 3
        return new self(new AliasExpression($this->wrappedExpression, $alias));
98
    }
99
100
    /**
101
     * @return ExprBuilder
102
     */
103
    public function sumNullable()
104
    {
105
        return $this->ifNull('0')->sum();
106
    }
107
108
    /**
109
     * @return ExprBuilder
110
     */
111 1
    public function in()
112
    {
113 1
        $arguments = static::normalizeExpression(func_get_args());
114
115 1
        return new self(new InExpression($this->wrappedExpression, $arguments));
116
    }
117
118
    /**
119
     * @return ExprBuilder
120
     */
121 1
    public function using()
122
    {
123 1
        return new self(new UsingExpression($this->wrappedExpression));
124
    }
125
126
    /**
127
     * @return ExprBuilder
128
     */
129 1
    public function desc()
130
    {
131 1
        return new self(new DescExpression($this->wrappedExpression));
132
    }
133
134
    /**
135
     * @return ExprBuilder
136
     */
137 1
    public function asc()
138
    {
139 1
        return new self(new AscExpression($this->wrappedExpression));
140
    }
141
142
    /**
143
     * @param string $connector
144
     * @param mixed  $expression
145
     *
146
     * @return ExprBuilder
147
     */
148 1
    public function conjunction($connector, $expression)
149
    {
150 1
        $expression = static::normalizeExpression($expression);
151
152 1
        return new self(new ConjunctionExpression($this, $connector, $expression));
153
    }
154
155
    ###############################
156
    ### COMPARISION EXPRESSIONS ###
157
    ###############################
158
159
    /**
160
     * @param mixed $from
161
     * @param mixed $to
162
     *
163
     * @return ExprBuilder
164
     */
165 1
    public function between($from, $to)
166
    {
167 1
        $from = static::normalizeExpression($from);
168 1
        $to = static::normalizeExpression($to);
169
170 1
        return new self(new Comparison\Between($this->wrappedExpression, $from, $to));
171
    }
172
173
    /**
174
     * @param mixed $expression
175
     *
176
     * @return ExprBuilder
177
     */
178
    public function eq($expression)
179
    {
180
        $expression = static::normalizeExpression($expression);
181
182
        return $this->conjunction(ConjunctionExpression::SYMBOL_EQ, $expression);
183
    }
184
185
    /**
186
     * @param mixed $expression
187
     *
188
     * @return ExprBuilder
189
     */
190 1
    public function gt($expression)
191
    {
192 1
        $expression = static::normalizeExpression($expression);
193
194 1
        return $this->conjunction(ConjunctionExpression::SYMBOL_GT, $expression);
195
    }
196
197
    /**
198
     * @param mixed $expression
199
     *
200
     * @return ExprBuilder
201
     */
202
    public function gte($expression)
203
    {
204
        $expression = static::normalizeExpression($expression);
205
206
        return $this->conjunction(ConjunctionExpression::SYMBOL_GTE, $expression);
207
    }
208
209
    /**
210
     * @return ExprBuilder
211
     */
212
    public function isNull()
213
    {
214
        return new self(new Comparison\IsNull($this->wrappedExpression));
215
    }
216
217
    /**
218
     * @param mixed $expression
219
     *
220
     * @return ExprBuilder
221
     */
222
    public function lt($expression)
223
    {
224
        $expression = static::normalizeExpression($expression);
225
226
        return $this->conjunction(ConjunctionExpression::SYMBOL_LT, $expression);
227
    }
228
229
    /**
230
     * @param mixed $expression
231
     *
232
     * @return ExprBuilder
233
     */
234
    public function lte($expression)
235
    {
236
        $expression = static::normalizeExpression($expression);
237
238
        return $this->conjunction(ConjunctionExpression::SYMBOL_LTE, $expression);
239
    }
240
241
    /**
242
     * @param mixed $expression
243
     *
244
     * @return ExprBuilder
245
     */
246
    public function neq($expression)
247
    {
248
        $expression = static::normalizeExpression($expression);
249
250
        return $this->conjunction(ConjunctionExpression::SYMBOL_NEQ, $expression);
251
    }
252
253
    /**
254
     * @param mixed $from
255
     * @param mixed $to
256
     *
257
     * @return ExprBuilder
258
     */
259 1
    public function notBetween($from, $to)
260
    {
261 1
        $from = static::normalizeExpression($from);
262 1
        $to = static::normalizeExpression($to);
263
264 1
        return new self(new Comparison\NotBetween($this->wrappedExpression, $from, $to));
265
    }
266
267
    /**
268
     * @param mixed $expression
269
     *
270
     * @return ExprBuilder
271
     */
272
    public function nullEq($expression)
273
    {
274
        $expression = static::normalizeExpression($expression);
275
276
        return $this->conjunction(ConjunctionExpression::SYMBOL_NULL_EQ, $expression);
277
    }
278
279
    ##############################
280
    ### ARITHMETIC EXPRESSIONS ###
281
    ##############################
282
283
    /**
284
     * @param mixed $right
285
     *
286
     * @return ExprBuilder
287
     */
288 1
    public function add($right)
289
    {
290 1
        $right = static::normalizeExpression($right);
291
292 1
        return new self(new Arithmetic\Add($this->wrappedExpression, $right));
293
    }
294
295
    /**
296
     * @param mixed $right
297
     *
298
     * @return ExprBuilder
299
     */
300
    public function div($right)
301
    {
302
        $right = static::normalizeExpression($right);
303
304
        return new self(new Arithmetic\Div($this->wrappedExpression, $right));
305
    }
306
307
    /**
308
     * @param mixed $right
309
     *
310
     * @return ExprBuilder
311
     */
312
    public function divide($right)
313
    {
314
        $right = static::normalizeExpression($right);
315
316
        return new self(new Arithmetic\Divide($this->wrappedExpression, $right));
317
    }
318
319
    /**
320
     * @param mixed $right
321
     *
322
     * @return ExprBuilder
323
     */
324
    public function modulo($right)
325
    {
326
        $right = static::normalizeExpression($right);
327
328
        return new self(new Arithmetic\Modulo($this->wrappedExpression, $right));
329
    }
330
331
    /**
332
     * @param mixed $right
333
     *
334
     * @return ExprBuilder
335
     */
336
    public function multiply($right)
337
    {
338
        $right = static::normalizeExpression($right);
339
340
        return new self(new Arithmetic\Multiply($this->wrappedExpression, $right));
341
    }
342
343
    /**
344
     * @param mixed $right
345
     *
346
     * @return ExprBuilder
347
     */
348
    public function subtract($right)
349
    {
350
        $right = static::normalizeExpression($right);
351
352
        return new self(new Arithmetic\Subtract($this->wrappedExpression, $right));
353
    }
354
355
    #################
356
    ### FUNCTIONS ###
357
    #################
358
359
    /**
360
     * @return ExprBuilder
361
     */
362
    public function asci()
363
    {
364
        return new self(new Func\Asci($this->wrappedExpression));
365
    }
366
367
    /**
368
     * @return ExprBuilder
369
     */
370
    public function bin()
371
    {
372
        return new self(new Func\Bin($this->wrappedExpression));
373
    }
374
375
    /**
376
     * @return ExprBuilder
377
     */
378
    public function bitLength()
379
    {
380
        return new self(new Func\BitLength($this->wrappedExpression));
381
    }
382
383
    /**
384
     * @return ExprBuilder
385
     */
386 1
    public function char()
387
    {
388 1
        return new self(new Func\Char($this->wrappedExpression));
389
    }
390
391
    /**
392
     * @return ExprBuilder
393
     */
394 1
    public function coalesce()
395
    {
396 1
        return new self(new Func\Coalesce($this->wrappedExpression));
397
    }
398
399
    /**
400
     * @return ExprBuilder
401
     */
402
    public function concat()
403
    {
404
        return new self(new Func\Concat($this->wrappedExpression));
405
    }
406
407
    /**
408
     * @return ExprBuilder
409
     */
410
    public function concatWs()
411
    {
412
        return new self(new Func\Concat($this->wrappedExpression));
413
    }
414
415
    /**
416
     * @return ExprBuilder
417
     */
418
    public function elt()
419
    {
420
        return new self(new Func\Elt($this->wrappedExpression));
421
    }
422
423
    /**
424
     * @return ExprBuilder
425
     */
426
    public function exportSet()
427
    {
428
        return new self(new Func\ExportSet($this->wrappedExpression));
429
    }
430
431
    /**
432
     * @return ExprBuilder
433
     */
434
    public function field()
435
    {
436
        return new self(new Func\Field($this->wrappedExpression));
437
    }
438
439
    /**
440
     * @param mixed $expression
441
     *
442
     * @return ExprBuilder
443
     */
444 1
    public function ifNull($expression)
445
    {
446 1
        $expression = static::normalizeExpression($expression);
447
448 1
        return new self(new Func\IfNull($this->wrappedExpression, $expression));
449
    }
450
451
    /**
452
     * @return ExprBuilder
453
     */
454 2
    public function max()
455
    {
456 2
        return new self(new Func\Max($this->wrappedExpression));
457
    }
458
459
    /**
460
     * @return ExprBuilder
461
     */
462 4
    public function sum()
463
    {
464 4
        return new self(new Func\Sum($this->wrappedExpression));
465
    }
466
467
    /**
468
     * @return ExprBuilder
469
     */
470 1
    public function year()
471
    {
472 1
        return new self(new Func\Year($this->wrappedExpression));
473
    }
474
}