@@ -25,16 +25,16 @@ public function generate(SpecObjectInterface $specObject): void
2525 $ enums = $ this ->extractEnums ($ openApiData );
2626
2727 $ template = $ this ->templatesManager ->getTemplate ('Enum.template ' );
28- foreach ($ enums as $ className => $ schema ) {
29- $ enumType = get_debug_type ( $ schema -> enum [ 0 ] );
28+ foreach ($ enums as $ enumName => $ schema ) {
29+ $ enumType = $ this -> getEnumType ( $ schema , $ enumName );
3030 $ this ->filesystem ->put (
31- rtrim ($ toDir , '/ ' ) . "/ {$ className }.php " ,
31+ rtrim ($ toDir , '/ ' ) . "/ {$ enumName }.php " ,
3232 $ this ->replacePlaceholders ($ template , [
3333 '{{ namespace }} ' => $ namespace ,
34- '{{ className }} ' => $ className ,
35- '{{ constants }} ' => $ this ->convertEnumSchemaToConstants ($ schema ),
34+ '{{ enumName }} ' => $ enumName ,
35+ '{{ cases }} ' => $ this ->convertEnumSchemaToCases ($ schema ),
3636 '{{ enumType }} ' => $ enumType ,
37- '{{ valuesArray }} ' => $ this ->convertEnumSchemaToValuesArray ($ schema ),
37+ '{{ enumPhpDoc }} ' => $ this ->convertEnumSchemaToPhpDoc ($ schema ),
3838 ])
3939 );
4040 }
@@ -47,29 +47,38 @@ private function extractEnums(stdClass $openApiData): array
4747 return array_filter ($ schemas , fn ($ schema ) => !empty ($ schema ->enum ));
4848 }
4949
50- private function convertEnumSchemaToConstants (stdClass $ schema ): string
50+ private function getEnumType (stdClass $ schema , string $ enumName ): string
51+ {
52+ return match ($ schema ->type ) {
53+ "integer " => "int " ,
54+ "string " => "string " ,
55+ default => throw new LogicException ("Enum {$ enumName } has invalid type ' {$ schema ->type }'. Supported types are: ['integer', 'string'] " ),
56+ };
57+ }
58+
59+ private function convertEnumSchemaToCases (stdClass $ schema ): string
5160 {
5261 $ result = '' ;
5362 foreach ($ schema ->enum as $ i => $ enum ) {
5463 $ varName = $ schema ->{'x-enum-varnames ' }[$ i ] ?? null ;
5564 if ($ varName === null ) {
5665 throw new LogicException ("x-enum-varnames for enum \"{$ enum }\" is not set " );
5766 }
67+ $ description = $ schema ->{'x-enum-descriptions ' }[$ i ] ?? null ;
68+ if ($ description ) {
69+ $ result .= " /** {$ description } */ \n" ;
70+ }
5871 $ value = var_export ($ enum , true );
59- $ result .= " public const {$ varName } = {$ value }; \n" ;
72+ $ result .= " case {$ varName } = {$ value }; \n" ;
6073 }
6174
62- return $ result ;
75+ return rtrim ( $ result, "\n" ) ;
6376 }
6477
65- private function convertEnumSchemaToValuesArray (stdClass $ schema ): string
78+ private function convertEnumSchemaToPhpDoc (stdClass $ schema ): string
6679 {
67- $ result = "[ \n" ;
68- foreach ($ schema ->{'x-enum-varnames ' } as $ varName ) {
69- $ result .= " self:: {$ varName }, \n" ;
70- }
71- $ result .= ' ] ' ;
72-
73- return $ result ;
80+ return $ schema ->description
81+ ? "\n" . $ this ->phpDocGenerator ->fromText (text: $ schema ->description , deleteEmptyLines: true )
82+ : "\n" ;
7483 }
7584}
0 commit comments