Passed
Push — master ( 4211b3...817f31 )
by Jean-Christophe
05:14
created

ClassToYuml::parseProperty()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 6
nop 4
crap 4
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 2
	public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){
27 2
		$this->class=$class;
28 2
		$this->displayProperties=$displayProperties;
29 2
		$this->displayAssociations=$displayAssociations;
30 2
		$this->displayMethods=$displayMethods;
31 2
		$this->displayMethodsParams=$displayMethodsParams;
32 2
		$this->displayPropertiesTypes=$displayPropertiesTypes;
33 2
		$this->displayAssociationClassProperties=$displayAssociationClassProperties;
34 2
	}
35
36
	public function init($hasManyToOne,$hasOneToMany){
37
		if($hasManyToOne){
38
			$this->loadManyToOne();
39
		}
40
		if($hasOneToMany){
41
			$this->loadOneToManys();
42
		}
43
	}
44
45 2
	public function parse(){
46 2
		$reflect=new \ReflectionClass($this->class);
47 2
		$yumlAnnot=OrmUtils::getAnnotationInfo($this->class, "#yuml");
48 2
		$color="";
49 2
		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 2
		$parts=[$reflect->getShortName()];
58
		
59 2
		if($this->displayProperties){
60 2
			$prikeys=OrmUtils::getKeyFields($this->class);
61 2
			$types=OrmUtils::getFieldTypes($this->class);
62 2
			$propertiesArray=[];
63 2
			$properties=$reflect->getProperties();
64 2
			foreach ($properties as $property){
65 2
				$this->parseProperty($propertiesArray, $property, $prikeys, $types);
66
			}
67 2
			$parts[]=\implode(Yuml::$memberSeparator, $propertiesArray);
68
		}
69
70 2
		if($this->displayMethods){
71 1
			$methodsArray=[];
72 1
			$methods=$reflect->getMethods();
73 1
			foreach ($methods as $method){
74 1
				$this->parseMethod($methodsArray, $method);
75
			}
76 1
			$parts[]=\implode(Yuml::$memberSeparator, $methodsArray);
77
		}
78
79 2
		$result=\implode(Yuml::$classSeparator, $parts).$color;
80 2
		$result=Yuml::setClassContent($result);
81 2
		if(isset($this->note)){
82
			$result.=$this->_getNote();
83
		}
84 2
		$this->parseResult=$result;
85 2
		return $result;
86
	}
87
	
88 2
	protected function parseProperty(&$propertiesArray,$property,$prikeys,$types){
89 2
		$propertyName=$property->getName();
90 2
		$type="";$isPri="";
91 2
		if($this->displayPropertiesTypes){
92 1
			if(\array_key_exists($propertyName, $types)){
93 1
				$type=Yuml::$parameterTypeSeparator.$types[$propertyName];
94
			}
95
		}
96 2
		if(\array_search($propertyName, $prikeys)!==false){
97 2
			$isPri=Yuml::$primary;
98
		}
99 2
		$propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]);
100 2
	}
101
	
102 1
	protected function parseMethod(&$methodsArray,$method){
103 1
		$parameters="";
104 1
		if($this->displayMethodsParams){
105 1
			$parameters=$this->getMethodParameters($method);
106
		}
107 1
		$methodName=$method->getName();
108 1
		$type="";
109 1
		if($method->hasReturnType()){
110
			$type=Yuml::$parameterTypeSeparator.$method->getReturnType();
111
		}
112 1
		$methodsArray[]=Yuml::setMethodVariables([$this->getAccess($method),$methodName,$parameters,$type]);
113 1
	}
114
115 2
	protected function getShortClassName($class){
116 2
		$reflect=new \ReflectionClass($class);
117 2
		return $reflect->getShortName();
118
	}
119
120 2
	protected function loadOneToManys(){
121 2
		$oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany");
122 2
		if($oneToManys){
123 2
			foreach ($oneToManys as $member=>$array){
124 2
				$this->oneToManys[$member]=$array["className"];
125
			}
126
		}
127 2
	}
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 && isset($joinColumn["className"])){
135
					$this->manyToOne[$member]=$joinColumn["className"];
136
				}
137
			}
138
		}
139
	}
140
141
	protected function _getYumlManyToOne(){
142
		return $this->_getYumlRelationsType($this->manyToOne,"0..*-1");
143
	}
144
145 2
	protected function _getYumlOneToMany(){
146 2
		return $this->_getYumlRelationsType($this->oneToManys,"1-0..*");
147
	}
148
149 2
	protected function _getYumlRelationsType($relations,$branche){
150 2
		$myClass=$this->getShortClassName($this->class);
151 2
		$yumlRelations=[];
152 2
		foreach ($relations as $model){
153 2
			$yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassToYuml($model,$this->displayAssociationClassProperties,false);
154
		}
155 2
		return $yumlRelations;
156
	}
157
158
	protected function _getNote(){
159
		return "-[note:".$this->note."]";
160
	}
161
162 1
	protected function getMethodParameters(\ReflectionMethod $method){
163 1
		$paramsValues=[];
164 1
		$parameters=$method->getParameters();
165 1
		foreach ($parameters as $parameter){
166 1
			$v=$parameter->getName();
167 1
			if($parameter->hasType()){
168
				$v.=Yuml::$parameterTypeSeparator.$parameter->getType();
169
			}
170 1
			$paramsValues[]=$v;
171
		}
172 1
		return \implode(Yuml::$parameterSeparator, $paramsValues);
173
	}
174
175 2
	protected function getAccess($property){
176 2
		$result=Yuml::$private;
177 2
		if($property->isPublic()){
178 1
			$result=Yuml::$public;
179 2
		}elseif($property->isProtected()){
180
			$result=Yuml::$protected;
181
		}
182 2
		return $result;
183
	}
184
185
	public function manyToOneTostring(){
186
		$this->loadManyToOne();
187
		return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne());
188
	}
189
190 2
	public function oneToManyTostring(){
191 2
		$this->loadOneToManys();
192 2
		return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany());
193
	}
194
195 2
	public function __toString(){
196 2
		$result=[$this->parse()];
197 2
		if($this->displayAssociations){
198
			$result=\array_merge($result,$this->_getYumlManyToOne());
199
			$result=\array_merge($result,$this->_getYumlOneToMany());
200
		}
201 2
		return \implode(Yuml::$groupeSeparator, $result);
202
	}
203
204
205
	public function setDisplayProperties($displayProperties) {
206
		$this->displayProperties=$displayProperties;
207
		return $this;
208
	}
209
210
	public function setDisplayMethods($displayMethods) {
211
		$this->displayMethods=$displayMethods;
212
		return $this;
213
	}
214
215
	public function setDisplayAssociations($displayAssociations) {
216
		$this->displayAssociations=$displayAssociations;
217
		return $this;
218
	}
219
}
220