1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\Xsd\XsdToPhp\Php\Structure; |
3
|
|
|
|
4
|
|
|
class PHPClass |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
protected $name; |
8
|
|
|
|
9
|
|
|
protected $namespace; |
10
|
|
|
|
11
|
|
|
protected $doc; |
12
|
|
|
|
13
|
16 |
|
public static function createFromFQCN($className) |
14
|
|
|
{ |
15
|
16 |
|
if (($pos = strrpos($className, '\\')) !== false) { |
16
|
|
|
return new self(substr($className, $pos + 1), substr($className, 0, $pos)); |
17
|
|
|
} else { |
18
|
16 |
|
return new self($className); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param bool $onlyParent |
24
|
|
|
* @return PHPProperty |
25
|
|
|
*/ |
26
|
8 |
|
public function isSimpleType($onlyParent = false) |
27
|
|
|
{ |
28
|
8 |
|
if ($onlyParent) { |
29
|
|
|
$e = $this->getExtends(); |
30
|
|
|
if ($e) { |
31
|
|
|
if ($e->hasProperty('__value')) { |
32
|
|
|
return $e->getProperty('__value'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} else { |
36
|
8 |
|
if ($this->hasPropertyInHierarchy('__value') && count($this->getPropertiesInHierarchy()) === 1) { |
37
|
2 |
|
return $this->getPropertyInHierarchy("__value"); |
38
|
|
|
} |
39
|
|
|
} |
40
|
7 |
|
} |
41
|
|
|
|
42
|
8 |
|
public function getPhpType() |
43
|
|
|
{ |
44
|
8 |
|
if (!$this->getNamespace()) { |
45
|
8 |
|
if ($this->isNativeType()) { |
46
|
8 |
|
return $this->getName(); |
47
|
|
|
} |
48
|
|
|
return "\\" . $this->getName(); |
49
|
|
|
} |
50
|
1 |
|
return "\\" . $this->getFullName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
View Code Duplication |
public function isNativeType() |
|
|
|
|
54
|
|
|
{ |
55
|
8 |
|
return !$this->getNamespace() && in_array($this->getName(), [ |
56
|
8 |
|
'string', |
57
|
8 |
|
'int', |
58
|
8 |
|
'float', |
59
|
8 |
|
'integer', |
60
|
8 |
|
'boolean', |
61
|
8 |
|
'array', |
62
|
8 |
|
'mixed', |
63
|
|
|
'callable' |
64
|
8 |
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
30 |
|
public function __construct($name = null, $namespace = null) |
69
|
|
|
{ |
70
|
30 |
|
$this->name = $name; |
71
|
30 |
|
$this->namespace = $namespace; |
72
|
30 |
|
} |
73
|
|
|
|
74
|
24 |
|
public function getName() |
75
|
|
|
{ |
76
|
24 |
|
return $this->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
30 |
|
public function setName($name) |
80
|
|
|
{ |
81
|
30 |
|
$this->name = $name; |
82
|
30 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
24 |
|
public function getNamespace() |
86
|
|
|
{ |
87
|
24 |
|
return $this->namespace; |
88
|
|
|
} |
89
|
|
|
|
90
|
30 |
|
public function setNamespace($namespace) |
91
|
|
|
{ |
92
|
30 |
|
$this->namespace = $namespace; |
93
|
30 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
public function getDoc() |
97
|
|
|
{ |
98
|
8 |
|
return $this->doc; |
99
|
|
|
} |
100
|
|
|
|
101
|
30 |
|
public function setDoc($doc) |
102
|
|
|
{ |
103
|
30 |
|
$this->doc = $doc; |
104
|
30 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function __toString() |
108
|
|
|
{ |
109
|
|
|
return $this->getFullName(); |
110
|
|
|
} |
111
|
|
|
|
112
|
30 |
|
public function getFullName() |
113
|
|
|
{ |
114
|
30 |
|
return "{$this->namespace}\\{$this->name}"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected $checks = array(); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @var PHPConstant[] |
122
|
|
|
*/ |
123
|
|
|
protected $constants = array(); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @var PHPProperty[] |
128
|
|
|
*/ |
129
|
|
|
protected $properties = array(); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* |
133
|
|
|
* @param |
134
|
|
|
* $property |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getChecks($property) |
138
|
|
|
{ |
139
|
|
|
return isset($this->checks[$property]) ? $this->checks[$property] : array(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @param |
145
|
|
|
* $property |
146
|
|
|
* @param |
147
|
|
|
* $check |
148
|
|
|
* @param |
149
|
|
|
* $value |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
1 |
|
public function addCheck($property, $check, $value) |
153
|
|
|
{ |
154
|
1 |
|
$this->checks[$property][$check][] = $value; |
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
* @return PHPProperty[] |
161
|
|
|
*/ |
162
|
19 |
|
public function getProperties() |
163
|
|
|
{ |
164
|
19 |
|
return $this->properties; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* @param string $name |
170
|
|
|
* @return boolean |
171
|
|
|
*/ |
172
|
8 |
|
public function hasProperty($name) |
173
|
|
|
{ |
174
|
8 |
|
return isset($this->properties[$name]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* |
179
|
|
|
* @param string $name |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
8 |
|
public function hasPropertyInHierarchy($name) |
183
|
|
|
{ |
184
|
8 |
|
if ($this->hasProperty($name)) { |
185
|
2 |
|
return true; |
186
|
|
|
} |
187
|
7 |
|
if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) { |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
7 |
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* |
195
|
|
|
* @param string $name |
196
|
|
|
* @return PHPProperty |
197
|
|
|
*/ |
198
|
2 |
|
public function getPropertyInHierarchy($name) |
199
|
|
|
{ |
200
|
2 |
|
if ($this->hasProperty($name)) { |
201
|
2 |
|
return $this->getProperty($name); |
202
|
|
|
} |
203
|
|
|
if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) { |
204
|
|
|
return $this->getExtends()->getPropertyInHierarchy($name); |
205
|
|
|
} |
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* |
211
|
|
|
* @param string $name |
|
|
|
|
212
|
|
|
* @return PHPProperty |
213
|
|
|
*/ |
214
|
2 |
|
public function getPropertiesInHierarchy() |
215
|
|
|
{ |
216
|
2 |
|
$ps = $this->getProperties(); |
217
|
|
|
|
218
|
2 |
|
if (($this instanceof PHPClass) && $this->getExtends()) { |
219
|
|
|
$ps = array_merge($ps, $this->getExtends()->getPropertiesInHierarchy()); |
220
|
|
|
} |
221
|
|
|
|
222
|
2 |
|
return $ps; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* |
227
|
|
|
* @param string $name |
228
|
|
|
* @return PHPProperty |
229
|
|
|
*/ |
230
|
8 |
|
public function getProperty($name) |
231
|
|
|
{ |
232
|
8 |
|
return $this->properties[$name]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* |
237
|
|
|
* @param PHPProperty $property |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
30 |
|
public function addProperty(PHPProperty $property) |
241
|
|
|
{ |
242
|
30 |
|
$this->properties[$property->getName()] = $property; |
243
|
30 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* |
248
|
|
|
* @var boolean |
249
|
|
|
*/ |
250
|
|
|
protected $abstract; |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* |
254
|
|
|
* @var PHPClass |
255
|
|
|
*/ |
256
|
|
|
protected $extends; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* |
260
|
|
|
* @return PHPClass |
261
|
|
|
*/ |
262
|
19 |
|
public function getExtends() |
263
|
|
|
{ |
264
|
19 |
|
return $this->extends; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* |
269
|
|
|
* @param PHPClass $extends |
270
|
|
|
* @return PHPClass |
271
|
|
|
*/ |
272
|
17 |
|
public function setExtends(PHPClass $extends) |
273
|
|
|
{ |
274
|
17 |
|
$this->extends = $extends; |
275
|
17 |
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function getAbstract() |
279
|
|
|
{ |
280
|
|
|
return $this->abstract; |
281
|
|
|
} |
282
|
|
|
|
283
|
25 |
|
public function setAbstract($abstract) |
284
|
|
|
{ |
285
|
25 |
|
$this->abstract = (boolean)$abstract; |
286
|
25 |
|
return $this; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.