Passed
Push — master ( cea48f...3c52fb )
by Jean-Christophe
05:56
created

ClassToYuml::removeManyToManyExt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\utils\yuml;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * yuml export tool for class
9
 * Ubiquity\utils\yuml$ClassToYuml
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.2
14
 *
15
 */
16
class ClassToYuml {
17
	protected $class;
18
	protected $displayProperties = true;
19
	protected $displayMethods = false;
20
	protected $displayMethodsParams = false;
21
	protected $displayPropertiesTypes = false;
22
	protected $displayAssociations;
23
	protected $displayAssociationClassProperties = false;
24
	protected $displayForeignKeys = true;
25
	protected $properties;
26
	protected $oneToManys = [ ];
27
	protected $manyToOne = [ ];
28
	protected $manyToManys = [ ];
29
	protected $extManyToManys = [ ];
30
	protected $parseResult;
31
	protected $note;
32
33 2
	public function __construct($class, $displayProperties = true, $displayAssociations = true, $displayMethods = false, $displayMethodsParams = false, $displayPropertiesTypes = false, $displayAssociationClassProperties = false) {
34 2
		$this->class = $class;
35 2
		$this->displayProperties = $displayProperties;
36 2
		$this->displayAssociations = $displayAssociations;
37 2
		$this->displayMethods = $displayMethods;
38 2
		$this->displayMethodsParams = $displayMethodsParams;
39 2
		$this->displayPropertiesTypes = $displayPropertiesTypes;
40 2
		$this->displayAssociationClassProperties = $displayAssociationClassProperties;
41 2
	}
42
43
	public function init($hasManyToOne, $hasOneToMany, $hasManyToMany) {
44
		if ($hasManyToOne) {
45
			$this->loadManyToOne ();
46
		}
47
		if ($hasOneToMany) {
48
			$this->loadOneToManys ();
49
		}
50
		if ($hasManyToMany) {
51
			$this->loadManyToManys ();
52
		}
53
	}
54
55 2
	public function parse() {
56 2
		$reflect = new \ReflectionClass ( $this->class );
57 2
		$yumlAnnot = OrmUtils::getAnnotationInfo ( $this->class, "#yuml" );
58 2
		$color = "";
59 2
		if ($yumlAnnot !== false) {
60
			if (isset ( $yumlAnnot ["color"] )) {
61
				$color = "{bg:" . $yumlAnnot ["color"] . "}";
62
			}
63
			if (isset ( $yumlAnnot ["note"] )) {
64
				$this->note = $yumlAnnot ["note"];
65
			}
66
		}
67 2
		$parts = [ $reflect->getShortName () ];
68
69 2
		if ($this->displayProperties) {
70 2
			$prikeys = OrmUtils::getKeyFields ( $this->class );
71 2
			$types = OrmUtils::getFieldTypes ( $this->class );
72 2
			$propertiesArray = [ ];
73 2
			$properties = $reflect->getProperties ();
74 2
			foreach ( $properties as $property ) {
75 2
				$this->parseProperty ( $propertiesArray, $property, $prikeys, $types );
76
			}
77 2
			$parts [] = \implode ( Yuml::$memberSeparator, $propertiesArray );
78
		}
79
80 2
		if ($this->displayMethods) {
81 1
			$methodsArray = [ ];
82 1
			$methods = $reflect->getMethods ();
83 1
			foreach ( $methods as $method ) {
84 1
				$this->parseMethod ( $methodsArray, $method );
85
			}
86 1
			$parts [] = \implode ( Yuml::$memberSeparator, $methodsArray );
87
		}
88
89 2
		$result = \implode ( Yuml::$classSeparator, $parts ) . $color;
90 2
		$result = Yuml::setClassContent ( $result );
91 2
		if (isset ( $this->note )) {
92
			$result .= $this->_getNote ();
93
		}
94 2
		$this->parseResult = $result;
95 2
		return $result;
96
	}
97
98 2
	protected function parseProperty(&$propertiesArray, $property, $prikeys, $types) {
99 2
		$propertyName = $property->getName ();
100 2
		$type = "";
101 2
		$isPri = "";
102 2
		if ($this->displayPropertiesTypes) {
103 1
			if (\array_key_exists ( $propertyName, $types )) {
104 1
				$type = Yuml::$parameterTypeSeparator . $types [$propertyName];
105
			}
106
		}
107 2
		if (\array_search ( $propertyName, $prikeys ) !== false) {
108 2
			$isPri = Yuml::$primary;
109
		}
110 2
		$propertiesArray [] = Yuml::setPropertyVariables ( [ $this->getAccess ( $property ),$isPri,$propertyName,$type ] );
111 2
	}
112
113 1
	protected function parseMethod(&$methodsArray, $method) {
114 1
		$parameters = "";
115 1
		if ($this->displayMethodsParams) {
116 1
			$parameters = $this->getMethodParameters ( $method );
117
		}
118 1
		$methodName = $method->getName ();
119 1
		$type = "";
120 1
		if ($method->hasReturnType ()) {
121
			$type = Yuml::$parameterTypeSeparator . $method->getReturnType ();
122
		}
123 1
		$methodsArray [] = Yuml::setMethodVariables ( [ $this->getAccess ( $method ),$methodName,$parameters,$type ] );
124 1
	}
125
126 2
	protected function getShortClassName($class) {
127 2
		$reflect = new \ReflectionClass ( $class );
128 2
		return $reflect->getShortName ();
129
	}
130
131 2
	protected function loadOneToManys() {
132 2
		$oneToManys = OrmUtils::getAnnotationInfo ( $this->class, "#oneToMany" );
133 2
		if ($oneToManys) {
134 2
			foreach ( $oneToManys as $member => $array ) {
135 2
				$this->oneToManys [$member] = $array ["className"];
136
			}
137
		}
138 2
	}
139
140 2
	public function loadManyToManys() {
141 2
		$manyToManys = OrmUtils::getAnnotationInfo ( $this->class, "#manyToMany" );
142 2
		if ($manyToManys) {
143 2
			foreach ( $manyToManys as $member => $array ) {
144 2
				if (isset ( $array ["targetEntity"] )) {
145 2
					$this->manyToManys [$member] = $array ["targetEntity"];
146 2
					$this->extManyToManys [$array ["targetEntity"]] = $this->class;
147
				}
148
			}
149
		}
150 2
	}
151
152
	protected function loadManyToOne() {
153
		$manyToOne = OrmUtils::getAnnotationInfo ( $this->class, "#manyToOne" );
154
		if ($manyToOne) {
155
			foreach ( $manyToOne as $member ) {
156
				$joinColumn = OrmUtils::getAnnotationInfoMember ( $this->class, "#joinColumn", $member );
157
				if ($joinColumn && isset ( $joinColumn ["className"] )) {
158
					$this->manyToOne [$member] = $joinColumn ["className"];
159
				}
160
			}
161
		}
162
	}
163
164
	protected function _getYumlManyToOne() {
165
		return $this->_getYumlRelationsType ( $this->manyToOne, "0..*-1" );
166
	}
167
168 2
	protected function _getYumlOneToMany() {
169 2
		return $this->_getYumlRelationsType ( $this->oneToManys, "1-0..*" );
170
	}
171
172 2
	protected function _getYumlManyToMany() {
173 2
		return $this->_getYumlRelationsType ( $this->manyToManys, "0..*-0..*" );
174
	}
175
176 2
	protected function _getYumlRelationsType($relations, $branche) {
177 2
		$myClass = $this->getShortClassName ( $this->class );
178 2
		$yumlRelations = [ ];
179 2
		foreach ( $relations as $model ) {
180 2
			$yumlRelations [] = Yuml::setClassContent ( $myClass ) . $branche . new ClassToYuml ( $model, $this->displayAssociationClassProperties, false );
181
		}
182 2
		return $yumlRelations;
183
	}
184
185
	protected function _getNote() {
186
		return "-[note:" . $this->note . "]";
187
	}
188
189 1
	protected function getMethodParameters(\ReflectionMethod $method) {
190 1
		$paramsValues = [ ];
191 1
		$parameters = $method->getParameters ();
192 1
		foreach ( $parameters as $parameter ) {
193 1
			$v = $parameter->getName ();
194 1
			if ($parameter->hasType ()) {
195
				$v .= Yuml::$parameterTypeSeparator . $parameter->getType ();
196
			}
197 1
			$paramsValues [] = $v;
198
		}
199 1
		return \implode ( Yuml::$parameterSeparator, $paramsValues );
200
	}
201
202 2
	protected function getAccess($property) {
203 2
		$result = Yuml::$private;
204 2
		if ($property->isPublic ()) {
205 1
			$result = Yuml::$public;
206 2
		} elseif ($property->isProtected ()) {
207
			$result = Yuml::$protected;
208
		}
209 2
		return $result;
210
	}
211
212
	public function manyToOneTostring() {
213
		$this->loadManyToOne ();
214
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlManyToOne () );
215
	}
