|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\creator\yuml; |
|
4
|
|
|
|
|
5
|
|
|
class YumlParser { |
|
6
|
|
|
private $stereotypes = [ "pk" => "«pk»","null" => "«null»" ]; |
|
7
|
|
|
private $defaultType = "varchar(30)"; |
|
8
|
|
|
private $originalString; |
|
9
|
|
|
private $parts; |
|
10
|
|
|
private $tables = [ ]; |
|
11
|
|
|
|
|
12
|
1 |
|
public function __construct($yumlString) { |
|
13
|
1 |
|
$this->originalString = $yumlString; |
|
14
|
1 |
|
$this->parse (); |
|
15
|
1 |
|
} |
|
16
|
|
|
|
|
17
|
1 |
|
private function getFkName($table, $prefix = "id") { |
|
18
|
1 |
|
return $prefix . \ucfirst ( $table ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
1 |
|
private function parse() { |
|
22
|
1 |
|
$str = $this->originalString; |
|
23
|
1 |
|
$this->parts = \preg_split ( '@\ *?,\ *?@', $str ); |
|
24
|
1 |
|
foreach ( $this->parts as $part ) { |
|
25
|
1 |
|
$this->parsePart ( $part ); |
|
26
|
|
|
} |
|
27
|
1 |
|
$this->parseAllProperties (); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
private function parsePart($part) { |
|
31
|
1 |
|
$matches = [ ]; |
|
32
|
1 |
|
\preg_match_all ( '@\[\w+[\|\]]@', $part, $matches ); |
|
33
|
1 |
|
if (\sizeof ( $matches [0] ) > 0) { |
|
34
|
1 |
|
foreach ( $matches [0] as $match ) { |
|
35
|
1 |
|
$table = \substr ( $match, 1, \strlen ( $match ) - 2 ); |
|
36
|
1 |
|
$this->tables [$table] = [ ]; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
private function parseAllProperties() { |
|
42
|
1 |
|
$tables = \array_keys ( $this->tables ); |
|
43
|
1 |
|
foreach ( $tables as $table ) { |
|
44
|
1 |
|
$matchProperties = [ ]; |
|
45
|
1 |
|
\preg_match ( '@\[' . $table . '\|(.*?)\]@', $this->originalString, $matchProperties ); |
|
46
|
1 |
|
if (isset ( $matchProperties [1] )) { |
|
47
|
1 |
|
$properties = $matchProperties [1]; |
|
48
|
1 |
|
$this->parseProperties ( $properties, $table ); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
1 |
|
foreach ( $tables as $table ) { |
|
52
|
1 |
|
$this->parseRelations ( $table ); |
|
53
|
|
|
} |
|
54
|
1 |
|
foreach ( $tables as $table ) { |
|
55
|
1 |
|
$this->parseManyRelations ( $table ); |
|
56
|
|
|
} |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
private function parseProperties($propertiesString, $table) { |
|
60
|
1 |
|
$properties = \explode ( ";", $propertiesString ); |
|
61
|
1 |
|
foreach ( $properties as $property ) { |
|
62
|
1 |
|
$result = $this->parseProperty ( $property ); |
|
63
|
1 |
|
if (! isset ( $this->tables [$table] ["properties"] )) |
|
64
|
1 |
|
$this->tables [$table] ["properties"] = [ ]; |
|
65
|
1 |
|
$this->tables [$table] ["properties"] [] = $result; |
|
66
|
|
|
} |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
private function parseProperty($property) { |
|
70
|
1 |
|
$matches = [ ]; |
|
71
|
1 |
|
$result = [ ]; |
|
72
|
1 |
|
\preg_match_all ( '@«(.+?)»@', $property, $matches ); |
|
73
|
1 |
|
if (is_array ( $matches )) { |
|
74
|
1 |
|
foreach ( $matches as $match ) { |
|
75
|
1 |
|
if (isset ( $match [0] )) { |
|
76
|
1 |
|
$property = \str_replace ( $match [0], "", $property ); |
|
77
|
1 |
|
switch ($match [0]) { |
|
78
|
1 |
|
case $this->stereotypes ["pk"] : |
|
79
|
1 |
|
$result ["pk"] = true; |
|
80
|
1 |
|
break; |
|
81
|
1 |
|
case $this->stereotypes ["null"] : |
|
82
|
1 |
|
$result ["null"] = true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
1 |
|
$parts = \explode ( ":", $property ); |
|
88
|
1 |
|
\preg_match ( '@\ *?(\w+)@', $parts [0], $match ); |
|
89
|
1 |
|
if (isset ( $match [1] )) |
|
90
|
1 |
|
$result ["name"] = $match [1]; |
|
91
|
1 |
|
if (isset ( $parts [1] )) { |
|
92
|
1 |
|
$result ["type"] = $parts [1]; |
|
93
|
|
|
} else { |
|
94
|
|
|
$result ["type"] = $this->defaultType; |
|
95
|
|
|
} |
|
96
|
1 |
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function getFirstKey($table) { |
|
100
|
1 |
|
$result = null; |
|
101
|
1 |
|
foreach ( $this->tables [$table] ["properties"] as $property ) { |
|
102
|
1 |
|
if (! isset ( $result )) |
|
103
|
1 |
|
$result = $property ["name"]; |
|
104
|
1 |
|
if (isset ( $property ["pk"] ) && $property ["pk"]) |
|
105
|
1 |
|
return $property ["name"]; |
|
106
|
|
|
} |
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function getFieldType($table, $fieldName) { |
|
111
|
1 |
|
foreach ( $this->tables [$table] ["properties"] as $property ) { |
|
112
|
1 |
|
if ($property ["name"] === $fieldName) |
|
113
|
1 |
|
return $property ["type"]; |
|
114
|
|
|
} |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
public function getPrimaryKeys($table) { |
|
119
|
1 |
|
$result = [ ]; |
|
120
|
1 |
|
foreach ( $this->tables [$table] ["properties"] as $property ) { |
|
121
|
1 |
|
if (isset ( $property ["pk"] ) && $property ["pk"]) |
|
122
|
1 |
|
$result [] = $property ["name"]; |
|
123
|
|
|
} |
|
124
|
1 |
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
private function parseRelations($table) { |
|
128
|
1 |
|
$matches = [ ]; |
|
129
|
1 |
|
\preg_match_all ( '@\[' . $table . '\][^,]*?1-.*?\[(\w+)\]@', $this->originalString, $matches ); |
|
130
|
1 |
|
$this->_parseRelations ( $table, $matches ); |
|
131
|
1 |
|
\preg_match_all ( '@\[(\w+)\].*?-[^,]*?1\[' . $table . '\]@', $this->originalString, $matches ); |
|
132
|
1 |
|
$this->_parseRelations ( $table, $matches ); |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
private function _parseRelations($table, $matches) { |
|
136
|
1 |
|
if (\sizeof ( $matches ) > 1) { |
|
137
|
1 |
|
$pk = $this->getFirstKey ( $table ); |
|
138
|
1 |
|
if (isset ( $pk )) { |
|
139
|
1 |
|
foreach ( $matches [1] as $match ) { |
|
140
|
1 |
|
$tableName = $match; |
|
141
|
1 |
|
$fk = $this->getFkName ( $table, $pk ); |
|
142
|
1 |
|
$this->tables [$table] ["relations"] [] = [ "TABLE_NAME" => $tableName,"COLUMN_NAME" => $fk ]; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
private function parseManyRelations($table) { |
|
149
|
1 |
|
$matches = [ ]; |
|
150
|
1 |
|
\preg_match_all ( '@\[' . $table . '\][^,]*?\*-.*?\*\[(\w+)\]@', $this->originalString, $matches ); |
|
151
|
1 |
|
$this->_parseManyRelations ( $table, $matches ); |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
private function _parseManyRelations($table, $matches) { |
|
155
|
1 |
|
$myPk = $this->getFirstKey ( $table ); |
|
156
|
1 |
|
$myFk = $this->getFkName ( $table, $myPk ); |
|
157
|
1 |
|
$myFkType = $this->getFieldType ( $table, $myPk ); |
|
158
|
1 |
|
if (\sizeof ( $matches ) > 1) { |
|
159
|
1 |
|
foreach ( $matches [1] as $match ) { |
|
160
|
|
|
$tableName = $match; |
|
161
|
|
|
$pk = $this->getFirstKey ( $tableName ); |
|
162
|
|
|
if (isset ( $pk )) { |
|
163
|
|
|
$fk = $this->getFkName ( $tableName, $pk ); |
|
164
|
|
|
$fkType = $this->getFieldType ( $tableName, $pk ); |
|
165
|
|
|
$newTable = $table . "_" . $tableName; |
|
166
|
|
|
$this->tables [$newTable] = [ ]; |
|
167
|
|
|
$this->tables [$newTable] ["properties"] [] = [ "name" => $myFk,"type" => $myFkType,"pk" => true ]; |
|
168
|
|
|
$this->tables [$newTable] ["properties"] [] = [ "name" => $fk,"type" => $fkType,"pk" => true ]; |
|
169
|
|
|
$this->tables [$tableName] ["relations"] [] = [ "TABLE_NAME" => $newTable,"COLUMN_NAME" => $fk ]; |
|
170
|
|
|
$this->tables [$table] ["relations"] [] = [ "TABLE_NAME" => $newTable,"COLUMN_NAME" => $myFk ]; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
1 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getParts() { |
|
177
|
|
|
return $this->parts; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function getTables() { |
|
181
|
|
|
return $this->tables; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
public function getTableNames() { |
|
185
|
1 |
|
return \array_keys ( $this->tables ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
public function getFields($table) { |
|
189
|
1 |
|
return $this->tables [$table] ["properties"]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
public function getForeignKeys($table) { |
|
193
|
1 |
|
if (isset ( $this->tables [$table] ["relations"] )) |
|
194
|
1 |
|
return $this->tables [$table] ["relations"]; |
|
195
|
1 |
|
return [ ]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|