20
20
class GenerateRequests extends ClassGenerator {
21
21
22
22
/**
23
- * @param string $file_path
24
- * @param bool $more_specificity
25
23
* @throws InvalidArgumentException
26
24
*/
27
25
public function generate (string $ file_path , bool $ more_specificity = false ): void {
28
26
$ api = Yaml::parseFile ($ file_path );
29
27
$ base_uri = self ::stringNotEndWith ($ api ['basePath ' ], '/ ' );
30
28
foreach ($ api ['paths ' ] as $ path => $ path_details ){
29
+ $ path_no_params = $ adapted_path = $ this ->pathNoParams ($ path );
31
30
if ($ more_specificity ){
32
31
$ adapted_path = $ this ->pathWithParamsNoPlaceholders ($ path );
33
32
}
34
- else {
35
- $ adapted_path = $ this ->pathNoParams ($ path );
36
- }
37
33
$ path_camel = $ this ->pathToCamelCase ($ adapted_path );
38
34
foreach ($ path_details as $ method => $ method_details ){
39
35
$ class_name = ucfirst ($ method ).$ path_camel ;
40
36
$ class = new ClassType ($ class_name );
41
37
$ class ->setExtends (self ::REQUEST_CLASS_NAME );
42
38
$ class ->addComment ($ method_details ['summary ' ]);
43
39
$ class ->addConstant ('METHOD ' , strtoupper ($ method ));
44
-
45
- if ($ base_uri ){
46
- $ path = self ::stringNotBeginWith ($ path , '/ ' );
47
- $ uri = "{$ base_uri }/ {$ path }" ;
48
- }
49
- else {
50
- $ uri = $ path ;
51
- }
52
- $ class ->addConstant ('URI ' , $ uri );
40
+ $ class ->addConstant ('URI ' , $ base_uri ? "$ base_uri/ $ path_no_params " : $ path_no_params );
53
41
54
42
$ this ->handleParams ($ method_details , $ class );
55
43
@@ -134,6 +122,7 @@ private function handlePathParams(array $path_params, ClassType $class): void{
134
122
$ class ->addProperty ('path_params ' )
135
123
->setStatic ()
136
124
->setValue ($ param_names )
125
+ ->setType ('array ' )
137
126
->setVisibility ('protected ' );
138
127
}
139
128
}
@@ -177,12 +166,14 @@ private function handleQueryParams(array $query_params, ClassType $class): void{
177
166
catch (InvalidArgumentException $ e ) {
178
167
$ query_params_property = $ class ->addProperty ('query_params ' )
179
168
->setStatic ()
169
+ ->setType ('array ' )
180
170
->setVisibility ('protected ' );
181
171
$ query_params_array = [];
182
172
}
183
173
$ value = array_merge ($ query_params_array , $ param_names );
184
174
$ query_params_property ->setStatic ()
185
175
->setValue ($ value )
176
+ ->setType ('array ' )
186
177
->setVisibility ('protected ' );
187
178
}
188
179
}
@@ -253,6 +244,7 @@ protected function handleResponse(array $method_details, ClassType $class): void
253
244
254
245
$ model_ns = $ this ->namespaceModel ();
255
246
$ has_2xx = false ;
247
+ $ list_types = [];
256
248
foreach ($ method_details ['responses ' ] as $ code_string => $ method ){
257
249
$ get_type_from = isset ($ method ['$ref ' ]) ? $ method : ($ method ['schema ' ] ?? null );
258
250
if (!is_null ($ get_type_from )){
@@ -263,7 +255,9 @@ protected function handleResponse(array $method_details, ClassType $class): void
263
255
$ type = $ this ->typeFromRef ($ get_type_from );
264
256
}
265
257
if ($ this ->notScalarType ($ type )){
266
- $ type = "\\$ model_ns \\$ type::class " ;
258
+ $ class_string = "\\$ model_ns \\$ type " ;
259
+ $ type = "$ class_string::class " ;
260
+ $ list_types [] = $ class_string ;
267
261
}
268
262
else {
269
263
$ type = "'' " ;
@@ -284,13 +278,20 @@ protected function handleResponse(array $method_details, ClassType $class): void
284
278
throw new InvalidArgumentException ('Response blocks must contain at least one positive response type ' );
285
279
}
286
280
281
+ $ list_types = array_unique ($ list_types );
282
+ if (!$ list_types ){
283
+ $ list_types = ['SwaggerModel ' ];
284
+ }
285
+ foreach ($ list_types as $ list_type ){
286
+ $ list_types [] = "list< $ list_type> " ;
287
+ }
288
+
287
289
$ response_models = implode (", \n\t" , $ response_models );
288
290
$ response_models = "[ \n\t$ response_models \n] " ;
289
291
$ class ->addMethod ('sendWith ' )
290
292
->addBody ("return \$client->make( \$this, $ response_models); " )
291
- ->addComment ('@param SwaggerClient $client ' )
292
- ->addComment ('@return SwaggerModel ' )
293
+ ->addComment ('@return ' .implode ('| ' , $ list_types ))
293
294
->addParameter ('client ' )
294
- ->setTypeHint ('SwaggerClient ' );
295
+ ->setType ('SwaggerClient ' );
295
296
}
296
297
}
0 commit comments