216
217 2
	public function oneToManyTostring() {
218 2
		$this->loadOneToManys ();
219 2
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlOneToMany () );
220
	}
221
222 2
	public function manyToManyTostring($load=true) {
223 2
		if($load){
224
			$this->loadManyToManys ();
225
		}
226 2
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlManyToMany () );
227
	}
228
229 2
	public function __toString() {
230 2
		$result = [ $this->parse () ];
231 2
		if ($this->displayAssociations) {
232
			$result = \array_merge ( $result, $this->_getYumlManyToOne () );
233
			$result = \array_merge ( $result, $this->_getYumlOneToMany () );
234
			$result = \array_merge ( $result, $this->_getYumlManyToMany () );
235
		}
236 2
		return \implode ( Yuml::$groupeSeparator, $result );
237
	}
238
239
	public function setDisplayProperties($displayProperties) {
240
		$this->displayProperties = $displayProperties;
241
		return $this;
242
	}
243
244
	public function setDisplayMethods($displayMethods) {
245
		$this->displayMethods = $displayMethods;
246
		return $this;
247
	}
248
249
	public function setDisplayAssociations($displayAssociations) {
250
		$this->displayAssociations = $displayAssociations;
251
		return $this;
252
	}
253
254
	/**
255
	 *
256
	 * @return array
257
	 */
258 2
	public function getExtManyToManys() {
259 2
		return $this->extManyToManys;
260
	}
261
262 2
	public function removeManyToManyExt($targetClass) {
263 2
		$member = array_search ( $targetClass, $this->manyToManys );
264 2
		if ($member !== false) {
265 2
			unset ( $this->manyToManys [$member] );
266
		}
267 2
	}
268
}
269