19
19
use OpenCodeModeling \JsonSchemaToPhp \Type \ArrayType ;
20
20
use OpenCodeModeling \JsonSchemaToPhp \Type \ReferenceType ;
21
21
use OpenCodeModeling \JsonSchemaToPhp \Type \ScalarType ;
22
- use OpenCodeModeling \JsonSchemaToPhp \Type \StringType ;
23
- use OpenCodeModeling \JsonSchemaToPhp \Type \TypeDefinition ;
24
22
use OpenCodeModeling \JsonSchemaToPhp \Type \TypeSet ;
25
23
use OpenCodeModeling \JsonSchemaToPhpAst \Common \IteratorFactory ;
26
24
use PhpParser \NodeVisitor ;
@@ -201,7 +199,7 @@ public function classBuilder(ArrayType $typeDefinition): ClassBuilder
201
199
return $ this ->classBuilderFromNative ($ name , ...$ typeDefinition ->items ());
202
200
}
203
201
204
- private function determineType (string $ name , TypeSet ...$ typeSets ): ?TypeDefinition
202
+ private function determineTypeName (string $ name , TypeSet ...$ typeSets ): ?string
205
203
{
206
204
if (\count ($ typeSets ) !== 1 ) {
207
205
throw new \RuntimeException ('Can only handle one JSON type ' );
@@ -219,9 +217,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
219
217
$ resolvedTypeSet = $ type ->resolvedType ();
220
218
221
219
if ($ resolvedTypeSet === null ) {
222
- $ resolvedTypeSet = new TypeSet (
223
- StringType::fromDefinition (['type ' => StringType::type (), 'name ' => $ type ->name ()])
224
- );
220
+ return $ type ->extractNameFromReference ();
225
221
}
226
222
if (\count ($ resolvedTypeSet ) !== 1 ) {
227
223
throw new \RuntimeException ('Can only handle one JSON type ' );
@@ -236,7 +232,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
236
232
);
237
233
}
238
234
239
- return $ type ;
235
+ return $ type-> name () ;
240
236
}
241
237
242
238
/**
@@ -246,8 +242,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
246
242
*/
247
243
public function nodeVisitorsFromNative (string $ name , TypeSet ...$ typeSets ): array
248
244
{
249
- $ type = $ this ->determineType ($ name , ...$ typeSets );
250
- $ typeName = $ type ->name ();
245
+ $ typeName = $ this ->determineTypeName ($ name , ...$ typeSets );
251
246
252
247
$ nodeVisitors = $ this ->iteratorFactory ->nodeVisitorsFromNative (
253
248
($ this ->propertyNameFilter )($ name ),
@@ -273,8 +268,7 @@ public function nodeVisitorsFromNative(string $name, TypeSet ...$typeSets): arra
273
268
274
269
public function classBuilderFromNative (string $ name , TypeSet ...$ typeSets ): ClassBuilder
275
270
{
276
- $ type = $ this ->determineType ($ name , ...$ typeSets );
277
- $ typeName = $ type ->name ();
271
+ $ typeName = $ this ->determineTypeName ($ name , ...$ typeSets );
278
272
279
273
$ classBuilder = $ this ->iteratorFactory ->classBuilderFromNative (
280
274
($ this ->propertyNameFilter )($ name ),
@@ -322,10 +316,10 @@ public function methodMagicConstruct(
322
316
$ method = new MethodGenerator (
323
317
'__construct ' ,
324
318
[
325
- (new ParameterGenerator ($ argumentName , ($ this ->classNameFilter )($ argumentType )))->setVariadic (true ),
319
+ (new ParameterGenerator (( $ this -> propertyNameFilter )( $ argumentName) , ($ this ->classNameFilter )($ argumentType )))->setVariadic (true ),
326
320
],
327
321
MethodGenerator::FLAG_PRIVATE ,
328
- new BodyGenerator ($ this ->parser , \sprintf ('$this->%s = $%s; ' , $ propertyName, $ argumentName ))
322
+ new BodyGenerator ($ this ->parser , \sprintf ('$this->%s = $%s; ' , ( $ this -> propertyNameFilter )( $ propertyName), ( $ this -> propertyNameFilter )( $ argumentName) ))
329
323
);
330
324
$ method ->setTyped ($ this ->typed );
331
325
@@ -349,7 +343,14 @@ public function methodAdd(
349
343
(new ParameterGenerator (($ this ->propertyNameFilter )($ argumentType ), ($ this ->classNameFilter )($ argumentType ))),
350
344
],
351
345
MethodGenerator::FLAG_PUBLIC ,
352
- new BodyGenerator ($ this ->parser , \sprintf ($ body , $ propertyName , ($ this ->propertyNameFilter )($ argumentType )))
346
+ new BodyGenerator (
347
+ $ this ->parser ,
348
+ \sprintf (
349
+ $ body ,
350
+ ($ this ->propertyNameFilter )($ propertyName ),
351
+ ($ this ->propertyNameFilter )($ argumentType )
352
+ )
353
+ )
353
354
);
354
355
$ method ->setTyped ($ this ->typed );
355
356
$ method ->setReturnType ('self ' );
@@ -378,7 +379,16 @@ static function($v) use ($%s) { return !$v->equals($%s); }
378
379
(new ParameterGenerator (($ this ->propertyNameFilter )($ argumentType ), ($ this ->classNameFilter )($ argumentType ))),
379
380
],
380
381
MethodGenerator::FLAG_PUBLIC ,
381
- new BodyGenerator ($ this ->parser , \sprintf ($ body , $ propertyName , $ propertyName , ($ this ->propertyNameFilter )($ argumentType ), ($ this ->propertyNameFilter )($ argumentType )))
382
+ new BodyGenerator (
383
+ $ this ->parser ,
384
+ \sprintf (
385
+ $ body ,
386
+ ($ this ->propertyNameFilter )($ propertyName ),
387
+ ($ this ->propertyNameFilter )($ propertyName ),
388
+ ($ this ->propertyNameFilter )($ argumentType ),
389
+ ($ this ->propertyNameFilter )($ argumentType )
390
+ )
391
+ )
382
392
);
383
393
$ method ->setTyped ($ this ->typed );
384
394
$ method ->setReturnType ('self ' );
@@ -479,7 +489,7 @@ static function($%s) use ($filter) { return $filter($%s); }
479
489
new ParameterGenerator ('filter ' , 'callable ' ),
480
490
],
481
491
MethodGenerator::FLAG_PUBLIC ,
482
- new BodyGenerator ($ this ->parser , \sprintf ($ body , $ propertyName , 'v ' , 'v ' ))
492
+ new BodyGenerator ($ this ->parser , \sprintf ($ body , ( $ this -> propertyNameFilter )( $ propertyName) , 'v ' , 'v ' ))
483
493
);
484
494
$ method ->setTyped ($ this ->typed );
485
495
$ method ->setReturnType ('self ' );
@@ -566,10 +576,10 @@ public function methodFromItems(
566
576
$ method = new MethodGenerator (
567
577
'fromItems ' ,
568
578
[
569
- (new ParameterGenerator ($ argumentName , ($ this ->classNameFilter )($ argumentType )))->setVariadic (true ),
579
+ (new ParameterGenerator (( $ this -> propertyNameFilter )( $ argumentName) , ($ this ->classNameFilter )($ argumentType )))->setVariadic (true ),
570
580
],
571
581
MethodGenerator::FLAG_PUBLIC | MethodGenerator::FLAG_STATIC ,
572
- new BodyGenerator ($ this ->parser , \sprintf ('return new self(...$%s); ' , $ argumentName ))
582
+ new BodyGenerator ($ this ->parser , \sprintf ('return new self(...$%s); ' , ( $ this -> propertyNameFilter )( $ argumentName) ))
573
583
);
574
584
$ method ->setTyped ($ this ->typed );
575
585
$ method ->setReturnType ('self ' );
0 commit comments