Completed
Push — master ( 17edba...96a1e3 )
by Jean-Christophe
01:43
created

ClassToYuml::loadOneToManys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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