Passed
Push — master ( cc4838...c78d17 )
by Aleksandar
02:19
created

ValueColumnTrait::buildValueColumn()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
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
use Exception;
23
24
trait ValueColumnTrait
25
{
26
    use WrapValueTrait;
27
    use SubQueryTrait;
28
    use QuoteNameTrait;
29
30
    /**
31
     * Build a value/column item.
32
     *
33
     * This method accepts following:
34
     * ```
35
     * StructuredQuery
36
     * ['value', value]
37
     * ['column', columnName]
38
     * ```
39
     *
40
     * @param array|StructuredQuery $item Item to be built.
41
     * @param MySqlQueryBuilderState $state Current query builder state
42
     * @return mixed|string
43
     * @throws Exception
44
     */
45 6
    protected function buildValueColumn(StructuredQuery|array $item, MySqlQueryBuilderState $state): mixed
46
    {
47 6
        if ($item instanceof StructuredQuery) {
1 ignored issue
show
introduced by
$item is never a sub-type of ArekX\PQL\Contracts\StructuredQuery.
Loading history...
48 3
            return $this->buildSubQuery($item, $state);
49
        }
50
51 6
        [$type, $data] = $item;
52
53 6
        if ($type === 'value') {
54 4
            return $this->buildWrapValue($data, $state);
55
        }
56
57 4
        if ($type === 'column') {
58 2
            return $this->quoteName($data);
59
        }
60
61 2
        throw new \UnexpectedValueException('Invalid type: ' . $type);
62
    }
63
}
64