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