@@ -15,30 +15,57 @@ trait SwaggerDtoComposerTrait
1515 */
1616 protected function checkDtoAttributes (array $ dto , array $ modelDto , string $ dtoName ): array
1717 {
18- foreach ($ dto as $ param => &$ info ) {
19- if (array_key_exists ('class ' , $ info )) {
20- if ($ info ['is_array ' ]) {
21- $ modelDto ['objects ' ][$ dtoName ][$ param ] = [
22- 'type ' => 'array ' ,
23- 'items ' => [
24- '$ref ' => '#/definitions/ ' . $ info ['type ' ],
25- ]
26- ];
27- } else {
28- $ modelDto ['objects ' ][$ dtoName ][$ param ] = [
29- 'type ' => 'object ' ,
30- '$ref ' => '#/definitions/ ' . $ info ['type ' ],
31- ];
32- }
33- $ modelDto ['objects ' ][$ info ['class ' ]] = $ info ['properties ' ];
34- $ paramDto = $ this ->checkDtoAttributes ($ info ['properties ' ], $ info ['properties ' ], $ info ['class ' ]);
35- if (array_key_exists ('objects ' , $ paramDto )) {
36- $ modelDto ['objects ' ] = array_merge ($ modelDto ['objects ' ], $ paramDto ['objects ' ]);
37- }
38- } else {
18+ $ modelDto ['objects ' ] = $ modelDto ['objects ' ] ?? [];
19+ $ modelDto ['objects ' ][$ dtoName ] = $ modelDto ['objects ' ][$ dtoName ] ?? [];
20+
21+ foreach ($ dto as $ param => $ info ) {
22+ if (!$ this ->isDtoReference ($ info )) {
3923 $ modelDto ['objects ' ][$ dtoName ][$ param ] = $ info ;
24+ continue ;
4025 }
26+ $ modelDto ['objects ' ][$ dtoName ][$ param ] = $ this ->buildDtoReferenceSchema ($ info );
27+ $ modelDto = $ this ->mergeNestedDtoDefinition ($ modelDto , $ info );
4128 }
29+
30+ return $ modelDto ;
31+ }
32+
33+ private function isDtoReference (mixed $ info ): bool
34+ {
35+ return is_array ($ info )
36+ && array_key_exists ('class ' , $ info )
37+ && array_key_exists ('type ' , $ info )
38+ && array_key_exists ('properties ' , $ info );
39+ }
40+
41+ private function buildDtoReferenceSchema (array $ info ): array
42+ {
43+ if ((bool )($ info ['is_array ' ] ?? false )) {
44+ return [
45+ 'type ' => 'array ' ,
46+ 'items ' => [
47+ '$ref ' => '#/definitions/ ' . $ info ['type ' ],
48+ ],
49+ ];
50+ }
51+
52+ return [
53+ 'type ' => 'object ' ,
54+ '$ref ' => '#/definitions/ ' . $ info ['type ' ],
55+ ];
56+ }
57+
58+ private function mergeNestedDtoDefinition (array $ modelDto , array $ info ): array
59+ {
60+ $ className = (string )$ info ['class ' ];
61+ $ properties = is_array ($ info ['properties ' ]) ? $ info ['properties ' ] : [];
62+ $ modelDto ['objects ' ][$ className ] = $ properties ;
63+
64+ $ paramDto = $ this ->checkDtoAttributes ($ properties , ['objects ' => [$ className => $ properties ]], $ className );
65+ if (array_key_exists ('objects ' , $ paramDto ) && is_array ($ paramDto ['objects ' ])) {
66+ $ modelDto ['objects ' ] = array_merge ($ modelDto ['objects ' ], $ paramDto ['objects ' ]);
67+ }
68+
4269 return $ modelDto ;
4370 }
4471}
0 commit comments