Completed
Pull Request — master (#20)
by Daniel
03:37 queued 01:43
created

ArrayConverter::walkCriteria()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 12
nop 2
1
<?php
2
3
namespace Psi\Component\ObjectAgent\Query\Converter;
4
5
use Psi\Component\ObjectAgent\Query\Comparison;
6
use Psi\Component\ObjectAgent\Query\Composite;
7
use Psi\Component\ObjectAgent\Query\Query;
8
use Psi\Component\ObjectAgent\Query\Join;
9
10
class ArrayConverter
11
{
12
    private static $comparisons = [
13
        Comparison::EQUALS,
14
        Comparison::NOT_EQUALS,
15
        Comparison::GREATER_THAN,
16
        Comparison::GREATER_THAN_EQUAL,
17
        Comparison::LESS_THAN,
18
        Comparison::LESS_THAN_EQUAL,
19
        Comparison::NULL,
20
        Comparison::NOT_NULL,
21
        Comparison::IN,
22
        Comparison::NOT_IN,
23
        Comparison::CONTAINS,
24
        Comparison::NOT_CONTAINS,
25
    ];
26
27
    private static $composites = [
28
        Composite::AND,
29
        Composite::OR,
30
    ];
31
32
    public function __invoke(array $query)
33
    {
34
        $query = $this->merge([
35
            'selects' => [],
36
            'from' => null,
37
            'criteria' => null,
38
            'orderings' => [],
39
            'joins' => [],
40
            'firstResult' => null,
41
            'maxResults' => null,
42
        ], $query);
43
44
        if (null === $query['from']) {
45
            throw new \InvalidArgumentException(sprintf(
46
                'You must specify the "from" part of the query.'
47
            ));
48
        }
49
50
        if ($query['criteria']) {
51
            $query['criteria'] = new Composite(Composite::AND, $this->walkCriteria($query['criteria']));
52
        }
53
54
        $query['joins'] = $this->walkJoins($query['joins']);
55
        $from = $query['from'];
56
        unset($query['from']);
57
58
        return Query::create($from, $query);
59
    }
60
61
    private function walkJoins(array $joins)
62
    {
63
        $joinObjects = [];
64
65
        foreach ($joins as $join) {
66
            $join = $this->merge([
67
                'type' => Join::INNER_JOIN,
68
                'join'=> null,
69
                'alias' => null,
70
            ], $join, [ 'join', 'alias']);
71
72
            $joinObjects[] = new Join($join['join'], $join['alias'], $join['type']);
73
        }
74
75
        return $joinObjects;
76
    }
77
78
    private function walkCriteria(array $exprs, array $original = null)
79
    {
80
        $original = $original ?: $exprs;
81
82
        $criterias = [];
83
        foreach ($exprs as $operator => $right) {
84
            if (in_array($operator, self::$comparisons)) {
85
                if (!is_array($right)) {
86
                    throw new \InvalidArgumentException(sprintf(
87
                        'Comparison must have an array as the right-sided value, got "%s"',
88
                        gettype($right)
89
                    ));
90
                }
91
92
                foreach ($right as $field => $value) {
93
                    $criterias[] = new Comparison($operator, $field, $value);
94
                }
95
96
                continue;
97
            }
98
99
            if (in_array($operator, self::$composites)) {
100
                $criterias[] = new Composite($operator, $this->walkCriteria($right, $original));
101
                continue;
102
            }
103
104
            throw new \InvalidArgumentException(sprintf(
105
                'Unknown expression operator "%s" in "%s"', $operator, $this->toString($original)
106
            ));
107
        }
108
109
        return $criterias;
110
    }
111
112
    private function toString(array $expr)
113
    {
114
        return json_encode($expr);
115
    }
116
117
    private function merge(array $defaults, array $values, array $required = [])
118
    {
119 View Code Duplication
        if ($diff = array_diff($required, array_keys($values))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            throw new \InvalidArgumentException(sprintf(
121
                'Keys "%s" are required for "%s"',
122
                implode('", "', $diff),  json_encode($values)
123
            ));
124
        }
125
126 View Code Duplication
        if ($diff = array_diff(array_keys($values), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
            throw new \InvalidArgumentException(sprintf(
128
                'Invalid query keys "%s", valid keys: "%s"',
129
                implode('", "', $diff), implode('", "', array_keys($defaults))
130
            ));
131
        }
132
133
        return array_merge($defaults, $values);
134
    }
135
}
136