|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Audit\Metadata; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class for the metadata of a database table. |
|
8
|
|
|
*/ |
|
9
|
|
|
class TableMetadata |
|
10
|
|
|
{ |
|
11
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
12
|
|
|
/** |
|
13
|
|
|
* The properties of the table that are stored by this class. |
|
14
|
|
|
* |
|
15
|
|
|
* var string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static array $fields = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The metadata of the columns of this table. |
|
21
|
|
|
* |
|
22
|
|
|
* @var TableColumnsMetadata |
|
23
|
|
|
*/ |
|
24
|
|
|
private TableColumnsMetadata $columns; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The properties of this table column. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private array $properties = []; |
|
32
|
|
|
|
|
33
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
34
|
|
|
/** |
|
35
|
|
|
* Object constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array[] $properties The metadata of the table. |
|
38
|
|
|
* @param TableColumnsMetadata $columns The metadata of the columns of this table. |
|
39
|
|
|
*/ |
|
40
|
14 |
|
public function __construct(array $properties, TableColumnsMetadata $columns) |
|
41
|
|
|
{ |
|
42
|
14 |
|
foreach (static::$fields as $field) |
|
43
|
|
|
{ |
|
44
|
14 |
|
if (isset($properties[$field])) |
|
45
|
|
|
{ |
|
46
|
14 |
|
$this->properties[$field] = $properties[$field]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
14 |
|
$this->columns = $columns; |
|
51
|
14 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
54
|
|
|
/** |
|
55
|
|
|
* Compares two metadata of two tables. Returns an array with the names of the different properties. |
|
56
|
|
|
* |
|
57
|
|
|
* @param TableMetadata $table1 The metadata of the first table. |
|
58
|
|
|
* @param TableMetadata $table2 The metadata of the second table. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string[] |
|
61
|
|
|
*/ |
|
62
|
8 |
|
public static function compareOptions(TableMetadata $table1, TableMetadata $table2): array |
|
63
|
|
|
{ |
|
64
|
8 |
|
$diff = []; |
|
65
|
|
|
|
|
66
|
8 |
|
foreach (static::$fields as $field) |
|
67
|
|
|
{ |
|
68
|
8 |
|
if (!in_array($field, ['table_schema', 'table_name'])) |
|
69
|
|
|
{ |
|
70
|
8 |
|
if ($table1->getProperty($field)!==$table2->getProperty($field)) |
|
71
|
|
|
{ |
|
72
|
4 |
|
$diff[] = $field; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
8 |
|
return $diff; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
81
|
|
|
/** |
|
82
|
|
|
* Returns table columns. |
|
83
|
|
|
* |
|
84
|
|
|
* @return TableColumnsMetadata |
|
85
|
|
|
*/ |
|
86
|
14 |
|
public function getColumns(): TableColumnsMetadata |
|
87
|
|
|
{ |
|
88
|
14 |
|
return $this->columns; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the options of this table. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array[] |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function getOptions(): array |
|
98
|
|
|
{ |
|
99
|
6 |
|
$ret = $this->properties; |
|
100
|
|
|
|
|
101
|
6 |
|
unset($ret['table_name']); |
|
102
|
6 |
|
unset($ret['table_schema']); |
|
103
|
|
|
|
|
104
|
6 |
|
return $ret; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
108
|
|
|
/** |
|
109
|
|
|
* Returns a property of this table. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name The name of the property. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string|null |
|
114
|
|
|
*/ |
|
115
|
14 |
|
public function getProperty(string $name): ?string |
|
116
|
|
|
{ |
|
117
|
14 |
|
if (isset($this->properties[$name])) |
|
118
|
|
|
{ |
|
119
|
14 |
|
return $this->properties[$name]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the name of schema. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getSchemaName(): string |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->properties['table_schema']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the name of this table. |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
13 |
|
public function getTableName(): string |
|
143
|
|
|
{ |
|
144
|
13 |
|
return $this->properties['table_name']; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
151
|
|
|
|