17
17
use OpenCodeModeling \CodeAst \Code \ParameterGenerator ;
18
18
use OpenCodeModeling \CodeAst \NodeVisitor \ClassMethod ;
19
19
use OpenCodeModeling \JsonSchemaToPhp \Type \ArrayType ;
20
+ use OpenCodeModeling \JsonSchemaToPhp \Type \BooleanType ;
21
+ use OpenCodeModeling \JsonSchemaToPhp \Type \IntegerType ;
22
+ use OpenCodeModeling \JsonSchemaToPhp \Type \NumberType ;
23
+ use OpenCodeModeling \JsonSchemaToPhp \Type \ObjectType ;
20
24
use OpenCodeModeling \JsonSchemaToPhp \Type \ReferenceType ;
21
25
use OpenCodeModeling \JsonSchemaToPhp \Type \ScalarType ;
26
+ use OpenCodeModeling \JsonSchemaToPhp \Type \StringType ;
27
+ use OpenCodeModeling \JsonSchemaToPhp \Type \TypeDefinition ;
22
28
use OpenCodeModeling \JsonSchemaToPhp \Type \TypeSet ;
23
29
use OpenCodeModeling \JsonSchemaToPhpAst \Common \IteratorFactory ;
24
30
use PhpParser \NodeVisitor ;
@@ -199,7 +205,7 @@ public function classBuilder(ArrayType $typeDefinition): ClassBuilder
199
205
return $ this ->classBuilderFromNative ($ name , ...$ typeDefinition ->items ());
200
206
}
201
207
202
- private function determineTypeName (string $ name , TypeSet ...$ typeSets ): ?string
208
+ private function determineType (string $ name , TypeSet ...$ typeSets ): ?TypeDefinition
203
209
{
204
210
if (\count ($ typeSets ) !== 1 ) {
205
211
throw new \RuntimeException ('Can only handle one JSON type ' );
@@ -217,7 +223,7 @@ private function determineTypeName(string $name, TypeSet ...$typeSets): ?string
217
223
$ resolvedTypeSet = $ type ->resolvedType ();
218
224
219
225
if ($ resolvedTypeSet === null ) {
220
- return $ type-> extractNameFromReference () ;
226
+ return $ type ;
221
227
}
222
228
if (\count ($ resolvedTypeSet ) !== 1 ) {
223
229
throw new \RuntimeException ('Can only handle one JSON type ' );
@@ -232,7 +238,25 @@ private function determineTypeName(string $name, TypeSet ...$typeSets): ?string
232
238
);
233
239
}
234
240
235
- return $ type ->name ();
241
+ return $ type ;
242
+ }
243
+
244
+ private function determineTypeMethod (TypeDefinition $ type ): string
245
+ {
246
+ switch (true ) {
247
+ case $ type instanceof ArrayType:
248
+ case $ type instanceof ObjectType:
249
+ return 'Array ' ;
250
+ case $ type instanceof BooleanType:
251
+ return 'Bool ' ;
252
+ case $ type instanceof IntegerType:
253
+ return 'Int ' ;
254
+ case $ type instanceof NumberType:
255
+ return 'Float ' ;
256
+ case $ type instanceof StringType:
257
+ default :
258
+ return 'String ' ;
259
+ }
236
260
}
237
261
238
262
/**
@@ -242,14 +266,24 @@ private function determineTypeName(string $name, TypeSet ...$typeSets): ?string
242
266
*/
243
267
public function nodeVisitorsFromNative (string $ name , TypeSet ...$ typeSets ): array
244
268
{
245
- $ typeName = $ this ->determineTypeName ($ name , ...$ typeSets );
269
+ $ type = $ this ->determineType ($ name , ...$ typeSets );
270
+
271
+ if ($ type === null ) {
272
+ throw new \RuntimeException ('Could not determine JSON schema type ' );
273
+ }
274
+ $ typeName = $ type ->name ();
275
+
276
+ if ($ type instanceof ReferenceType) {
277
+ $ typeName = $ type ->extractNameFromReference ();
278
+ }
279
+ $ typeMethod = $ this ->determineTypeMethod ($ type );
246
280
247
281
$ nodeVisitors = $ this ->iteratorFactory ->nodeVisitorsFromNative (
248
282
($ this ->propertyNameFilter )($ name ),
249
283
($ this ->classNameFilter )($ typeName )
250
284
);
251
285
252
- $ nodeVisitors [] = new ClassMethod ($ this ->methodFromArray ($ name , $ typeName ));
286
+ $ nodeVisitors [] = new ClassMethod ($ this ->methodFromArray ($ name , $ typeName, ' from ' . $ typeMethod ));
253
287
$ nodeVisitors [] = new ClassMethod ($ this ->methodFromItems ($ name , $ typeName ));
254
288
$ nodeVisitors [] = new ClassMethod ($ this ->methodEmptyList ());
255
289
$ nodeVisitors [] = new ClassMethod ($ this ->methodMagicConstruct ($ name , $ name , $ typeName ));
@@ -260,22 +294,32 @@ public function nodeVisitorsFromNative(string $name, TypeSet ...$typeSets): arra
260
294
$ nodeVisitors [] = new ClassMethod ($ this ->methodContains ($ name , $ typeName ));
261
295
$ nodeVisitors [] = new ClassMethod ($ this ->methodFilter ($ name ));
262
296
$ nodeVisitors [] = new ClassMethod ($ this ->methodItems ($ name , $ typeName ));
263
- $ nodeVisitors [] = new ClassMethod ($ this ->methodToArray ($ name , $ typeName ));
297
+ $ nodeVisitors [] = new ClassMethod ($ this ->methodToArray ($ name , $ typeName, ' to ' . $ typeMethod ));
264
298
$ nodeVisitors [] = new ClassMethod ($ this ->methodEquals ());
265
299
266
300
return $ nodeVisitors ;
267
301
}
268
302
269
303
public function classBuilderFromNative (string $ name , TypeSet ...$ typeSets ): ClassBuilder
270
304
{
271
- $ typeName = $ this ->determineTypeName ($ name , ...$ typeSets );
305
+ $ type = $ this ->determineType ($ name , ...$ typeSets );
306
+
307
+ if ($ type === null ) {
308
+ throw new \RuntimeException ('Could not determine JSON schema type ' );
309
+ }
310
+ $ typeName = $ type ->name ();
311
+
312
+ if ($ type instanceof ReferenceType) {
313
+ $ typeName = $ type ->extractNameFromReference ();
314
+ }
315
+ $ typeMethod = $ this ->determineTypeMethod ($ type );
272
316
273
317
$ classBuilder = $ this ->iteratorFactory ->classBuilderFromNative (
274
318
($ this ->propertyNameFilter )($ name ),
275
319
($ this ->classNameFilter )($ typeName )
276
320
);
277
321
$ classBuilder ->addMethod (
278
- ClassMethodBuilder::fromNode ($ this ->methodFromArray ($ name , $ typeName )->generate ()),
322
+ ClassMethodBuilder::fromNode ($ this ->methodFromArray ($ name , $ typeName, ' from ' . $ typeMethod )->generate ()),
279
323
ClassMethodBuilder::fromNode ($ this ->methodFromItems ($ name , $ typeName )->generate ()),
280
324
ClassMethodBuilder::fromNode ($ this ->methodEmptyList ()->generate ()),
281
325
ClassMethodBuilder::fromNode ($ this ->methodMagicConstruct ($ name , $ name , $ typeName )->generate ()),
@@ -286,7 +330,7 @@ public function classBuilderFromNative(string $name, TypeSet ...$typeSets): Clas
286
330
ClassMethodBuilder::fromNode ($ this ->methodContains ($ name , $ typeName )->generate ()),
287
331
ClassMethodBuilder::fromNode ($ this ->methodFilter ($ name )->generate ()),
288
332
ClassMethodBuilder::fromNode ($ this ->methodItems ($ name , $ typeName )->generate ()),
289
- ClassMethodBuilder::fromNode ($ this ->methodToArray ($ name , $ typeName )->generate ()),
333
+ ClassMethodBuilder::fromNode ($ this ->methodToArray ($ name , $ typeName, ' to ' . $ typeMethod )->generate ()),
290
334
ClassMethodBuilder::fromNode ($ this ->methodEquals ()->generate ()),
291
335
);
292
336
@@ -518,11 +562,12 @@ public function methodItems(
518
562
519
563
public function methodToArray (
520
564
string $ propertyName ,
521
- string $ argumentType
565
+ string $ argumentType ,
566
+ string $ typeMethod = 'toString '
522
567
): MethodGenerator {
523
568
$ body = <<<'PHP'
524
569
return \array_map(static function (%s $%s) {
525
- return $%s->toString ();
570
+ return $%s->%s ();
526
571
}, $this->%s);
527
572
PHP;
528
573
@@ -534,7 +579,7 @@ public function methodToArray(
534
579
'toArray ' ,
535
580
[],
536
581
MethodGenerator::FLAG_PUBLIC ,
537
- new BodyGenerator ($ this ->parser , \sprintf ($ body , $ argumentType , $ argumentTypeVarName , $ argumentTypeVarName , $ propertyName ))
582
+ new BodyGenerator ($ this ->parser , \sprintf ($ body , $ argumentType , $ argumentTypeVarName , $ argumentTypeVarName , $ typeMethod , $ propertyName ))
538
583
);
539
584
$ method ->setTyped ($ this ->typed );
540
585
$ method ->setReturnType ('array ' );
@@ -544,11 +589,12 @@ public function methodToArray(
544
589
545
590
public function methodFromArray (
546
591
string $ argumentName ,
547
- string $ typeName
592
+ string $ typeName ,
593
+ string $ typeMethod = 'fromString '
548
594
): MethodGenerator {
549
595
$ body = <<<'PHP'
550
596
return new self(...array_map(static function (string $item) {
551
- return %s::fromString ($item);
597
+ return %s::%s ($item);
552
598
}, $%s));
553
599
PHP;
554
600
$ argumentName = ($ this ->propertyNameFilter )($ argumentName );
@@ -560,7 +606,7 @@ public function methodFromArray(
560
606
new ParameterGenerator ($ argumentName , 'array ' ),
561
607
],
562
608
MethodGenerator::FLAG_PUBLIC | MethodGenerator::FLAG_STATIC ,
563
- new BodyGenerator ($ this ->parser , \sprintf ($ body , $ typeName , $ argumentName ))
609
+ new BodyGenerator ($ this ->parser , \sprintf ($ body , $ typeName , $ typeMethod , $ argumentName ))
564
610
);
565
611
$ method ->setTyped ($ this ->typed );
566
612
$ method ->setReturnType ('self ' );
0 commit comments