@@ -44,8 +44,8 @@ public static function generate(string $pathPrefix, Representation\Schema $schem
4444 $ className = $ schema ->className ;
4545 if (count ($ aliases ) > 0 ) {
4646 $ className = ClassString::factory (
47- $ className ->namespace ,
48- 'Schema \\AliasAbstract \\Abstract ' . md5 (json_encode ($ schema ->schema ->getSerializableData ())),
47+ $ className ->baseNamespaces ,
48+ 'Schema \\AliasAbstract \\Tiet ' . implode ( '\\ Tiet ' , str_split ( strtoupper ( md5 (json_encode ($ schema ->schema ->getSerializableData ()))), 8 )),
4949 );
5050 $ aliases [] = $ schema ->className ;
5151 }
@@ -133,6 +133,19 @@ public static function generate(string $pathPrefix, Representation\Schema $schem
133133 $ types = [];
134134 if ($ property ->type ->type === 'union ' && is_array ($ property ->type ->payload )) {
135135 $ types [] = self ::buildUnionType ($ property ->type );
136+ $ schemaClasses = [...self ::getUnionTypeSchemas ($ property ->type )];
137+
138+ if (count ($ schemaClasses ) > 0 ) {
139+ $ castToUnionToType = ClassString::factory ($ schema ->className ->baseNamespaces , Utils::className ('Attribute \\CastUnionToType \\' . $ schema ->className ->relative . '\\' . $ property ->name ));
140+
141+ yield from CastUnionToType::generate ($ pathPrefix , $ castToUnionToType , ...$ schemaClasses );
142+
143+ $ constructorParam ->addAttribute (
144+ new Node \Attribute (
145+ new Node \Name ($ castToUnionToType ->fullyQualified ->source ),
146+ ),
147+ );
148+ }
136149 }
137150
138151 if ($ property ->type ->type === 'array ' && ! is_string ($ property ->type ->payload )) {
@@ -196,7 +209,11 @@ public static function generate(string $pathPrefix, Representation\Schema $schem
196209 $ nullable = count ($ types ) > 1 || count (explode ('| ' , implode ('| ' , $ types ))) > 1 ? 'null| ' : '? ' ;
197210 }
198211
199- $ constructor ->addParam ($ constructorParam ->setType ($ nullable . implode ('| ' , $ types )));
212+ if (count ($ types ) > 0 ) {
213+ $ constructorParam ->setType ($ nullable . implode ('| ' , $ types ));
214+ }
215+
216+ $ constructor ->addParam ($ constructorParam );
200217 }
201218
202219 if (count ($ constructDocBlock ) > 0 ) {
@@ -211,7 +228,7 @@ public static function generate(string $pathPrefix, Representation\Schema $schem
211228 $ aliasTms = $ factory ->namespace ($ alias ->namespace ->source );
212229 $ aliasClass = $ factory ->class ($ alias ->className )->makeFinal ()->makeReadonly ()->extend ($ className ->relative );
213230
214- yield new File ($ pathPrefix , $ alias ->className , $ aliasTms ->addStmt ($ aliasClass )->getNode ());
231+ yield new File ($ pathPrefix , $ alias ->relative , $ aliasTms ->addStmt ($ aliasClass )->getNode ());
215232 }
216233 }
217234
0 commit comments