ColumnList::generate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
use \BfwSql\Queries\AbstractQuery;
6
7
class ColumnList extends AbstractList
8
{
9
    /**
10
     * @var \BfwSql\Queries\Parts\Table $table The table where are columns
11
     */
12
    protected $table;
13
    
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $separator = ',';
18
    
19
    /**
20
     * {@inheritdoc}
21
     * 
22
     * @param \BfwSql\Queries\AbstractQuery $querySystem
23
     * @param \BfwSql\Queries\Parts\Table $table The table where are column
24
     */
25
    public function __construct(AbstractQuery $querySystem, Table $table)
26
    {
27
        parent::__construct($querySystem);
28
        $this->table = $table;
29
    }
30
    
31
    /**
32
     * Getter accessor to property table
33
     * 
34
     * @return \BfwSql\Queries\Parts\Table
35
     */
36
    public function getTable(): Table
37
    {
38
        return $this->table;
39
    }
40
    
41
    /**
42
     * Magic method __invoke, used when the user call object like a function
43
     * @link http://php.net/manual/en/language.oop5.magic.php#object.invoke
44
     * 
45
     * @param array $columns The list of columns to declare
46
     *  The key into the array is the shortcut of the column.
47
     *  The value is the column name.
48
     *  If no key is defined (so integer), the column will not have shortcut.
49
     * 
50
     * @return void
51
     */
52
    public function __invoke(array $columns)
53
    {
54
        $this->invokeCheckIsDisabled();
55
        
56
        $usedClass   = \BfwSql\UsedClass::getInstance();
57
        $columnClass = $usedClass->obtainClassNameToUse('QueriesPartsColumn');
58
        
59
        foreach ($columns as $shortcut => $name) {
60
            if (is_int($shortcut)) {
61
                $column = new $columnClass($this->table, $name);
62
            } else {
63
                $column = new $columnClass($this->table, $name, $shortcut);
64
            }
65
            
66
            $this->list[] = $column;
67
        }
68
    }
69
    
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function generate(): string
74
    {
75
        if ($this->isDisabled === true) {
76
            return '';
77
        }
78
        
79
        $sqlPart = '';
80
        
81
        foreach ($this->list as $index => $column) {
82
            $sqlPart .= $this->querySystem->getQuerySgbd()->listItem(
83
                $column->generate(),
84
                $index,
85
                $this->separator
86
            );
87
        }
88
        
89
        return $sqlPart;
90
    }
91
}
92