Completed
Branch feature/pre-split (4c50c1)
by Anton
03:17
created

ColumnsTrait::mountColumns()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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