Completed
Push — master ( f1592e...b3630f )
by Daniel
03:25 queued 01:30
created

ArrayConverter::walkJoins()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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