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

ExprNormalizer::normalizeArray()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 11
cts 12
cp 0.9167
rs 9.2
cc 4
eloc 10
nc 6
nop 1
crap 4.0092
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
}