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