Passed
Push — master ( ee1769...a6250f )
by Jean-Christophe
01:23
created

ClassToYuml::parseMethod()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\utils\yuml;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * @author jc
9
 * yuml export tool for class
10
 */
11
class ClassToYuml {
12
	protected $class;
13
	protected $displayProperties=true;
14
	protected $displayMethods=false;
15
	protected $displayMethodsParams=false;
16
	protected $displayPropertiesTypes=false;
17
	protected $displayAssociations;
18
	protected $displayAssociationClassProperties=false;
19
	protected $displayForeignKeys=true;
20
	protected $properties;
21
	protected $oneToManys=[];
22
	protected $manyToOne=[];
23
	protected $parseResult;
24
	protected $note;
25
26
	public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){
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
				$this->parseProperty($propertiesArray, $property, $prikeys, $types);
66
			}
67
			$parts[]=\implode(Yuml::$memberSeparator, $propertiesArray);
68
		}
69
70
		if($this->displayMethods){
71
			$methodsArray=[];
72
			$methods=$reflect->getMethods();
73
			foreach ($methods as $method){
74
				$this->parseMethod($methodsArray, $method);
75
			}
76
			$parts[]=\implode(Yuml::$memberSeparator, $methodsArray);
77
		}
78
79
		$result=\implode(Yuml::$classSeparator, $parts).$color;
80
		$result=Yuml::setClassContent($result);
81
		if(isset($this->note)){
82
			$result.=$this->_getNote();
83
		}
84
		$this->parseResult=$result;
85
		return $result;
86
	}
87
	
88
	protected function parseProperty(&$propertiesArray,$property,$prikeys,$types){
89
		$propertyName=$property->getName();
90
		$type="";$isPri="";
91
		if($this->displayPropertiesTypes){
92
			if(\array_key_exists($propertyName, $types)){
93
				$type=Yuml::$parameterTypeSeparator.$types[$propertyName];
94
			}
95
		}
96
		if(\array_search($propertyName, $prikeys)!==false){
97
			$isPri=Yuml::$primary;
98
		}
99
		$propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]);
100
	}
101
	
102
	protected function parseMethod(&$methodsArray,$method){
103
		$parameters="";
104
		if($this->displayMethodsParams){
105
			$parameters=$this->getMethodParameters($method);
106
		}
107
		$methodName=$method->getName();
108
		$type="";
109
		if($method->hasReturnType()){
110
			$type=Yuml::$parameterTypeSeparator.$method->getReturnType();
111
		}
112
		$methodsArray[]=Yuml::setMethodVariables([$this->getAccess($method),$methodName,$parameters,$type]);
113
	}
114
115
	protected function getShortClassName($class){
116
		$reflect=new \ReflectionClass($class);
117
		return $reflect->getShortName();
118
	}
119
120
	protected function loadOneToManys(){
121
		$oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany");
122
		if($oneToManys){
123
			foreach ($oneToManys as $member=>$array){
124
				$this->oneToManys[$member]=$array["className"];
125
			}
126
		}
127
	}
128
129
	protected function loadManyToOne(){
130
		$manyToOne=OrmUtils::getAnnotationInfo($this->class, "#manyToOne");
131
		if($manyToOne){
132
			foreach ($manyToOne as $member){
133
				$joinColumn=OrmUtils::getAnnotationInfoMember($this->class, "#joinColumn", $member);
134
				if($joinColumn){
135
					if(isset($joinColumn["className"])){
136
						$this->manyToOne[$member]=$joinColumn["className"];
137
					}
138
				}
139
			}
140
		}
141
	}
142
143
	protected function _getYumlManyToOne(){
144
		return $this->_getYumlRelationsType($this->manyToOne,"0..*-1");
145
	}
146
147
	protected function _getYumlOneToMany(){
148
		return $this->_getYumlRelationsType($this->oneToManys,"1-0..*");
149
	}
150
151
	protected function _getYumlRelationsType($relations,$branche){
152
		$myClass=$this->getShortClassName($this->class);
153
		$yumlRelations=[];
154
		foreach ($relations as $model){
155
			$yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassToYuml($model,$this->displayAssociationClassProperties,false);
156
		}
157
		return $yumlRelations;
158
	}
159
160
	protected function _getNote(){
161
		return "-[note:".$this->note."]";
162
	}
163
164
	protected function getMethodParameters(\ReflectionMethod $method){
165
		$paramsValues=[];
166
		$parameters=$method->getParameters();
167
		foreach ($parameters as $parameter){
168
			$v=$parameter->getName();
169
			if($parameter->hasType()){
170
				$v.=Yuml::$parameterTypeSeparator.$parameter->getType();
171
			}
172
			$paramsValues[]=$v;
173
		}
174
		return \implode(Yuml::$parameterSeparator, $paramsValues);
175
	}
176
177
	protected function getAccess($property){
178
		$result=Yuml::$private;
179
		if($property->isPublic()){
180
			$result=Yuml::$public;
181
		}elseif($property->isProtected()){
182
			$result=Yuml::$protected;
183
		}
184
		return $result;
185
	}
186
187
	public function manyToOneTostring(){
188
		$this->loadManyToOne();
189
		return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne());
190
	}
191
192
	public function oneToManyTostring(){
193
		$this->loadOneToManys();
194
		return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany());
195
	}
196
197
	public function __toString(){
198
		$result=[$this->parse()];
199
		if($this->displayAssociations){
200
			$result=\array_merge($result,$this->_getYumlManyToOne());
201
			$result=\array_merge($result,$this->_getYumlOneToMany());
202
		}
203
		return \implode(Yuml::$groupeSeparator, $result);
204
	}
205
206
207
	public function setDisplayProperties($displayProperties) {
208
		$this->displayProperties=$displayProperties;
209
		return $this;
210
	}
211
212
	public function setDisplayMethods($displayMethods) {
213
		$this->displayMethods=$displayMethods;
214
		return $this;
215
	}
216
217
	public function setDisplayAssociations($displayAssociations) {
218
		$this->displayAssociations=$displayAssociations;
219
		return $this;
220
	}
221
}
222