15
15
use OpenCodeModeling \CodeAst \Builder \FileCollection ;
16
16
use OpenCodeModeling \CodeAst \Code \ClassConstGenerator ;
17
17
use OpenCodeModeling \CodeAst \Package \ClassInfoList ;
18
+ use OpenCodeModeling \JsonSchemaToPhp \Type \CustomSupport ;
19
+ use OpenCodeModeling \JsonSchemaToPhp \Type \TypeDefinition ;
18
20
use OpenCodeModeling \JsonSchemaToPhp \Type \TypeSet ;
19
21
use OpenCodeModeling \JsonSchemaToPhpAst \ValueObjectFactory ;
20
22
@@ -196,6 +198,12 @@ public function generateImmutableRecord(
196
198
$ fileCollection = FileCollection::fromItems ($ classBuilder );
197
199
198
200
if ($ jsonSchemaSet !== null ) {
201
+ $ classBuilder ->setNamespace (
202
+ $ this ->extractNamespace (
203
+ $ classInfo ->getClassNamespaceFromPath ($ immutableRecordDirectory ), $ jsonSchemaSet ->first ()
204
+ )
205
+ );
206
+
199
207
$ this ->valueObjectFactory ->generateClasses (
200
208
$ classBuilder ,
201
209
$ fileCollection ,
@@ -219,4 +227,18 @@ private function codeImmutableRecordLogic(ClassBuilder $file): void
219
227
$ file ->addImplement ('ImmutableRecord ' )
220
228
->addTrait ('ImmutableRecordLogic ' );
221
229
}
230
+
231
+ private function extractNamespace (string $ classNamespacePath , TypeDefinition $ typeDefinition ): string
232
+ {
233
+ if (! $ typeDefinition instanceof CustomSupport) {
234
+ return $ classNamespacePath ;
235
+ }
236
+ $ namespace = $ typeDefinition ->custom ()['namespace ' ] ?? '' ;
237
+
238
+ if ($ namespace === '' ) {
239
+ $ namespace = $ typeDefinition ->custom ()['ns ' ] ?? '' ;
240
+ }
241
+
242
+ return \trim ($ classNamespacePath . '\\' . $ namespace , '\\' );
243
+ }
222
244
}
0 commit comments