Completed
Push — master ( a41938...18c83d )
by Beniamin
04:49
created

ExprNormalizer::normalizeExpression()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.5625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 9
cts 12
cp 0.75
rs 8.8571
cc 6
eloc 11
nc 5
nop 1
crap 6.5625
1
<?php
2
namespace Phuria\QueryBuilder;
3
4
use Phuria\QueryBuilder\Expression\EmptyExpression;
5
use Phuria\QueryBuilder\Expression\ExpressionInterface;
6
use Phuria\QueryBuilder\Expression\ImplodeExpression;
7
use Phuria\QueryBuilder\Expression\RawExpression;
8
9
/**
10
 * @author Beniamin Jonatan Šimko <[email protected]>
11
 */
12
class ExprNormalizer
13
{
14
    /**
15
     * @param mixed $expression
16
     *
17
     * @return ExpressionInterface
18
     */
19 32
    public static function normalizeExpression($expression)
20
    {
21 32
        switch (true) {
22 32
            case $expression instanceof ExpressionInterface:
23 24
                return $expression;
24 32
            case is_array($expression):
25 31
                return static::normalizeArray($expression);
26 29
            case '' === $expression || null === $expression:
27
                return new EmptyExpression();
28 29
            case is_scalar($expression):
29 29
                return new RawExpression($expression);
30
        }
31
32
        throw new \InvalidArgumentException('Invalid argument.');
33
    }
34
35
    /**
36
     * @param array $expressions
37
     *
38
     * @return ExpressionInterface
39
     */
40 31
    public static function normalizeArray(array $expressions)
41
    {
42 31
        $normalized = [];
43
44 31
        foreach ($expressions as $exp) {
45 31
            $normalized[] = static::normalizeExpression($exp);
46 31
        }
47
48 31
        switch (count($normalized)) {
49 31
            case 0:
50
                return new EmptyExpression();
51 31
            case 1:
52 30
                return $normalized[0];
53 12
        }
54
55 12
        return new ImplodeExpression($normalized);
56
    }
57
}