Completed
Push — master ( 3b553a...0a5d2e )
by Jean-Christophe
01:30
created

ClassParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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