1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\Xsd\XsdToPhp\Php; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Inflector\Inflector; |
5
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClass; |
6
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClassOf; |
7
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPProperty; |
8
|
|
|
use Zend\Code\Generator; |
9
|
|
|
use Zend\Code\Generator\DocBlock\Tag\ParamTag; |
10
|
|
|
use Zend\Code\Generator\DocBlock\Tag\PropertyTag; |
11
|
|
|
use Zend\Code\Generator\DocBlock\Tag\ReturnTag; |
12
|
|
|
use Zend\Code\Generator\DocBlockGenerator; |
13
|
|
|
use Zend\Code\Generator\MethodGenerator; |
14
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
15
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
16
|
|
|
|
17
|
|
|
class ClassGenerator |
18
|
|
|
{ |
19
|
|
|
|
20
|
8 |
|
private function handleBody(Generator\ClassGenerator $class, PHPClass $type) |
21
|
|
|
{ |
22
|
8 |
|
foreach ($type->getProperties() as $prop) { |
23
|
7 |
|
if ($prop->getName() !== '__value') { |
24
|
7 |
|
$this->handleProperty($class, $prop); |
25
|
7 |
|
} |
26
|
8 |
|
} |
27
|
8 |
|
foreach ($type->getProperties() as $prop) { |
28
|
7 |
|
if ($prop->getName() !== '__value') { |
29
|
7 |
|
$this->handleMethod($class, $prop, $type); |
30
|
7 |
|
} |
31
|
8 |
|
} |
32
|
|
|
|
33
|
8 |
|
if (count($type->getProperties()) === 1 && $type->hasProperty('__value')) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
private function isNativeType(PHPClass $class) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
return !$class->getNamespace() && in_array($class->getName(), [ |
43
|
|
|
'string', |
44
|
|
|
'int', |
45
|
|
|
'float', |
46
|
|
|
'integer', |
47
|
|
|
'boolean', |
48
|
|
|
'array', |
49
|
|
|
'mixed', |
50
|
|
|
'callable' |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true) |
|
|
|
|
55
|
|
|
{ |
56
|
2 |
|
$type = $prop->getType(); |
57
|
|
|
|
58
|
2 |
|
$docblock = new DocBlockGenerator('Construct'); |
59
|
2 |
|
$paramTag = new ParamTag("value", "mixed"); |
|
|
|
|
60
|
2 |
|
$paramTag->setTypes(($type ? $type->getPhpType() : "mixed")); |
61
|
|
|
|
62
|
2 |
|
$docblock->setTag($paramTag); |
63
|
|
|
|
64
|
2 |
|
$param = new ParameterGenerator("value"); |
65
|
2 |
|
if ($type && !$type->isNativeType()) { |
66
|
|
|
$param->setType($type->getPhpType()); |
67
|
|
|
} |
68
|
2 |
|
$method = new MethodGenerator("__construct", [ |
69
|
|
|
$param |
70
|
2 |
|
]); |
71
|
2 |
|
$method->setDocBlock($docblock); |
72
|
2 |
|
$method->setBody("\$this->value(\$value);"); |
73
|
|
|
|
74
|
2 |
|
$generator->addMethodFromGenerator($method); |
75
|
|
|
|
76
|
2 |
|
$docblock = new DocBlockGenerator('Gets or sets the inner value'); |
77
|
2 |
|
$paramTag = new ParamTag("value", "mixed"); |
|
|
|
|
78
|
2 |
View Code Duplication |
if ($type && $type instanceof PHPClassOf) { |
|
|
|
|
79
|
|
|
$paramTag->setTypes($type->getArg()->getType()->getPhpType() . "[]"); |
80
|
2 |
|
} elseif ($type) { |
81
|
2 |
|
$paramTag->setTypes($prop->getType()->getPhpType()); |
82
|
2 |
|
} |
83
|
2 |
|
$docblock->setTag($paramTag); |
84
|
|
|
|
85
|
2 |
|
$returnTag = new ReturnTag("mixed"); |
|
|
|
|
86
|
|
|
|
87
|
2 |
View Code Duplication |
if ($type && $type instanceof PHPClassOf) { |
|
|
|
|
88
|
|
|
$returnTag->setTypes($type->getArg()->getType()->getPhpType() . "[]"); |
89
|
2 |
|
} elseif ($type) { |
90
|
2 |
|
$returnTag->setTypes($type->getPhpType()); |
91
|
2 |
|
} |
92
|
2 |
|
$docblock->setTag($returnTag); |
93
|
|
|
|
94
|
2 |
|
$param = new ParameterGenerator("value"); |
95
|
2 |
|
$param->setDefaultValue(null); |
96
|
|
|
|
97
|
2 |
|
if ($type && !$type->isNativeType()) { |
98
|
|
|
$param->setType($type->getPhpType()); |
99
|
|
|
} |
100
|
2 |
|
$method = new MethodGenerator("value", []); |
101
|
2 |
|
$method->setDocBlock($docblock); |
102
|
|
|
|
103
|
2 |
|
$methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; |
104
|
2 |
|
$methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; |
105
|
2 |
|
$methodBody .= "}" . PHP_EOL; |
106
|
2 |
|
$methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; |
107
|
2 |
|
$method->setBody($methodBody); |
108
|
|
|
|
109
|
2 |
|
$generator->addMethodFromGenerator($method); |
110
|
|
|
|
111
|
2 |
|
$docblock = new DocBlockGenerator('Gets a string value'); |
112
|
2 |
|
$docblock->setTag(new ReturnTag("string")); |
|
|
|
|
113
|
2 |
|
$method = new MethodGenerator("__toString"); |
114
|
2 |
|
$method->setDocBlock($docblock); |
115
|
2 |
|
$method->setBody("return strval(\$this->" . $prop->getName() . ");"); |
116
|
2 |
|
$generator->addMethodFromGenerator($method); |
117
|
2 |
|
} |
118
|
|
|
|
119
|
7 |
|
private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
|
|
|
120
|
|
|
{ |
121
|
7 |
|
$methodBody = ''; |
122
|
7 |
|
$docblock = new DocBlockGenerator(); |
123
|
|
|
|
124
|
7 |
|
$docblock->setShortDescription("Sets a new " . $prop->getName()); |
125
|
|
|
|
126
|
7 |
|
if ($prop->getDoc()) { |
127
|
|
|
$docblock->setLongDescription($prop->getDoc()); |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
$patramTag = new ParamTag($prop->getName()); |
131
|
7 |
|
$docblock->setTag($patramTag); |
132
|
|
|
|
133
|
7 |
|
$return = new ReturnTag("self"); |
|
|
|
|
134
|
7 |
|
$docblock->setTag($return); |
135
|
|
|
|
136
|
7 |
|
$type = $prop->getType(); |
137
|
|
|
|
138
|
7 |
|
$method = new MethodGenerator("set" . Inflector::classify($prop->getName())); |
139
|
|
|
|
140
|
7 |
|
$parameter = new ParameterGenerator($prop->getName(), "mixed"); |
141
|
|
|
|
142
|
7 |
|
if ($type && $type instanceof PHPClassOf) { |
143
|
4 |
|
$patramTag->setTypes($type->getArg() |
144
|
4 |
|
->getType()->getPhpType() . "[]"); |
145
|
4 |
|
$parameter->setType("array"); |
146
|
|
|
|
147
|
4 |
|
if ($p = $type->getArg()->getType()->isSimpleType() |
148
|
4 |
|
) { |
149
|
|
|
if (($t = $p->getType())) { |
150
|
|
|
$patramTag->setTypes($t->getPhpType()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
7 |
|
} elseif ($type) { |
154
|
3 |
|
if ($type->isNativeType()) { |
155
|
3 |
|
$patramTag->setTypes($type->getPhpType()); |
156
|
3 |
|
} elseif ($p = $type->isSimpleType()) { |
157
|
|
|
if (($t = $p->getType()) && !$t->isNativeType()) { |
158
|
|
|
$patramTag->setTypes($t->getPhpType()); |
159
|
|
|
$parameter->setType($t->getPhpType()); |
160
|
|
|
} elseif ($t && !$t->isNativeType()) { |
161
|
|
|
$patramTag->setTypes($t->getPhpType()); |
162
|
|
|
$parameter->setType($t->getPhpType()); |
163
|
|
|
} elseif ($t) { |
164
|
|
|
$patramTag->setTypes($t->getPhpType()); |
165
|
|
|
} |
166
|
|
|
} else { |
167
|
|
|
$patramTag->setTypes($type->getPhpType()); |
168
|
|
|
$parameter->setType($type->getPhpType()); |
169
|
|
|
} |
170
|
3 |
|
} |
171
|
|
|
|
172
|
7 |
|
$methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; |
173
|
7 |
|
$methodBody .= "return \$this;"; |
174
|
7 |
|
$method->setBody($methodBody); |
175
|
7 |
|
$method->setDocBlock($docblock); |
176
|
7 |
|
$method->setParameter($parameter); |
177
|
|
|
|
178
|
7 |
|
$generator->addMethodFromGenerator($method); |
179
|
7 |
|
} |
180
|
|
|
|
181
|
7 |
|
private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
|
184
|
7 |
|
if ($prop->getType() instanceof PHPClassOf) { |
185
|
4 |
|
$docblock = new DocBlockGenerator(); |
186
|
4 |
|
$docblock->setShortDescription("isset " . $prop->getName()); |
187
|
4 |
|
if ($prop->getDoc()) { |
188
|
|
|
$docblock->setLongDescription($prop->getDoc()); |
189
|
|
|
} |
190
|
|
|
|
191
|
4 |
|
$patramTag = new ParamTag("index", "scalar"); |
|
|
|
|
192
|
4 |
|
$docblock->setTag($patramTag); |
193
|
|
|
|
194
|
4 |
|
$docblock->setTag(new ReturnTag("boolean")); |
|
|
|
|
195
|
|
|
|
196
|
4 |
|
$paramIndex = new ParameterGenerator("index", "mixed"); |
197
|
|
|
|
198
|
4 |
|
$method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]); |
199
|
4 |
|
$method->setDocBlock($docblock); |
200
|
4 |
|
$method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);"); |
201
|
4 |
|
$generator->addMethodFromGenerator($method); |
202
|
|
|
|
203
|
4 |
|
$docblock = new DocBlockGenerator(); |
204
|
4 |
|
$docblock->setShortDescription("unset " . $prop->getName()); |
205
|
4 |
|
if ($prop->getDoc()) { |
206
|
|
|
$docblock->setLongDescription($prop->getDoc()); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
$patramTag = new ParamTag("index", "scalar"); |
|
|
|
|
210
|
4 |
|
$docblock->setTag($patramTag); |
211
|
4 |
|
$paramIndex = new ParameterGenerator("index", "mixed"); |
212
|
|
|
|
213
|
4 |
|
$docblock->setTag(new ReturnTag("void")); |
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
4 |
|
$method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]); |
217
|
4 |
|
$method->setDocBlock($docblock); |
218
|
4 |
|
$method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);"); |
219
|
4 |
|
$generator->addMethodFromGenerator($method); |
220
|
4 |
|
} |
221
|
|
|
// //// |
222
|
|
|
|
223
|
7 |
|
$docblock = new DocBlockGenerator(); |
224
|
|
|
|
225
|
7 |
|
$docblock->setShortDescription("Gets as " . $prop->getName()); |
226
|
|
|
|
227
|
7 |
|
if ($prop->getDoc()) { |
228
|
|
|
$docblock->setLongDescription($prop->getDoc()); |
229
|
|
|
} |
230
|
|
|
|
231
|
7 |
|
$tag = new ReturnTag("mixed"); |
|
|
|
|
232
|
7 |
|
$type = $prop->getType(); |
233
|
7 |
|
if ($type && $type instanceof PHPClassOf) { |
234
|
4 |
|
$tt = $type->getArg()->getType(); |
235
|
4 |
|
$tag->setTypes($tt->getPhpType() . "[]"); |
236
|
4 |
View Code Duplication |
if ($p = $tt->isSimpleType()) { |
|
|
|
|
237
|
|
|
if (($t = $p->getType())) { |
238
|
|
|
$tag->setTypes($t->getPhpType() . "[]"); |
239
|
|
|
} |
240
|
|
|
} |
241
|
7 |
|
} elseif ($type) { |
242
|
|
|
|
243
|
3 |
|
if ($p = $type->isSimpleType()) { |
244
|
|
|
if ($t = $p->getType()) { |
245
|
|
|
$tag->setTypes($t->getPhpType()); |
246
|
|
|
} |
247
|
|
|
} else { |
248
|
3 |
|
$tag->setTypes($type->getPhpType()); |
249
|
|
|
} |
250
|
3 |
|
} |
251
|
|
|
|
252
|
7 |
|
$docblock->setTag($tag); |
253
|
|
|
|
254
|
7 |
|
$method = new MethodGenerator("get" . Inflector::classify($prop->getName())); |
255
|
7 |
|
$method->setDocBlock($docblock); |
256
|
7 |
|
$method->setBody("return \$this->" . $prop->getName() . ";"); |
257
|
|
|
|
258
|
7 |
|
$generator->addMethodFromGenerator($method); |
259
|
7 |
|
} |
260
|
|
|
|
261
|
4 |
|
private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
|
|
|
262
|
|
|
{ |
263
|
4 |
|
$type = $prop->getType(); |
264
|
4 |
|
$propName = $type->getArg()->getName(); |
265
|
|
|
|
266
|
4 |
|
$docblock = new DocBlockGenerator(); |
267
|
4 |
|
$docblock->setShortDescription("Adds as $propName"); |
268
|
|
|
|
269
|
4 |
|
if ($prop->getDoc()) { |
270
|
|
|
$docblock->setLongDescription($prop->getDoc()); |
271
|
|
|
} |
272
|
|
|
|
273
|
4 |
|
$return = new ReturnTag(); |
274
|
4 |
|
$return->setTypes("self"); |
275
|
4 |
|
$docblock->setTag($return); |
276
|
|
|
|
277
|
4 |
|
$patramTag = new ParamTag($propName, $type->getArg()->getType()->getPhpType()); |
278
|
4 |
|
$docblock->setTag($patramTag); |
279
|
|
|
|
280
|
4 |
|
$method = new MethodGenerator("addTo" . Inflector::classify($prop->getName())); |
281
|
|
|
|
282
|
4 |
|
$parameter = new ParameterGenerator($propName); |
283
|
4 |
|
$tt = $type->getArg()->getType(); |
284
|
|
|
|
285
|
4 |
|
if (!$tt->isNativeType()) { |
286
|
|
|
|
287
|
1 |
|
if ($p = $tt->isSimpleType()) { |
288
|
|
|
if (($t = $p->getType())) { |
289
|
|
|
$patramTag->setTypes($t->getPhpType()); |
290
|
|
|
|
291
|
|
|
if (!$t->isNativeType()) { |
292
|
|
|
$parameter->setType($t->getPhpType()); |
293
|
|
|
} |
294
|
|
|
} |
295
|
1 |
|
} elseif (!$tt->isNativeType()) { |
296
|
1 |
|
$parameter->setType($tt->getPhpType()); |
297
|
1 |
|
} |
298
|
1 |
|
} |
299
|
|
|
|
300
|
4 |
|
$methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; |
301
|
4 |
|
$methodBody .= "return \$this;"; |
302
|
4 |
|
$method->setBody($methodBody); |
303
|
4 |
|
$method->setDocBlock($docblock); |
304
|
4 |
|
$method->setParameter($parameter); |
305
|
|
|
|
306
|
4 |
|
$generator->addMethodFromGenerator($method); |
307
|
4 |
|
} |
308
|
|
|
|
309
|
7 |
|
private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
310
|
|
|
{ |
311
|
7 |
|
if ($prop->getType() instanceof PHPClassOf) { |
312
|
4 |
|
$this->handleAdder($generator, $prop, $class); |
313
|
4 |
|
} |
314
|
|
|
|
315
|
7 |
|
$this->handleGetter($generator, $prop, $class); |
316
|
7 |
|
$this->handleSetter($generator, $prop, $class); |
317
|
7 |
|
} |
318
|
|
|
|
319
|
8 |
|
private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop) |
320
|
|
|
{ |
321
|
8 |
|
$generatedProp = new PropertyGenerator($prop->getName()); |
322
|
8 |
|
$generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE); |
323
|
|
|
|
324
|
8 |
|
$class->addPropertyFromGenerator($generatedProp); |
325
|
|
|
|
326
|
8 |
|
if ($prop->getType() && (!$prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) { |
|
|
|
|
327
|
|
|
// $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); |
|
|
|
|
328
|
4 |
|
} |
329
|
|
|
|
330
|
8 |
|
$docBlock = new DocBlockGenerator(); |
331
|
8 |
|
$generatedProp->setDocBlock($docBlock); |
332
|
|
|
|
333
|
8 |
|
if ($prop->getDoc()) { |
334
|
|
|
$docBlock->setLongDescription($prop->getDoc()); |
335
|
|
|
} |
336
|
8 |
|
$tag = new PropertyTag($prop->getName(), 'mixed'); |
|
|
|
|
337
|
|
|
|
338
|
8 |
|
$type = $prop->getType(); |
339
|
|
|
|
340
|
8 |
|
if ($type && $type instanceof PHPClassOf) { |
341
|
4 |
|
$tt = $type->getArg()->getType(); |
342
|
4 |
|
$tag->setTypes($tt->getPhpType() . "[]"); |
343
|
4 |
View Code Duplication |
if ($p = $tt->isSimpleType()) { |
|
|
|
|
344
|
|
|
if (($t = $p->getType())) { |
345
|
|
|
$tag->setTypes($t->getPhpType() . "[]"); |
346
|
|
|
} |
347
|
|
|
} |
348
|
8 |
|
} elseif ($type) { |
349
|
|
|
|
350
|
4 |
|
if ($type->isNativeType()) { |
351
|
4 |
|
$tag->setTypes($type->getPhpType()); |
352
|
4 |
|
} elseif (($p = $type->isSimpleType()) && ($t = $p->getType())) { |
353
|
|
|
$tag->setTypes($t->getPhpType()); |
354
|
|
|
} else { |
355
|
|
|
$tag->setTypes($prop->getType()->getPhpType()); |
356
|
|
|
} |
357
|
4 |
|
} |
358
|
8 |
|
$docBlock->setTag($tag); |
359
|
8 |
|
} |
360
|
|
|
|
361
|
8 |
|
public function generate(PHPClass $type) |
362
|
|
|
{ |
363
|
8 |
|
$class = new \Zend\Code\Generator\ClassGenerator(); |
364
|
8 |
|
$docblock = new DocBlockGenerator("Class representing " . $type->getName()); |
365
|
8 |
|
if ($type->getDoc()) { |
366
|
8 |
|
$docblock->setLongDescription($type->getDoc()); |
367
|
8 |
|
} |
368
|
8 |
|
$class->setNamespaceName($type->getNamespace() ?: NULL); |
369
|
8 |
|
$class->setName($type->getName()); |
370
|
8 |
|
$class->setDocblock($docblock); |
371
|
|
|
|
372
|
8 |
|
if ($extends = $type->getExtends()) { |
373
|
|
|
|
374
|
2 |
|
if ($p = $extends->isSimpleType()) { |
375
|
2 |
|
$this->handleProperty($class, $p); |
376
|
2 |
|
$this->handleValueMethod($class, $p, $extends); |
377
|
2 |
|
} else { |
378
|
|
|
|
379
|
|
|
$class->setExtendedClass($extends->getName()); |
380
|
|
|
|
381
|
|
|
if ($extends->getNamespace() != $type->getNamespace()) { |
382
|
|
|
if ($extends->getName() == $type->getName()) { |
383
|
|
|
$class->addUse($type->getExtends() |
384
|
|
|
->getFullName(), $extends->getName() . "Base"); |
385
|
|
|
$class->setExtendedClass($extends->getName() . "Base"); |
386
|
|
|
} else { |
387
|
|
|
$class->addUse($extends->getFullName()); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} |
391
|
2 |
|
} |
392
|
|
|
|
393
|
8 |
|
if ($this->handleBody($class, $type)) { |
394
|
8 |
|
return $class; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|