Ubiquity  2.0.3
php rapid development framework
ClassToYuml.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\yuml;
4 
6 
11 class ClassToYuml {
12  protected $class;
13  protected $displayProperties=true;
14  protected $displayMethods=false;
15  protected $displayMethodsParams=false;
16  protected $displayPropertiesTypes=false;
19  protected $displayForeignKeys=true;
20  protected $properties;
21  protected $oneToManys=[];
22  protected $manyToOne=[];
23  protected $parseResult;
24  protected $note;
25 
27  $this->class=$class;
28  $this->displayProperties=$displayProperties;
29  $this->displayAssociations=$displayAssociations;
30  $this->displayMethods=$displayMethods;
31  $this->displayMethodsParams=$displayMethodsParams;
32  $this->displayPropertiesTypes=$displayPropertiesTypes;
33  $this->displayAssociationClassProperties=$displayAssociationClassProperties;
34  }
35 
36  public function init($hasManyToOne,$hasOneToMany){
37  if($hasManyToOne){
38  $this->loadManyToOne();
39  }
40  if($hasOneToMany){
41  $this->loadOneToManys();
42  }
43  }
44 
45  public function parse(){
46  $reflect=new \ReflectionClass($this->class);
47  $yumlAnnot=OrmUtils::getAnnotationInfo($this->class, "#yuml");
48  $color="";
49  if ($yumlAnnot!==false){
50  if(isset($yumlAnnot["color"])){
51  $color="{bg:".$yumlAnnot["color"]."}";
52  }
53  if(isset($yumlAnnot["note"])){
54  $this->note=$yumlAnnot["note"];
55  }
56  }
57  $parts=[$reflect->getShortName()];
58 
59  if($this->displayProperties){
60  $prikeys=OrmUtils::getKeyFields($this->class);
61  $types=OrmUtils::getFieldTypes($this->class);
62  $propertiesArray=[];
63  $properties=$reflect->getProperties();
64  foreach ($properties as $property){
65  $propertyName=$property->getName();
66  $type="";$isPri="";
67  if($this->displayPropertiesTypes){
68  if(\array_key_exists($propertyName, $types)){
69  $type=Yuml::$parameterTypeSeparator.$types[$propertyName];
70  }
71  }
72  if(\array_search($propertyName, $prikeys)!==false){
73  $isPri=Yuml::$primary;
74  }
75  $propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]);
76  }
77  $parts[]=\implode(Yuml::$memberSeparator, $propertiesArray);
78  }
79 
80  if($this->displayMethods){
81  $methodsArray=[];
82  $methods=$reflect->getMethods();
83  foreach ($methods as $method){
84  $parameters="";
85  if($this->displayMethodsParams){
86  $parameters=$this->getMethodParameters($method);
87  }
88  $methodName=$method->getName();
89  $type="";
90  if($method->hasReturnType()){
91  $type=Yuml::$parameterTypeSeparator.$method->getReturnType();
92  }
93  $methodsArray[]=Yuml::setMethodVariables([$this->getAccess($method),$methodName,$parameters,$type]);
94  }
95  $parts[]=\implode(Yuml::$memberSeparator, $methodsArray);
96  }
97 
98  $result=\implode(Yuml::$classSeparator, $parts).$color;
99  $result=Yuml::setClassContent($result);
100  if(isset($this->note)){
101  $result.=$this->_getNote();
102  }
103  $this->parseResult=$result;
104  return $result;
105  }
106 
107  protected function getShortClassName($class){
108  $reflect=new \ReflectionClass($class);
109  return $reflect->getShortName();
110  }
111 
112  protected function loadOneToManys(){
113  $oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany");
114  if($oneToManys){
115  foreach ($oneToManys as $member=>$array){
116  $this->oneToManys[$member]=$array["className"];
117  }
118  }
119  }
120 
121  protected function loadManyToOne(){
122  $manyToOne=OrmUtils::getAnnotationInfo($this->class, "#manyToOne");
123  if($manyToOne){
124  foreach ($manyToOne as $member){
125  $joinColumn=OrmUtils::getAnnotationInfoMember($this->class, "#joinColumn", $member);
126  if($joinColumn){
127  if(isset($joinColumn["className"])){
128  $this->manyToOne[$member]=$joinColumn["className"];
129  }
130  }
131  }
132  }
133  }
134 
135  protected function _getYumlManyToOne(){
136  return $this->_getYumlRelationsType($this->manyToOne,"0..*-1");
137  }
138 
139  protected function _getYumlOneToMany(){
140  return $this->_getYumlRelationsType($this->oneToManys,"1-0..*");
141  }
142 
143  protected function _getYumlRelationsType($relations,$branche){
144  $myClass=$this->getShortClassName($this->class);
145  $yumlRelations=[];
146  foreach ($relations as $model){
147  $yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassToYuml($model,$this->displayAssociationClassProperties,false);
148  }
149  return $yumlRelations;
150  }
151 
152  protected function _getNote(){
153  return "-[note:".$this->note."]";
154  }
155 
156  protected function getMethodParameters(\ReflectionMethod $method){
157  $paramsValues=[];
158  $parameters=$method->getParameters();
159  foreach ($parameters as $parameter){
160  $v=$parameter->getName();
161  if($parameter->hasType()){
162  $v.=Yuml::$parameterTypeSeparator.$parameter->getType();
163  }
164  $paramsValues[]=$v;
165  }
166  return \implode(Yuml::$parameterSeparator, $paramsValues);
167  }
168 
169  protected function getAccess($property){
170  $result=Yuml::$private;
171  if($property->isPublic()){
172  $result=Yuml::$public;
173  }elseif($property->isProtected()){
174  $result=Yuml::$protected;
175  }
176  return $result;
177  }
178 
179  public function manyToOneTostring(){
180  $this->loadManyToOne();
181  return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne());
182  }
183 
184  public function oneToManyTostring(){
185  $this->loadOneToManys();
186  return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany());
187  }
188 
189  public function __toString(){
190  $result=[$this->parse()];
191  if($this->displayAssociations){
192  $result=\array_merge($result,$this->_getYumlManyToOne());
193  $result=\array_merge($result,$this->_getYumlOneToMany());
194  }
195  return \implode(Yuml::$groupeSeparator, $result);
196  }
197 
198 
200  $this->displayProperties=$displayProperties;
201  return $this;
202  }
203 
205  $this->displayMethods=$displayMethods;
206  return $this;
207  }
208 
210  $this->displayAssociations=$displayAssociations;
211  return $this;
212  }
213 }
static $groupeSeparator
Definition: Yuml.php:11
static getAnnotationInfo($class, $keyAnnotation)
Definition: OrmUtils.php:195
setDisplayAssociations($displayAssociations)
static setMethodVariables($values)
Definition: Yuml.php:50
setDisplayProperties($displayProperties)
setDisplayMethods($displayMethods)
static getAnnotationInfoMember($class, $keyAnnotation, $member)
Definition: OrmUtils.php:201
static $parameterSeparator
Definition: Yuml.php:9
static $parameterTypeSeparator
Definition: Yuml.php:10
getMethodParameters(\ReflectionMethod $method)
static $memberSeparator
Definition: Yuml.php:8
static setClassContent($content)
Definition: Yuml.php:54
static getFieldTypes($className)
Definition: OrmUtils.php:86
_getYumlRelationsType($relations, $branche)
__construct($class, $displayProperties=true, $displayAssociations=true, $displayMethods=false, $displayMethodsParams=false, $displayPropertiesTypes=false, $displayAssociationClassProperties=false)
Definition: ClassToYuml.php:26
static getKeyFields($instance)
Definition: OrmUtils.php:72
static setPropertyVariables($values)
Definition: Yuml.php:46
static $classSeparator
Definition: Yuml.php:7
init($hasManyToOne, $hasOneToMany)
Definition: ClassToYuml.php:36