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