Completed
Push — master ( 4e6026...e0b9fa )
by Jean-Christophe
01:30
created

ClassToYuml::_getNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\utils\yuml;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\parser\Reflexion;
7
use Ubiquity\utils\StrUtils;
8
9
/**
10
 * @author jc
11
 * yuml export tool for class
12
 */
13
class ClassToYuml {
14
	protected $class;
15
	protected $displayProperties=true;
16
	protected $displayMethods=false;
17
	protected $displayMethodsParams=false;
18
	protected $displayPropertiesTypes=false;
19
	protected $displayAssociations;
20
	protected $displayAssociationClassProperties=false;
21
	protected $displayForeignKeys=true;
22
	protected $properties;
23
	protected $oneToManys=[];
24
	protected $manyToOne=[];
25
	protected $parseResult;
26
	protected $note;
27
28
	public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){
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
201
	public function setDisplayProperties($displayProperties) {
202
		$this->displayProperties=$displayProperties;
203
		return $this;
204
	}
205
206
	public function setDisplayMethods($displayMethods) {
207
		$this->displayMethods=$displayMethods;
208
		return $this;
209
	}
210
211
	public function setDisplayAssociations($displayAssociations) {
212
		$this->displayAssociations=$displayAssociations;
213
		return $this;
214
	}
215
}
216