Passed
Push — master ( 74fbac...a461f1 )
by Jean-Christophe
04:58
created

ClassToYuml::oneToManyTostring()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 1
	public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){
27 1
		$this->class=$class;
28 1
		$this->displayProperties=$displayProperties;
29 1
		$this->displayAssociations=$displayAssociations;
30 1
		$this->displayMethods=$displayMethods;
31 1
		$this->displayMethodsParams=$displayMethodsParams;
32 1
		$this->displayPropertiesTypes=$displayPropertiesTypes;
33 1
		$this->displayAssociationClassProperties=$displayAssociationClassProperties;
34 1
	}
35
36
	public function init($hasManyToOne,$hasOneToMany){
37
		if($hasManyToOne){
38
			$this->loadManyToOne();
39
		}
40
		if($hasOneToMany){
41
			$this->loadOneToManys();
42
		}
43
	}
44
45 1
	public function parse(){
46 1
		$reflect=new \ReflectionClass($this->class);
47 1
		$yumlAnnot=OrmUtils::getAnnotationInfo($this->class, "#yuml");
48 1
		$color="";
49 1
		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 1
		$parts=[$reflect->getShortName()];
58
		
59 1
		if($this->displayProperties){
60 1
			$prikeys=OrmUtils::getKeyFields($this->class);
61 1
			$types=OrmUtils::getFieldTypes($this->class);
62 1
			$propertiesArray=[];
63 1
			$properties=$reflect->getProperties();
64 1
			foreach ($properties as $property){
65 1
				$this->parseProperty($propertiesArray, $property, $prikeys, $types);
66
			}
67 1
			$parts[]=\implode(Yuml::$memberSeparator, $propertiesArray);
68
		}
69
70 1
		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 1
		$result=\implode(Yuml::$classSeparator, $parts).$color;
80 1
		$result=Yuml::setClassContent($result);
81 1
		if(isset($this->note)){
82
			$result.=$this->_getNote();
83
		}
84 1
		$this->parseResult=$result;
85 1
		return $result;
86
	}
87
	
88 1
	protected function parseProperty(&$propertiesArray,$property,$prikeys,$types){
89 1
		$propertyName=$property->getName();
90 1
		$type="";$isPri="";
91 1
		if($this->displayPropertiesTypes){
92
			if(\array_key_exists($propertyName, $types)){
93
				$type=Yuml::$parameterTypeSeparator.$types[$propertyName];
94
			}
95
		}
96 1
		if(\array_search($propertyName, $prikeys)!==false){
97 1
			$isPri=Yuml::$primary;
98
		}
99 1
		$propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]);
100 1
	}
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 1
	protected function getShortClassName($class){
116 1
		$reflect=new \ReflectionClass($class);
117 1
		return $reflect->getShortName();
118
	}
119
120 1
	protected function loadOneToManys(){
121 1
		$oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany");
122 1
		if($oneToManys){
123 1
			foreach ($oneToManys as $member=>$array){
124 1
				$this->oneToManys[$member]=$array["className"];
125
			}
126
		}
127 1
	}
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 1
	protected function _getYumlOneToMany(){
146 1
		return $this->_getYumlRelationsType($this->oneToManys,"1-0..*");
147
	}
148
149 1
	protected function _getYumlRelationsType($relations,$branche){
150 1
		$myClass=$this->getShortClassName($this->class);
151 1
		$yumlRelations=[];
152 1
		foreach ($relations as $model){
153 1
			$yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassToYuml($model,$this->displayAssociationClassProperties,false);
154
		}
155 1
		return $yumlRelations;
156
	}
157
158
	protected function _getNote(){
159
		return "-[note:".$this->note."]";
160
	}
161
162
	protected function getMethodParameters(\ReflectionMethod $method){
163
		$paramsValues=[];
164
		$parameters=$method->getParameters();
165
		foreach ($parameters as $parameter){
166
			$v=$parameter->getName();
167
			if($parameter->hasType()){
168
				$v.=Yuml::$parameterTypeSeparator.$parameter->getType();
169
			}
170
			$paramsValues[]=$v;
171
		}
172
		return \implode(Yuml::$parameterSeparator, $paramsValues);
173
	}
174
175 1
	protected function getAccess($property){
176 1
		$result=Yuml::$private;
177 1
		if($property->isPublic()){
178
			$result=Yuml::$public;
179 1
		}elseif($property->isProtected()){
180
			$result=Yuml::$protected;
181
		}
182 1
		return $result;
183
	}
184
185
	public function manyToOneTostring(){
186
		$this->loadManyToOne();
187
		return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne());
188
	}
189
190 1
	public function oneToManyTostring(){
191 1
		$this->loadOneToManys();
192 1
		return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany());
193
	}
194
195 1
	public function __toString(){
196 1
		$result=[$this->parse()];
197 1
		if($this->displayAssociations){
198
			$result=\array_merge($result,$this->_getYumlManyToOne());
199
			$result=\array_merge($result,$this->_getYumlOneToMany());
200
		}
201 1
		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