ColumnsTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getAlias() 0 1 ?
getColumns() 0 1 ?
A mountColumns() 0 24 4
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Loaders\Traits;
9
10
use Spiral\Database\Builders\SelectQuery;
11
12
/**
13
 * Provides ability to add aliased columns into SelectQuery.
14
 */
15
trait ColumnsTrait
16
{
17
    /**
18
     * Set columns into SelectQuery.
19
     *
20
     * @param SelectQuery $query
21
     * @param bool        $minify    Minify column names (will work in case when query parsed in
22
     *                               FETCH_NUM mode).
23
     * @param string      $prefix    Prefix to be added for each column name.
24
     * @param bool        $overwrite When set to true existed columns will be removed.
25
     */
26
    protected function mountColumns(
27
        SelectQuery $query,
28
        bool $minify = false,
29
        string $prefix = '',
30
        bool $overwrite = false
31
    ) {
32
        //Column source alias
33
        $alias = $this->getAlias();
34
35
        $columns = $overwrite ? [] : $query->getColumns();
36
        foreach ($this->getColumns() as $name) {
37
            $column = $name;
38
39
            if ($minify) {
40
                //Let's use column number instead of full name
41
                $column = 'c' . count($columns);
42
            }
43
44
            $columns[] = "{$alias}.{$name} AS {$prefix}{$column}";
45
        }
46
47
        //Updating column set
48
        $query->columns($columns);
49
    }
50
51
    /**
52
     * Joined table alias.
53
     *
54
     * @return string
55
     */
56
    abstract protected function getAlias(): string;
57
58
    /**
59
     * list of columns to be loaded.
60
     *
61
     * @return array
62
     */
63
    abstract protected function getColumns(): array;
64
}