1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Classes\Db; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\AbstractAdapter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Colunas dos bancos |
9
|
|
|
* |
10
|
|
|
* @author Pedro Alarcao <[email protected]> |
11
|
|
|
* @link https://github.com/pedro151/orm-generator |
12
|
|
|
*/ |
13
|
|
|
class Column |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Colunas dos bancos |
18
|
|
|
* |
19
|
|
|
* @author Pedro Alarcao <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final private function __construct () |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* create instance |
27
|
|
|
* |
28
|
|
|
* @return \Classes\Db\Column |
29
|
|
|
*/ |
30
|
|
|
public static function getInstance () |
31
|
|
|
{ |
32
|
|
|
return new Column(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Classes\Db\Constrant[] |
42
|
|
|
*/ |
43
|
|
|
private $primarykey; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $type; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $comment; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var boolean |
57
|
|
|
*/ |
58
|
|
|
private $nullable; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
private $max_length; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var \Classes\Db\Constrant[] |
67
|
|
|
*/ |
68
|
|
|
private $dependences; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \Classes\Db\Constrant |
72
|
|
|
*/ |
73
|
|
|
private $refForeingkey; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @type string |
77
|
|
|
*/ |
78
|
|
|
private $sequence; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getName () |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* popula o |
90
|
|
|
* |
91
|
|
|
* @param $array |
92
|
|
|
*/ |
93
|
|
|
public function populate ( $array ) |
94
|
|
|
{ |
95
|
|
|
$this->name = $array[ 'name' ]; |
96
|
|
|
$this->type = $array[ 'type' ]; |
97
|
|
|
$this->nullable = $array[ 'nullable' ]; |
98
|
|
|
$this->max_length = $array[ 'max_length' ]; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
public function isPrimaryKey () |
107
|
|
|
{ |
108
|
|
|
return !empty( $this->primarykey ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function isForeingkey () |
115
|
|
|
{ |
116
|
|
|
return !empty( $this->refForeingkey ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function hasDependence () |
123
|
|
|
{ |
124
|
|
|
return !empty( $this->dependences ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getType ( $inPHP = true ) |
131
|
|
|
{ |
132
|
|
|
if ( !$inPHP ) { |
133
|
|
|
return $this->type; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return AbstractAdapter::convertTypeToPHP ( $this->type ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $type |
141
|
|
|
* @param bool $inPHP |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function equalType ( $type, $inPHP = true ) |
146
|
|
|
{ |
147
|
|
|
return $this->getType ( $inPHP ) === $type; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param AbstractAdapter $type |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public function getTypeByConfig ( AbstractAdapter $type ) |
156
|
|
|
{ |
157
|
|
|
return $type->convertTypeToTypeFramework ( $this->getType ( false ) ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getComment () |
164
|
|
|
{ |
165
|
|
|
return $this->comment; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $comment |
170
|
|
|
*/ |
171
|
|
|
public function setComment ( $comment ) |
172
|
|
|
{ |
173
|
|
|
$this->comment = $comment; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param \Classes\Db\Constrant $primarykey |
180
|
|
|
*/ |
181
|
|
|
public function setPrimaryKey ( Constrant $primarykey ) |
182
|
|
|
{ |
183
|
|
|
$this->primarykey = $primarykey; |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param \Classes\Db\Constrant $dependece |
190
|
|
|
*/ |
191
|
|
|
public function addDependece ( Constrant $dependece ) |
192
|
|
|
{ |
193
|
|
|
$this->dependences[] = $dependece; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $constraint_name |
200
|
|
|
* @param $table_name |
201
|
|
|
* @param $column_name |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function createDependece ( $constraint_name, $table_name, $column_name, $database, $schema = null ) |
206
|
|
|
{ |
207
|
|
|
$objConstrantDependence = Constrant::getInstance () |
208
|
|
|
->populate ( |
209
|
|
|
array ( |
210
|
|
|
'constrant' => $constraint_name, |
211
|
|
|
'schema' => $schema, |
212
|
|
|
'table' => $table_name, |
213
|
|
|
'column' => $column_name, |
214
|
|
|
'database' => $database |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$this->addDependece ( $objConstrantDependence ); |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param \Classes\Db\Constrant $reference |
225
|
|
|
*/ |
226
|
|
|
public function addRefFk ( Constrant $reference ) |
227
|
|
|
{ |
228
|
|
|
$this->refForeingkey = $reference; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* retorna as foreingkeys |
235
|
|
|
* |
236
|
|
|
* @return \Classes\Db\Constrant |
237
|
|
|
*/ |
238
|
|
|
public function getFks () |
239
|
|
|
{ |
240
|
|
|
return $this->refForeingkey; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Retorna as dependencias da tabela |
245
|
|
|
* |
246
|
|
|
* @return \Classes\Db\Constrant[] |
247
|
|
|
*/ |
248
|
|
|
public function getDependences () |
249
|
|
|
{ |
250
|
|
|
return $this->dependences; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
public function hasDependences () |
257
|
|
|
{ |
258
|
|
|
return (bool) count ( $this->dependences ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Retorna a constrant da primarykey da tabela |
263
|
|
|
* |
264
|
|
|
* @return \Classes\Db\Constrant[] |
265
|
|
|
*/ |
266
|
|
|
public function getPrimaryKey () |
267
|
|
|
{ |
268
|
|
|
return $this->primarykey; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* |
273
|
|
|
*/ |
274
|
|
|
public function getMaxLength () |
275
|
|
|
{ |
276
|
|
|
return $this->max_length; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function hasSequence () |
283
|
|
|
{ |
284
|
|
|
return (bool) $this->sequence; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function getSequence () |
291
|
|
|
{ |
292
|
|
|
return $this->sequence; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $sequence |
297
|
|
|
*/ |
298
|
|
|
public function setSequence ( $sequence ) |
299
|
|
|
{ |
300
|
|
|
$this->sequence = $sequence; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return boolean |
305
|
|
|
*/ |
306
|
|
|
public function isNullable () |
307
|
|
|
{ |
308
|
|
|
return $this->nullable; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..