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