|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Classes\Db; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
7
|
|
|
* @link https://github.com/pedro151/ORM-Generator |
|
8
|
|
|
*/ |
|
9
|
|
|
class DbTable |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final private function __construct () |
|
15
|
|
|
{ |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return \Classes\Db\DbTable |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function getInstance(){ |
|
22
|
|
|
return new self(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $name; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $schema; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $database; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \Classes\Db\Column[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $columns = array (); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $namespace; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @type \Classes\Db\Column[] |
|
52
|
|
|
*/ |
|
53
|
|
|
private $primarykeys = array (); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @type \Classes\Db\Column[] |
|
57
|
|
|
*/ |
|
58
|
|
|
private $foreingkeys = array (); |
|
59
|
|
|
|
|
60
|
|
|
private $dependence = array (); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @type string[] |
|
64
|
|
|
*/ |
|
65
|
|
|
private $sequence = array (); |
|
66
|
|
|
|
|
67
|
|
|
public function populate ( $array ) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->name = $array[ 'table' ]; |
|
70
|
|
|
$this->schema = isset( $array[ 'schema' ] ) ? $array[ 'schema' ] : null; |
|
71
|
|
|
$this->database = $array[ 'database' ]; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* adiciona uma coluna |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Classes\Db\Column $column |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addColumn ( Column $column ) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->columns[ $column->getName () ] = $column; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $columnName |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Classes\Db\Column |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getColumn ( $columnName ) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->columns[ $columnName ]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function hasColumn ( $columnName ) |
|
99
|
|
|
{ |
|
100
|
|
|
return isset( $this->columns[ $columnName ] ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return \Classes\Db\Column[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getPrimaryKeys () |
|
107
|
|
|
{ |
|
108
|
|
|
if ( empty ( $this->primarykeys ) ) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->primarykeys = array_filter ( $this->columns , function ( $column ){ return $column->isPrimaryKey (); } ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->primarykeys; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function hasPrimaryKey () |
|
120
|
|
|
{ |
|
121
|
|
|
if ( empty ( $this->sequence ) ) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->getPrimaryKeys (); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return ! empty ( $this->sequence ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return \Classes\Db\Column[] |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getForeingkeys () |
|
133
|
|
|
{ |
|
134
|
|
|
|
|
135
|
|
|
if ( empty ( $this->foreingkeys ) ) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->foreingkeys = array_filter ( $this->columns , function ( $column ){ return $column->isForeingkey (); } ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this->foreingkeys; |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return \Classes\Db\Column[] |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getDependences () |
|
148
|
|
|
{ |
|
149
|
|
|
if ( empty ( $this->dependence ) ) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->dependence = array_filter ( $this->columns , function ( $column ){ return $column->hasDependence (); } ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $this->dependence; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return string[] |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getSequences () |
|
161
|
|
|
{ |
|
162
|
|
|
if ( empty ( $this->sequence ) ) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->sequence = array_filter ( $this->columns , function ( $column ){ return $column->hasSequence (); } ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $this->sequence; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
|
|
public function hasSequences () |
|
174
|
|
|
{ |
|
175
|
|
|
if ( empty ( $this->sequence ) ) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->getSequences (); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return ! empty ( $this->sequence ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return \Classes\Db\Column[] |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getColumns () |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->columns; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getName () |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->name; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getSchema () |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->schema; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return bool |
|
209
|
|
|
*/ |
|
210
|
|
|
public function hasSchema () |
|
211
|
|
|
{ |
|
212
|
|
|
return (bool) $this->schema; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getDatabase () |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->database; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getNamespace () |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->namespace; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $Namespace |
|
|
|
|
|
|
233
|
|
|
*/ |
|
234
|
|
|
public function setNamespace ( $namespace ) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->namespace = $namespace; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.