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 |
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)) { |
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
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.