55use  Riverwaysoft \DtoConverter \Dto \DtoEnumProperty ;
66use  Riverwaysoft \DtoConverter \Dto \DtoList ;
77use  Riverwaysoft \DtoConverter \Dto \DtoType ;
8+ use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpBaseType ;
89use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpListType ;
910use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpTypeInterface ;
1011use  Riverwaysoft \DtoConverter \Dto \PhpType \PhpUnionType ;
1314
1415class  DartClassFactoryGenerator
1516{
16-     public  function  __construct (private  string |null  $ includePattern null )
17+     public  function  __construct (private  string |null  $ excludePattern null )
1718    {
1819
1920    }
@@ -24,15 +25,15 @@ public function generateClassFactory(DtoType $dto, DtoList $dtoList): string
2425            return  '' ;
2526        }
2627
27-         if  ($ this includePattern  && ! preg_match (pattern: $ this includePattern , subject: $ dtogetName ())) {
28-            return  '' ;
28+         if  ($ this excludePattern  && preg_match (pattern: $ this excludePattern , subject: $ dtogetName ())) {
29+              return  '' ;
2930        }
3031
3132        $ factoryProperties'' ;
3233
3334        foreach  ($ dtogetProperties () as  $ property
3435            Assert::false ($ propertyinstanceof  DtoEnumProperty, "Dart factories only work in a class context, not enum " );
35-             $ propertyValue$ this resolveFactoryProperty ($ propertygetName (), $ propertygetType (), $ dtoList
36+             $ propertyValue$ this resolveFactoryProperty ($ propertygetName (), $ propertygetType (), $ dto ,  $ dtoList
3637            $ factoryPropertiessprintf ("      %s: %s, \n" , $ propertygetName (), $ propertyValue
3738        }
3839
@@ -45,19 +46,36 @@ public function generateClassFactory(DtoType $dto, DtoList $dtoList): string
4546        );
4647    }
4748
48-     private  function  resolveFactoryProperty (string  $ propertyNamePhpTypeInterface $ typeDtoList $ dtoListstring 
49+     private  function  resolveFactoryProperty (string  $ propertyNamePhpTypeInterface $ typeDtoType   $ dto ,  DtoList $ dtoListstring 
4950    {
5051        if  ($ typeinstanceof  PhpUnionType && $ typeisNullable ()) {
51-             return  sprintf ("json[' {$ propertyName'] != null ? %s : null " , $ this resolveFactoryProperty ($ propertyName$ typegetFirstNotNullType (), $ dtoList
52+             return  sprintf ("json[' {$ propertyName'] != null ? %s : null " , $ this resolveFactoryProperty ($ propertyName$ typegetFirstNotNullType (), $ dto ,  $ dtoList
5253        }
5354
5455        if  ($ typeinstanceof  PhpListType) {
5556            $ collectionType$ typegetType ();
56-             if  (!($ collectionTypeinstanceof  PhpUnknownType)) {
57-                 throw  new  \Exception ('Only class instance can be converted to collection ' );
57+ 
58+             if  ($ collectionTypeinstanceof  PhpUnknownType) {
59+                 $ collectionInnerType$ collectionTypegetName ();
60+                 return  sprintf ("List<%s>.from(json['%s'].map((e) => %s.fromJson(e))) " , $ collectionInnerType$ propertyName$ collectionInnerType
61+             }
62+ 
63+             if  ($ collectionTypeinstanceof  PhpBaseType) {
64+                 $ dartTypematch  (true ) {
65+                     $ collectionTypeequalsTo (PhpBaseType::int ()) => 'int ' ,
66+                     $ collectionTypeequalsTo (PhpBaseType::float ()) => 'double ' ,
67+                     $ collectionTypeequalsTo (PhpBaseType::string ()) => 'String ' ,
68+                     $ collectionTypeequalsTo (PhpBaseType::bool ()) => 'bool ' ,
69+                     $ collectionTypeequalsTo (PhpBaseType::mixed ()), $ collectionTypeequalsTo (PhpBaseType::iterable ()), $ collectionTypeequalsTo (PhpBaseType::array ()) => 'Object ' ,
70+                     $ collectionTypeequalsTo (PhpBaseType::null ()) => 'null ' ,
71+                     $ collectionTypeequalsTo (PhpBaseType::self ()) => $ dtogetName (),
72+                     default  => throw  new  \Exception (sprintf ("Unknown base PHP type: %s " , $ typejsonSerialize ()))
73+                 };
74+ 
75+                 return  sprintf ("List<%s>.from(json['%s']) " , $ dartType$ propertyName
5876            }
59-              $ class  =  $ collectionType -> getName (); 
60-             return   sprintf ("List<%s>.from(json['%s'].map((e) =>  %s.fromJson(e)))  , $ class $ propertyName,  $ class 
77+ 
78+             throw   new  \ Exception ( sprintf ("Only PHP base types and class instance can be converted to collection. Property:  %s#%s  , $ dto -> getName () , $ propertyName) );
6179        }
6280
6381        if  ($ typeinstanceof  PhpUnknownType) {
0 commit comments