@@ -18,7 +18,9 @@ class AnnotationParameterData
18
18
public bool $ nullable ;
19
19
public ?string $ example ;
20
20
public ?string $ nestedArraySwaggerType ;
21
+ public ?int $ arrayDepth ;
21
22
public ?array $ nestedObjectParameterData ;
23
+ public ?ParameterConstraints $ constraints ;
22
24
23
25
public function __construct (
24
26
string $ swaggerType ,
@@ -29,7 +31,9 @@ public function __construct(
29
31
bool $ nullable ,
30
32
?string $ example = null ,
31
33
?string $ nestedArraySwaggerType = null ,
34
+ ?int $ arrayDepth = null ,
32
35
?array $ nestedObjectParameterData = null ,
36
+ ?ParameterConstraints $ constraints = null ,
33
37
) {
34
38
$ this ->swaggerType = $ swaggerType ;
35
39
$ this ->name = $ name ;
@@ -39,7 +43,9 @@ public function __construct(
39
43
$ this ->nullable = $ nullable ;
40
44
$ this ->example = $ example ;
41
45
$ this ->nestedArraySwaggerType = $ nestedArraySwaggerType ;
46
+ $ this ->arrayDepth = $ arrayDepth ;
42
47
$ this ->nestedObjectParameterData = $ nestedObjectParameterData ;
48
+ $ this ->constraints = $ constraints ;
43
49
}
44
50
45
51
private function addArrayItemsIfArray (ParenthesesBuilder $ container )
@@ -49,18 +55,42 @@ private function addArrayItemsIfArray(ParenthesesBuilder $container)
49
55
}
50
56
51
57
$ itemsHead = "@OA \\Items " ;
52
- $ items = new ParenthesesBuilder ();
58
+ $ layers = [];
59
+ for ($ i = 0 ; $ i < $ this ->arrayDepth ; $ i ++) {
60
+ $ items = new ParenthesesBuilder ();
61
+
62
+ // add array time for all nested arrays
63
+ if ($ i < $ this ->arrayDepth - 1 ) {
64
+ $ items ->addKeyValue ("type " , "array " );
65
+ // handle bottommost elements
66
+ } else {
67
+ // add element type if present
68
+ if ($ this ->nestedArraySwaggerType !== null ) {
69
+ $ items ->addKeyValue ("type " , $ this ->nestedArraySwaggerType );
70
+ }
71
+
72
+ // add example value
73
+ if ($ this ->example != null ) {
74
+ $ items ->addKeyValue ("example " , $ this ->example );
75
+ }
76
+
77
+ // add constraints
78
+ $ this ->constraints ?->addConstraints($ items );
79
+ }
53
80
54
- if ($ this ->nestedArraySwaggerType !== null ) {
55
- $ items ->addKeyValue ("type " , $ this ->nestedArraySwaggerType );
81
+ $ layers [] = $ items ;
56
82
}
57
83
58
- // add example value
59
- if ($ this ->example != null ) {
60
- $ items ->addKeyValue ("example " , $ this ->example );
84
+ // serialize the layers from the bottom up
85
+ $ layers = array_reverse ($ layers );
86
+ $ serialized_layer = $ itemsHead . $ layers [0 ]->toString ();
87
+ for ($ i = 1 ; $ i < $ this ->arrayDepth ; $ i ++) {
88
+ $ layer = $ layers [$ i ];
89
+ $ layer ->addValue ($ serialized_layer );
90
+ $ serialized_layer = $ itemsHead . $ layer ->toString ();
61
91
}
62
92
63
- $ container ->addValue ($ itemsHead . $ items -> toString () );
93
+ $ container ->addValue ($ serialized_layer );
64
94
}
65
95
66
96
private function addObjectParamsIfObject (ParenthesesBuilder $ container )
@@ -85,6 +115,7 @@ private function generateSchemaAnnotation(): string
85
115
$ body = new ParenthesesBuilder ();
86
116
87
117
$ body ->addKeyValue ("type " , $ this ->swaggerType );
118
+ $ body ->addKeyValue ("nullable " , $ this ->nullable );
88
119
$ this ->addArrayItemsIfArray ($ body );
89
120
90
121
return $ head . $ body ->toString ();
@@ -128,6 +159,11 @@ public function toPropertyAnnotation(): string
128
159
$ body ->addKeyValue ("description " , $ this ->description );
129
160
}
130
161
162
+ // handle param constraints (array constrains have to be added to the element, not the array)
163
+ if ($ this ->swaggerType !== "array " ) {
164
+ $ this ->constraints ?->addConstraints($ body );
165
+ }
166
+
131
167
// handle arrays
132
168
$ this ->addArrayItemsIfArray ($ body );
133
169
0 commit comments