Passed
Push — master ( 4ac107...bec696 )
by Aleksandar
72:47
created

ValueColumnTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 42
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A buildValueColumn() 0 21 5
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\MySql\Builder\Builders\Traits;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState;
22
23
trait ValueColumnTrait
24
{
25
    use WrapValueTrait;
26
    use SubQueryTrait;
27
    use QuoteNameTrait;
28
29
    /**
30
     * Build a value/column item.
31
     *
32
     * This method accepts following:
33
     * ```
34
     * StructuredQuery
35
     * ['value', value]
36
     * ['column', columnName]
37
     * ```
38
     *
39
     * @param array|StructuredQuery $item Item to be built.
40
     * @param MySqlQueryBuilderState $state Current query builder state
41
     * @return mixed|string
42
     * @throws \Exception
43
     */
44 6
    protected function buildValueColumn($item, MySqlQueryBuilderState $state)
45
    {
46 6
        if ($item instanceof StructuredQuery) {
47 3
            return $this->buildSubQuery($item, $state);
48
        }
49
50 6
        if (!is_array($item)) {
1 ignored issue
show
introduced by
The condition is_array($item) is always true.
Loading history...
51 2
            throw new \Exception('Param can only be an array or an instance of StructuredQuery.');
52
        }
53
54 6
        [$type, $data] = $item;
55
56 6
        if ($type === 'value') {
57 4
            return $this->buildWrapValue($data, $state);
58
        }
59
60 4
        if ($type === 'column') {
61 2
            return $this->quoteName($data);
62
        }
63
64 2
        throw new \Exception('Invalid type: ' . $type);
65
    }
66
}