@@ -18,6 +18,7 @@ class AnnotationParameterData
1818 public bool $ nullable ;
1919 public ?string $ example ;
2020 public ?string $ nestedArraySwaggerType ;
21+ public ?int $ arrayDepth ;
2122 public ?array $ nestedObjectParameterData ;
2223 public ?ParameterConstraints $ constraints ;
2324
@@ -30,6 +31,7 @@ public function __construct(
3031 bool $ nullable ,
3132 ?string $ example = null ,
3233 ?string $ nestedArraySwaggerType = null ,
34+ ?int $ arrayDepth = null ,
3335 ?array $ nestedObjectParameterData = null ,
3436 ?ParameterConstraints $ constraints = null ,
3537 ) {
@@ -41,30 +43,54 @@ public function __construct(
4143 $ this ->nullable = $ nullable ;
4244 $ this ->example = $ example ;
4345 $ this ->nestedArraySwaggerType = $ nestedArraySwaggerType ;
46+ $ this ->arrayDepth = $ arrayDepth ;
4447 $ this ->nestedObjectParameterData = $ nestedObjectParameterData ;
4548 $ this ->constraints = $ constraints ;
4649 }
4750
4851 private function addArrayItemsIfArray (ParenthesesBuilder $ container )
4952 {
50- ///TODO: nested constraints should be added here
5153 if ($ this ->swaggerType !== "array " ) {
5254 return ;
5355 }
5456
5557 $ itemsHead = "@OA \\Items " ;
56- $ 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+ }
5780
58- if ($ this ->nestedArraySwaggerType !== null ) {
59- $ items ->addKeyValue ("type " , $ this ->nestedArraySwaggerType );
81+ $ layers [] = $ items ;
6082 }
6183
62- // add example value
63- if ($ this ->example != null ) {
64- $ 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 ();
6591 }
6692
67- $ container ->addValue ($ itemsHead . $ items -> toString () );
93+ $ container ->addValue ($ serialized_layer );
6894 }
6995
7096 private function addObjectParamsIfObject (ParenthesesBuilder $ container )
@@ -133,8 +159,10 @@ public function toPropertyAnnotation(): string
133159 $ body ->addKeyValue ("description " , $ this ->description );
134160 }
135161
136- // handle param constraints
137- $ this ->constraints ?->addConstraints($ body );
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+ }
138166
139167 // handle arrays
140168 $ this ->addArrayItemsIfArray ($ body );
0 commit comments