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

ExprNormalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 46
ccs 20
cts 24
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalizeExpression() 0 15 6
A normalizeArray() 0 17 4
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
}