diff --git a/src/GenerateRequests.php b/src/GenerateRequests.php index efe9a79..3fe4039 100644 --- a/src/GenerateRequests.php +++ b/src/GenerateRequests.php @@ -20,20 +20,16 @@ class GenerateRequests extends ClassGenerator { /** - * @param string $file_path - * @param bool $more_specificity * @throws InvalidArgumentException */ public function generate(string $file_path, bool $more_specificity = false): void{ $api = Yaml::parseFile($file_path); $base_uri = self::stringNotEndWith($api['basePath'], '/'); foreach ($api['paths'] as $path => $path_details){ + $path_no_params = $adapted_path = $this->pathNoParams($path); if ($more_specificity){ $adapted_path = $this->pathWithParamsNoPlaceholders($path); } - else { - $adapted_path = $this->pathNoParams($path); - } $path_camel = $this->pathToCamelCase($adapted_path); foreach ($path_details as $method => $method_details){ $class_name = ucfirst($method).$path_camel; @@ -41,15 +37,7 @@ public function generate(string $file_path, bool $more_specificity = false): voi $class->setExtends(self::REQUEST_CLASS_NAME); $class->addComment($method_details['summary']); $class->addConstant('METHOD', strtoupper($method)); - - if ($base_uri){ - $path = self::stringNotBeginWith($path, '/'); - $uri = "{$base_uri}/{$path}"; - } - else { - $uri = $path; - } - $class->addConstant('URI', $uri); + $class->addConstant('URI', $base_uri ? "$base_uri/$path_no_params" : $path_no_params); $this->handleParams($method_details, $class); @@ -134,6 +122,7 @@ private function handlePathParams(array $path_params, ClassType $class): void{ $class->addProperty('path_params') ->setStatic() ->setValue($param_names) + ->setType('array') ->setVisibility('protected'); } } @@ -177,12 +166,14 @@ private function handleQueryParams(array $query_params, ClassType $class): void{ catch (InvalidArgumentException $e) { $query_params_property = $class->addProperty('query_params') ->setStatic() + ->setType('array') ->setVisibility('protected'); $query_params_array = []; } $value = array_merge($query_params_array, $param_names); $query_params_property->setStatic() ->setValue($value) + ->setType('array') ->setVisibility('protected'); } } @@ -253,6 +244,7 @@ protected function handleResponse(array $method_details, ClassType $class): void $model_ns = $this->namespaceModel(); $has_2xx = false; + $list_types = []; foreach ($method_details['responses'] as $code_string => $method){ $get_type_from = isset($method['$ref']) ? $method : ($method['schema'] ?? null); if (!is_null($get_type_from)){ @@ -263,7 +255,9 @@ protected function handleResponse(array $method_details, ClassType $class): void $type = $this->typeFromRef($get_type_from); } if ($this->notScalarType($type)){ - $type = "\\$model_ns\\$type::class"; + $class_string = "\\$model_ns\\$type"; + $type = "$class_string::class"; + $list_types[] = $class_string; } else { $type = "''"; @@ -284,13 +278,20 @@ protected function handleResponse(array $method_details, ClassType $class): void throw new InvalidArgumentException('Response blocks must contain at least one positive response type'); } + $list_types = array_unique($list_types); + if (!$list_types){ + $list_types = ['SwaggerModel']; + } + foreach ($list_types as $list_type){ + $list_types[] = "list<$list_type>"; + } + $response_models = implode(",\n\t", $response_models); $response_models = "[\n\t$response_models\n]"; $class->addMethod('sendWith') ->addBody("return \$client->make(\$this, $response_models);") - ->addComment('@param SwaggerClient $client') - ->addComment('@return SwaggerModel') + ->addComment('@return '.implode('|', $list_types)) ->addParameter('client') - ->setTypeHint('SwaggerClient'); + ->setType('SwaggerClient'); } } diff --git a/src/SwaggerClient.php b/src/SwaggerClient.php index 02f55b2..47a7b2a 100644 --- a/src/SwaggerClient.php +++ b/src/SwaggerClient.php @@ -9,10 +9,10 @@ interface SwaggerClient { /** * @template T of SwaggerModel * - * @param array> $response_models + * @param array|''> $response_models * @return T|list */ - public function make(SwaggerRequest $request, array $response_models); + public function make(SwaggerRequest $swagger, array $response_models); public function messageFromRequest(SwaggerRequest $swagger):RequestInterface; } diff --git a/src/SwaggerRequest.php b/src/SwaggerRequest.php index 75869c3..89a4c8f 100644 --- a/src/SwaggerRequest.php +++ b/src/SwaggerRequest.php @@ -51,10 +51,7 @@ public function getQuery(): array{ public function getPath(): string{ $query = []; foreach (static::$path_params as $param_name){ - if (!isset($this->{$param_name})){ - continue; - } - $query[] = $this->{$param_name}; + $query[] = $this->{$param_name} ?? ''; } if (count($query)>0){ return '/'.implode('/', $query);