Passed
Push — master ( b979f8...3829ec )
by Aleksandar
07:20 queued 11s
created

RawBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 61
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastParts() 0 3 1
A mergeParams() 0 16 3
A getRequiredParts() 0 3 1
A getPartBuilders() 0 5 1
A getInitialParts() 0 3 1
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\Pdo\MySql\Builders;
19
20
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState;
21
use ArekX\PQL\Sql\Query\Raw;
22
23
/**
24
 * Represents a query builder for building raw queries.
25
 *
26
 * @see Raw
27
 */
28
class RawBuilder extends QueryPartBuilder
29
{
30
    /**
31
     * @inheritDoc
32
     */
33 44
    protected function getInitialParts(): array
34
    {
35 44
        return [];
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 44
    protected function getRequiredParts(): array
42
    {
43 44
        return ['query'];
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 44
    protected function getPartBuilders(): array
50
    {
51
        return [
52 44
            'query' => fn($part) => $part,
53 44
            'params' => fn($part, MySqlQueryBuilderState $state) => $this->mergeParams($part, $state)
54 44
        ];
55
    }
56
57
    /**
58
     * Merges params set for this query into the parameters of the
59
     * currently passed state.
60
     *
61
     * @param array $params Parameters to be merged.
62
     * @param MySqlQueryBuilderState $state Query builder state.
63
     * @return null Null is always returned as this function only parses the parameters.
64
     */
65 3
    protected function mergeParams($params, MySqlQueryBuilderState $state)
66
    {
67 3
        $paramsBuilder = $state->getParamsBuilder();
68
69 3
        foreach ($params as $key => $value) {
70 3
            $type = null;
71 3
            $paramValue = $value;
72
73 3
            if (is_array($value)) {
74 2
                [$paramValue, $type] = $value;
75
            }
76
77 3
            $paramsBuilder->add($key, $paramValue, $type);
78
        }
79
80 3
        return null;
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86 44
    protected function getLastParts(): array
87
    {
88 44
        return [];
89
    }
90
}