|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Audit\Metadata; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Audit\MySql\Metadata\AlterColumnMetadata; |
|
7
|
|
|
use SetBased\Audit\MySql\Metadata\AuditColumnMetadata; |
|
8
|
|
|
use SetBased\Audit\MySql\Metadata\ColumnMetadata; |
|
9
|
|
|
use SetBased\Exception\FallenException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Metadata of a list of table columns. |
|
13
|
|
|
*/ |
|
14
|
|
|
class TableColumnsMetadata |
|
15
|
|
|
{ |
|
16
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
17
|
|
|
/** |
|
18
|
|
|
* The metadata of the columns. |
|
19
|
|
|
* |
|
20
|
|
|
* @var ColumnMetadata[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $columns = []; |
|
23
|
|
|
|
|
24
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
25
|
|
|
/** |
|
26
|
|
|
* Object constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array[] $columns The metadata of the columns as returned by AuditDataLayer::getTableColumns(). |
|
29
|
|
|
* @param string $type The class for columns metadata. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $columns = [], string $type = 'ColumnMetadata') |
|
32
|
33 |
|
{ |
|
33
|
|
|
foreach ($columns as $column) |
|
34
|
33 |
|
{ |
|
35
|
|
|
$this->columns[$column['column_name']] = static::columnFactory($type, $column); |
|
36
|
32 |
|
} |
|
37
|
|
|
} |
|
38
|
33 |
|
|
|
39
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
40
|
|
|
/** |
|
41
|
|
|
* Combines the metadata of two lists of table columns. |
|
42
|
|
|
* |
|
43
|
|
|
* @param TableColumnsMetadata $columns1 The first metadata of a list of table columns. |
|
44
|
|
|
* @param TableColumnsMetadata $columns2 The second metadata of a list of table columns. |
|
45
|
|
|
* |
|
46
|
|
|
* @return TableColumnsMetadata |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function combine(TableColumnsMetadata $columns1, TableColumnsMetadata $columns2): TableColumnsMetadata |
|
49
|
30 |
|
{ |
|
50
|
|
|
$columns = new TableColumnsMetadata(); |
|
51
|
30 |
|
|
|
52
|
|
|
$columns->appendTableColumns($columns1); |
|
53
|
30 |
|
$columns->appendTableColumns($columns2); |
|
54
|
30 |
|
|
|
55
|
|
|
return $columns; |
|
56
|
30 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
59
|
|
|
/** |
|
60
|
|
|
* Compares two lists of table columns and returns a list of table columns that are in both lists but have different |
|
61
|
|
|
* metadata. |
|
62
|
|
|
* |
|
63
|
|
|
* @param TableColumnsMetadata $oldColumns The old metadata of the table columns. |
|
64
|
|
|
* @param TableColumnsMetadata $newColumns The new metadata of the table columns. |
|
65
|
|
|
* @param string[] $ignore The properties to be ignored. |
|
66
|
|
|
* |
|
67
|
|
|
* @return TableColumnsMetadata |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function differentColumnTypes(TableColumnsMetadata $oldColumns, |
|
70
|
30 |
|
TableColumnsMetadata $newColumns, |
|
71
|
|
|
array $ignore = []): TableColumnsMetadata |
|
72
|
|
|
{ |
|
73
|
|
|
$diff = new TableColumnsMetadata(); |
|
74
|
30 |
|
foreach ($oldColumns->columns as $columnName => $oldColumn) |
|
75
|
30 |
|
{ |
|
76
|
|
|
if (isset($newColumns->columns[$columnName])) |
|
77
|
30 |
|
{ |
|
78
|
|
|
if (!ColumnMetadata::compare($oldColumn, $newColumns->columns[$columnName], $ignore)) |
|
79
|
30 |
|
{ |
|
80
|
|
|
$diff->appendTableColumn($newColumns->columns[$columnName]); |
|
81
|
5 |
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $diff; |
|
86
|
30 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
89
|
|
|
/** |
|
90
|
|
|
* Compares two lists of table columns and returns a list of table columns that are in the first list of table columns |
|
91
|
|
|
* but not in the second list of table columns. |
|
92
|
|
|
* |
|
93
|
|
|
* @param TableColumnsMetadata $columns1 The first list of table columns. |
|
94
|
|
|
* @param TableColumnsMetadata $columns2 The second list of table columns. |
|
95
|
|
|
* |
|
96
|
|
|
* @return TableColumnsMetadata |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function notInOtherSet(TableColumnsMetadata $columns1, |
|
99
|
30 |
|
TableColumnsMetadata $columns2): TableColumnsMetadata |
|
100
|
|
|
{ |
|
101
|
|
|
$diff = new TableColumnsMetadata(); |
|
102
|
30 |
|
foreach ($columns1->columns as $columnName => $column1) |
|
103
|
30 |
|
{ |
|
104
|
|
|
if (!isset($columns2->columns[$columnName])) |
|
105
|
30 |
|
{ |
|
106
|
|
|
$diff->appendTableColumn($column1); |
|
107
|
4 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $diff; |
|
111
|
30 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
114
|
|
|
/** |
|
115
|
|
|
* A factory for table column metadata. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $type The type of the metadata. |
|
118
|
|
|
* @param array $column The metadata of the column |
|
119
|
|
|
* |
|
120
|
|
|
* @return AlterColumnMetadata|AuditColumnMetadata|ColumnMetadata |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function columnFactory(string $type, array $column): ColumnMetadata |
|
123
|
32 |
|
{ |
|
124
|
|
|
switch ($type) |
|
125
|
32 |
|
{ |
|
126
|
|
|
case 'ColumnMetadata': |
|
127
|
32 |
|
return new ColumnMetadata($column); |
|
128
|
32 |
|
|
|
129
|
|
|
case 'AlterColumnMetadata': |
|
130
|
27 |
|
return new AlterColumnMetadata($column); |
|
131
|
|
|
|
|
132
|
|
|
case 'AuditColumnMetadata': |
|
133
|
27 |
|
return new AuditColumnMetadata($column); |
|
134
|
27 |
|
|
|
135
|
|
|
default: |
|
136
|
|
|
throw new FallenException('type', $type); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
141
|
|
|
/** |
|
142
|
|
|
* Appends a table column to this list of table columns. |
|
143
|
|
|
* |
|
144
|
|
|
* @param ColumnMetadata $column The metadata of the table column. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function appendTableColumn(ColumnMetadata $column): void |
|
147
|
30 |
|
{ |
|
148
|
|
|
$this->columns[$column->getName()] = $column; |
|
149
|
30 |
|
} |
|
150
|
30 |
|
|
|
151
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
152
|
|
|
/** |
|
153
|
|
|
* Appends table columns to this list of table columns. |
|
154
|
|
|
* |
|
155
|
|
|
* @param TableColumnsMetadata $columns The metadata of the table columns. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function appendTableColumns(TableColumnsMetadata $columns): void |
|
158
|
30 |
|
{ |
|
159
|
|
|
foreach ($columns->columns as $column) |
|
160
|
30 |
|
{ |
|
161
|
|
|
$this->appendTableColumn($column); |
|
162
|
30 |
|
} |
|
163
|
|
|
} |
|
164
|
30 |
|
|
|
165
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
166
|
|
|
/** |
|
167
|
|
|
* Enhances all columns with field 'after'. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function enhanceAfter(): void |
|
170
|
30 |
|
{ |
|
171
|
|
|
$previous = null; |
|
172
|
30 |
|
foreach ($this->columns as $column) |
|
173
|
30 |
|
{ |
|
174
|
|
|
$column->setAfter($previous); |
|
175
|
30 |
|
$previous = $column->getName(); |
|
176
|
30 |
|
} |
|
177
|
|
|
} |
|
178
|
30 |
|
|
|
179
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
180
|
|
|
/** |
|
181
|
|
|
* Returns a column given the column name. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $columnName The name of the column. |
|
184
|
|
|
* |
|
185
|
|
|
* @return ColumnMetadata |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getColumn(string $columnName): ColumnMetadata |
|
188
|
6 |
|
{ |
|
189
|
|
|
return $this->columns[$columnName]; |
|
190
|
6 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
193
|
|
|
/** |
|
194
|
|
|
* Returns the columns names. |
|
195
|
|
|
* |
|
196
|
|
|
* @return string[] |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getColumnNames(): array |
|
199
|
6 |
|
{ |
|
200
|
|
|
return array_keys($this->columns); |
|
201
|
6 |
|
} |
|
202
|
|
|
|
|
203
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
204
|
|
|
/** |
|
205
|
|
|
* Returns the underlying array with metadata of this list of table columns. |
|
206
|
|
|
* |
|
207
|
|
|
* @return ColumnMetadata[] |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getColumns(): array |
|
210
|
32 |
|
{ |
|
211
|
|
|
return $this->columns; |
|
212
|
32 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
215
|
|
|
/** |
|
216
|
|
|
* Returns the length of the longest column name. |
|
217
|
|
|
* |
|
218
|
|
|
* @return int |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getLongestColumnNameLength(): int |
|
221
|
27 |
|
{ |
|
222
|
|
|
$max = 0; |
|
223
|
27 |
|
foreach ($this->columns as $column) |
|
224
|
27 |
|
{ |
|
225
|
|
|
$max = max($max, mb_strlen($column->getName())); |
|
226
|
27 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $max; |
|
229
|
27 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
232
|
|
|
/** |
|
233
|
|
|
* Returns the number of columns. |
|
234
|
|
|
* |
|
235
|
|
|
* @return int |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getNumberOfColumns(): int |
|
238
|
30 |
|
{ |
|
239
|
|
|
return count($this->columns); |
|
240
|
30 |
|
} |
|
241
|
|
|
|
|
242
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
243
|
|
|
/** |
|
244
|
|
|
* Makes all columns nullable. |
|
245
|
|
|
*/ |
|
246
|
|
|
public function makeNullable(): void |
|
247
|
27 |
|
{ |
|
248
|
|
|
foreach ($this->columns as $column) |
|
249
|
27 |
|
{ |
|
250
|
|
|
$column->makeNullable(); |
|
251
|
27 |
|
} |
|
252
|
|
|
} |
|
253
|
27 |
|
|
|
254
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
255
|
|
|
/** |
|
256
|
|
|
* Prepends table columns to this list of table columns. |
|
257
|
|
|
* |
|
258
|
|
|
* @param TableColumnsMetadata $columns The metadata of the table columns. |
|
259
|
|
|
*/ |
|
260
|
|
|
public function prependTableColumns(TableColumnsMetadata $columns): void |
|
261
|
8 |
|
{ |
|
262
|
|
|
$this->columns = array_merge($columns->columns, $this->columns); |
|
263
|
8 |
|
} |
|
264
|
8 |
|
|
|
265
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
266
|
|
|
/** |
|
267
|
|
|
* Removes a table column. |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $columnName The table column name. |
|
270
|
|
|
*/ |
|
271
|
|
|
public function removeColumn(string $columnName): void |
|
272
|
|
|
{ |
|
273
|
|
|
unset($this->columns[$columnName]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
277
|
|
|
/** |
|
278
|
|
|
* Removes the default values from all columns. |
|
279
|
|
|
*/ |
|
280
|
|
|
public function unsetDefaults(): void |
|
281
|
6 |
|
{ |
|
282
|
|
|
foreach ($this->columns as $column) |
|
283
|
6 |
|
{ |
|
284
|
|
|
$column->unsetDefault(); |
|
285
|
6 |
|
} |
|
286
|
|
|
} |
|
287
|
6 |
|
|
|
288
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
292
|
|
|
|