1+ <?php 
2+ 
3+ namespace  Riverwaysoft \DtoConverter \Language \Dart ;
4+ 
5+ use  Riverwaysoft \DtoConverter \Dto \DtoEnumProperty ;
6+ use  Riverwaysoft \DtoConverter \Dto \DtoList ;
7+ use  Riverwaysoft \DtoConverter \Dto \DtoType ;
8+ use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpListType ;
9+ use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpTypeInterface ;
10+ use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpUnionType ;
11+ use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpUnknownType ;
12+ use  Webmozart \Assert \Assert ;
13+ 
14+ class  DartClassFactoryGenerator
15+ {
16+     public  function  __construct (private  string |null  $ includePatternnull )
17+     {
18+ 
19+     }
20+ 
21+     public  function  generateClassFactory (DtoType $ dtoDtoList $ dtoListstring 
22+     {
23+         if  (empty ($ dtoListgetList ())) {
24+             return  '' ;
25+         }
26+ 
27+         if  ($ this includePattern  && !preg_match (pattern: $ this includePattern , subject: $ dtogetName ())) {
28+            return  '' ;
29+         }
30+ 
31+         $ factoryProperties'' ;
32+ 
33+         foreach  ($ dtogetProperties () as  $ property
34+             Assert::false ($ propertyinstanceof  DtoEnumProperty, "Dart factories only work in a class context, not enum " );
35+             $ propertyValue$ this resolveFactoryProperty ($ propertygetName (), $ propertygetType (), $ dtoList
36+             $ factoryPropertiessprintf ("      %s: %s, \n" , $ propertygetName (), $ propertyValue
37+         }
38+ 
39+         return  sprintf ("\n  factory %s.fromJson(Map<String, dynamic> json) {  
40+     return %s( \n%s    ); 
41+   } \n"
42+             $ dtogetName (),
43+             $ dtogetName (),
44+             $ factoryProperties
45+         );
46+     }
47+ 
48+     private  function  resolveFactoryProperty (string  $ propertyNamePhpTypeInterface $ typeDtoList $ dtoListstring 
49+     {
50+         if  ($ typeinstanceof  PhpUnionType && $ typeisNullable ()) {
51+             return  sprintf ("json[' {$ propertyName'] != null ? %s : null " , $ this resolveFactoryProperty ($ propertyName$ typegetFirstNotNullType (), $ dtoList
52+         }
53+ 
54+         if  ($ typeinstanceof  PhpListType) {
55+             $ collectionType$ typegetType ();
56+             if  (!($ collectionTypeinstanceof  PhpUnknownType)) {
57+                 throw  new  \Exception ('Only class instance can be converted to collection ' );
58+             }
59+             $ class$ collectionTypegetName ();
60+             return  sprintf ("List<%s>.from(json['%s'].map((e) => %s.fromJson(e))) " , $ class$ propertyName$ class
61+         }
62+ 
63+         if  ($ typeinstanceof  PhpUnknownType) {
64+             if  ($ typegetName () === 'DateTime '  || $ typegetName () === 'DateTimeImmutable ' ) {
65+                 return  sprintf ("DateTime.parse(json['%s']) " , $ propertyName
66+             }
67+ 
68+             if  ($ dtoListgetDtoByType ($ typegetName ())?->getExpressionType()->isAnyEnum ()) {
69+                 return  sprintf ("%s.values[json['%s']] " , $ typegetName (), $ propertyName
70+             }
71+ 
72+             return  sprintf ("%s.fromJson(json['%s']) " , $ typegetName (), $ propertyName
73+         }
74+ 
75+         return  sprintf ("json['%s'] " , $ propertyName
76+     }
77+ }
0 commit comments