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\ColumnList|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\ColumnList|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->invokeCheckIsDisabled(); |
106
|
|
|
|
107
|
|
|
$this->defineNameAndShortcut($nameInfos); |
108
|
|
|
$this->defineColumns($columns); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Find the table name and the shortcut from info passed in argument. |
113
|
|
|
* |
114
|
|
|
* @param string|array $nameInfos Table name. |
115
|
|
|
* It can be an array if a table shortcut is declared. |
116
|
|
|
* In array mode, the format is ['shortcut' => 'name'] |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* |
120
|
|
|
* @throws \Exception If the format is not correct (string or array) |
121
|
|
|
*/ |
122
|
|
|
protected function defineNameAndShortcut($nameInfos) |
123
|
|
|
{ |
124
|
|
|
if (!is_array($nameInfos) && !is_string($nameInfos)) { |
|
|
|
|
125
|
|
|
throw new Exception( |
126
|
|
|
'Table information is not in the right format.', |
127
|
|
|
self::ERR_TABLE_INFOS_BAD_FORMAT |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (is_array($nameInfos)) { |
132
|
|
|
$this->name = (string) reset($nameInfos); |
133
|
|
|
$this->shortcut = (string) key($nameInfos); |
134
|
|
|
} else { |
135
|
|
|
$this->name = (string) $nameInfos; |
136
|
|
|
$this->shortcut = null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!empty($this->tablePrefix)) { |
140
|
|
|
$this->name = $this->tablePrefix.$this->name; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Define the columns object and declare columns passed in argument |
146
|
|
|
* |
147
|
|
|
* @param string|array|null $columns (default: null) Columns into this |
148
|
|
|
* table which will be use on the final query |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function defineColumns($columns) |
153
|
|
|
{ |
154
|
|
|
$class = $this->obtainColumnsClassName(); |
155
|
|
|
$this->columns = new $class($this->querySystem, $this); |
156
|
|
|
|
157
|
|
|
if ($columns === null) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (!is_array($columns)) { |
162
|
|
|
$columns = [$columns]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->columns->__invoke($columns); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Find the correct class (with namespace) to use for columns object. |
170
|
|
|
* Based on the value of the property columnWithValue. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function obtainColumnsClassName(): string |
175
|
|
|
{ |
176
|
|
|
if ($this->columnsWithValue === true) { |
177
|
|
|
return __NAMESPACE__.'\ColumnValueList'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return __NAMESPACE__.'\ColumnList'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Instanciate the columnsList object. |
185
|
|
|
* To generate it before the call to __invoke() when is necessary |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function createColumnInstance() |
190
|
|
|
{ |
191
|
|
|
$this->defineColumns(null); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function generate(): string |
198
|
|
|
{ |
199
|
|
|
if ($this->isDisabled === true) { |
200
|
|
|
return ''; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->querySystem |
204
|
|
|
->getQuerySgbd() |
205
|
|
|
->table($this->name, $this->shortcut) |
206
|
|
|
; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|