AliasTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 55
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A buildAliasedNames() 0 28 6
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\Traits;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\Pdo\MySql\MySqlQueryBuilderState;
22
23
trait AliasTrait
24
{
25
    use QuoteNameTrait;
26
    use SubQueryTrait;
27
28
    /**
29
     * Build AS part containing names in SQL.
30
     *
31
     * If string is passed to this method, the name is quoted as is using quoteName()
32
     *
33
     * If array is passed then all values are quoted using quote names and if key of the specific
34
     * array item is string then it is added to the end AS. So following:
35
     * ```
36
     * ['alias' => 'table']
37
     * ```
38
     * Results in `table` AS `alias`.
39
     *
40
     * If a structured query is passed as a value, it is also parsed.
41
     *
42
     * If structured query is passed as a name part, it will be processed too.
43
     *
44
     * @param array|string|StructuredQuery $namePart Part to be processed.
45
     * @param MySqlQueryBuilderState $state State of the builder
46
     * @return string
47
     * @see SubQueryTrait::buildSubQuery()
48
     * @see QuoteNameTrait::quoteName()
49
     */
50 55
    protected function buildAliasedNames(StructuredQuery|array|string $namePart, MySqlQueryBuilderState $state): string
51
    {
52 55
        if ($namePart instanceof StructuredQuery) {
53 16
            return $this->buildSubQuery($namePart, $state);
54
        }
55
56 52
        if (is_string($namePart)) {
57 46
            return $this->quoteName($namePart);
58
        }
59
60 14
        $items = [];
61
62 14
        foreach ($namePart as $as => $item) {
63 14
            if ($item instanceof StructuredQuery) {
64 5
                $item = $this->buildSubQuery($item, $state);
65
            } else {
66 10
                $item = $this->quoteName($item);
67
            }
68
69
70 14
            if (is_string($as)) {
71 13
                $item .= " AS " . $this->quoteName($as);
72
            }
73
74 14
            $items[] = $item;
75
        }
76
77 14
        return implode(', ', $items);
78
    }
79
}
80