Completed
Branch 2.0 (acba87)
by Vermeulen
02:20
created

Table::defineNameAndShortcut()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 1
dl 0
loc 19
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
use \Exception;
6
7
class Table extends AbstractPart
8
{
9
    /**
10
     * @const ERR_TABLE_INFOS_BAD_FORMAT Exception if the datas which contains
11
     * table name and shortcut have not the expected format.
12
     */
13
    const ERR_TABLE_INFOS_BAD_FORMAT = 2515001;
14
    
15
    /**
16
     * @var string $name The table name
17
     */
18
    protected $name = '';
19
    
20
    /**
21
     * @var string|null $shortcut The table shortcut
22
     */
23
    protected $shortcut;
24
    
25
    /**
26
     * @var \BfwSql\Queries\Parts\ColumnsList|null $columns Object containing
0 ignored issues
show
Bug introduced by
The type BfwSql\Queries\Parts\ColumnsList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
     * all columns of this table to use into the request
28
     */
29
    protected $columns;
30
    
31
    /**
32
     * @var boolean $columnsWithValue If the columnList object will contain
33
     * the column value or not (change the __invoke() format).
34
     */
35
    protected $columnsWithValue = false;
36
    
37
    /**
38
     * Getter accessor to property name
39
     * 
40
     * @return string
41
     */
42
    public function getName(): string
43
    {
44
        return $this->name;
45
    }
46
    
47
    /**
48
     * Getter accessor to property shortcut
49
     * 
50
     * @return string|null
51
     */
52
    public function getShortcut()
53
    {
54
        return $this->shortcut;
55
    }
56
57
    /**
58
     * Getter accessor to property columns
59
     * 
60
     * @return \BfwSql\Queries\Parts\ColumnsList|null
61
     */
62
    public function getColumns()
63
    {
64
        return $this->columns;
65
    }
66
    
67
    /**
68
     * Getter accessor to property columnsWithValue
69
     * 
70
     * @return bool
71
     */
72
    public function getColumnsWithValue(): bool
73
    {
74
        return $this->columnsWithValue;
75
    }
76
    
77
    /**
78
     * Setter accessor to property columnsWithValue
79
     * 
80
     * @param bool $columnsWithValue If the columnList object will contain
81
     * the column value or not (change the __invoke() format).
82
     * 
83
     * @return $this
84
     */
85
    public function setColumnsWithValue(bool $columnsWithValue): self
86
    {
87
        $this->columnsWithValue = $columnsWithValue;
88
        return $this;
89
    }
90
    
91
    /**
92
     * Magic method __invoke, used when the user call object like a function
93
     * @link http://php.net/manual/en/language.oop5.magic.php#object.invoke
94
     * 
95
     * @param string|array $nameInfos Table name.
96
     *  It can be an array if a table shortcut is declared.
97
     *  In array mode, the format is ['shortcut' => 'name']
98
     * @param string|array|null $columns (default: null) Columns into this
99
     *  table which will be use on the final query
100
     * 
101
     * @return void
102
     */
103
    public function __invoke($nameInfos, $columns = null)
104
    {
105
        $this->defineNameAndShortcut($nameInfos);
106
        $this->defineColumns($columns);
107
    }
108
    
109
    /**
110
     * Find the table name and the shortcut from info passed in argument.
111
     * 
112
     * @param string|array $nameInfos Table name.
113
     *  It can be an array if a table shortcut is declared.
114
     *  In array mode, the format is ['shortcut' => 'name']
115
     * 
116
     * @return void
117
     * 
118
     * @throws \Exception If the format is not correct (string or array)
119
     */
120
    protected function defineNameAndShortcut($nameInfos)
121
    {
122
        if (!is_array($nameInfos) && !is_string($nameInfos)) {
1 ignored issue
show
introduced by
The condition is_string($nameInfos) is always true.
Loading history...
123
            throw new Exception(
124
                'Table information is not in the right format.',
125
                self::ERR_TABLE_INFOS_BAD_FORMAT
126
            );
127
        }
128
        
129
        if (is_array($nameInfos)) {
130
            $this->name     = (string) reset($nameInfos);
131
            $this->shortcut = (string) key($nameInfos);
132
        } else {
133
            $this->name     = (string) $nameInfos;
134
            $this->shortcut = null;
135
        }
136
        
137
        if (!empty($this->tablePrefix)) {
138
            $this->name = $this->tablePrefix.$this->name;
139
        }
140
    }
141
    
142
    /**
143
     * Define the columns object and declare columns passed in argument
144
     * 
145
     * @param string|array|null $columns (default: null) Columns into this
146
     *  table which will be use on the final query
147
     * 
148
     * @return void
149
     */
150
    protected function defineColumns($columns)
151
    {
152
        $class         = $this->obtainColumnsClassName();
153
        $this->columns = new $class($this->querySystem, $this);
154
        
155
        if ($columns === null) {
156
            return;
157
        }
158
        
159
        if (!is_array($columns)) {
160
            $columns = [$columns];
161
        }
162
        
163
        $this->columns->__invoke($columns);
164
    }
165
    
166
    /**
167
     * Find the correct class (with namespace) to use for columns object.
168
     * Based on the value of the property columnWithValue.
169
     * 
170
     * @return string
171
     */
172
    protected function obtainColumnsClassName(): string
173
    {
174
        if ($this->columnsWithValue === true) {
175
            return __NAMESPACE__.'\ColumnValueList';
176
        }
177
        
178
        return __NAMESPACE__.'\ColumnList';
179
    }
180
    
181
    /**
182
     * Instanciate the columnsList object.
183
     * To generate it before the call to __invoke() when is necessary
184
     */
185
    public function createColumnInstance()
186
    {
187
        $this->defineColumns(null);
188
    }
189
    
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function generate(): string
194
    {
195
        $partQuery = '`'.$this->name.'`';
196
        
197
        if ($this->shortcut !== null) {
198
            $partQuery .= ' AS `'.$this->shortcut.'`';
199
        }
200
        
201
        return $partQuery;
202
    }
203
}
204