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