From c0337b593c07b3ddac5bb785e4e67dd6ea100a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Simon=20Maria=20M=C3=B6llers?= Date: Thu, 6 Jul 2017 11:47:37 +0200 Subject: [PATCH] Parse Symfony params #5985 --- .../resources/php-symfony/Controller.mustache | 284 +++++++++++++----- .../php-symfony/api_controller.mustache | 8 +- 2 files changed, 209 insertions(+), 83 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php-symfony/Controller.mustache b/modules/swagger-codegen/src/main/resources/php-symfony/Controller.mustache index e5561f01345a..af937c7d99fe 100644 --- a/modules/swagger-codegen/src/main/resources/php-symfony/Controller.mustache +++ b/modules/swagger-codegen/src/main/resources/php-symfony/Controller.mustache @@ -34,83 +34,209 @@ use Symfony\Component\HttpKernel\Exception\HttpException; class Controller extends BaseController { - /** - * This will return a response with code 400. Usage example: - * - * return $this->createBadRequestResponse('Unable to access this page!'); - * - * @param string $message A message - * - * @return Response - */ - public function createBadRequestResponse($message = 'Bad Request.') - { - return new Response($message, 400); - } - - /** - * This will return an error response. Usage example: - * - * return $this->createErrorResponse(new UnauthorizedHttpException()); - * - * @param HttpException $exception An HTTP exception - * - * @return Response - */ - public function createErrorResponse(HttpException $exception) - { - $statusCode = $exception->getStatusCode(); - $headers = array_merge($exception->getHeaders(), ['Content-Type' => 'application/json']); - - $json = $this->exceptionToArray($exception); - $json["statusCode"] = $statusCode; - - return new Response(json_encode($json, 15, 512), $statusCode, $headers); - } - - /** - * Serializes data to a given type format. - * - * @param mixed $data The data to serialize. - * @param string $class The source data class. - * @param string $format The target serialization format. - * @return string A serialized data string. - */ - public function serialize($data, $format) - { - return $this->get('{{bundleAlias}}.model.model_serializer')->serialize($data, $format); - } - - /** - * Deserializes data from a given type format. - * - * @param string $data The data to deserialize. - * @param string $class The target data class. - * @param string $format The source serialization format. - * @return mixed A deserialized data. - */ - public function deserialize($data, $class, $format) - { - return $this->get('{{bundleAlias}}.model.model_serializer')->deserialize($data, $class, $format); - } - - /** - * Converts an exception to a serializable array. - * - * @param \Exception|null $exception - * - * @return array - */ - private function exceptionToArray(\Exception $exception = null) - { - if (null === $exception) { - return null; - } - - return [ - "message" => $exception->getMessage(), - "type" => get_class($exception), - "previous" => $this->exceptionToArray($exception->getPrevious()), - ]; - } + /** + * This will return a response with code 400. Usage example: + * return $this->createBadRequestResponse('Unable to access this page!'); + * + * @param string $message A message + * + * @return Response + */ + public function createBadRequestResponse($message = 'Bad Request.') + { + return new Response($message, 400); + } + + /** + * This will return an error response. Usage example: + * return $this->createErrorResponse(new UnauthorizedHttpException()); + * + * @param HttpException $exception An HTTP exception + * + * @return Response + */ + public function createErrorResponse(HttpException $exception) + { + $statusCode = $exception->getStatusCode(); + $headers = array_merge($exception->getHeaders(), ['Content-Type' => 'application/json']); + + $json = $this->exceptionToArray($exception); + $json['statusCode'] = $statusCode; + + return new Response(json_encode($json, 15, 512), $statusCode, $headers); + } + + /** + * Serializes data to a given type format. + * + * @param mixed $data The data to serialize. + * @param string $class The source data class. + * @param string $format The target serialization format. + * + * @return string A serialized data string. + */ + public function serialize($data, $format) + { + return $this->get('{{bundleAlias}}.model.model_serializer')->serialize($data, $format); + } + + /** + * Deserializes data from a given type format. + * + * @param string $data The data to deserialize. + * @param string $class The target data class. + * @param string $format The source serialization format. + * + * @return mixed A deserialized data. + */ + public function deserialize($data, $class, $format) + { + return $this->get('{{bundleAlias}}.model.model_serializer')->deserialize($data, $class, $format); + } + + /** + * Decodes a string value. + * + * @param string $string The string value to decode. + * @param string $dataType The data type of the parameter. + * + * @return mixed The decoded value. + */ + public function fromString($string, $dataType) + { + if ($dataType === 'integer' || $dataType === 'number') { + return $this->toNumber($string); + } + if ($dataType === 'bool') { + return $this->toBoolean($string); + } + if ($dataType === '\DateTime') { + return $this->toDateTime($string); + } + + return $string; + } + + /** + * Decodes a header value. + * + * @param string $header The header value to decode. + * @param string $dataType The data type of the parameter. + * + * @return mixed The decoded value. + */ + public function fromHeader($header, $dataType) + { + return $this->fromString($header, $dataType); + } + + /** + * Decodes a query value. + * + * @param string $query The query value to decode. + * @param string $dataType The data type of the parameter. + * + * @return mixed The decoded value. + */ + public function fromQuery($query, $dataType) + { + return $this->fromString($query, $dataType); + } + + /** + * Decodes a path value. + * + * @param string $path The path value to decode. + * @param string $dataType The data type of the parameter. + * + * @return mixed The decoded value. + */ + public function fromPath($path, $dataType) + { + return $this->fromString($path, $dataType); + } + + /** + * Decodes a form value. + * + * @param string $form The form value to decode. + * @param string $dataType The data type of the parameter. + * + * @return mixed The decoded value. + */ + public function fromForm($form, $dataType) + { + return $this->fromString($form, $dataType); + } + + /** + * Decoded a string to a number. + * + * @param string $string The string to decode. + * + * @return number|null A decoded number, or null, if not a valid string. + */ + private function toNumber($string) + { + if (is_numeric($string)) { + return $string + 0; + } + + return null; + } + + /** + * Decoded a string to a boolean. + * + * @param string $string The string to decode. + * + * @return boolean|null A decoded boolean, or null, if not a valid string. + */ + private function toBoolean($string) + { + if ($string === 'true') { + return true; + } + if ($string === 'false') { + return false; + } + + return null; + } + + /** + * Decoded a string to a date time. + * + * @param string $string The string to decode. + * + * @return \DateTime|null A decoded date time, or null, if not a valid string. + */ + private function toDateTime($string) + { + if ($dateTime = date_create($string)) { + return $dateTime; + } + + return null; + } + + /** + * Converts an exception to a serializable array. + * + * @param \Exception|null $exception + * + * @return array + */ + private function exceptionToArray(\Exception $exception = null) + { + if (null === $exception) { + return null; + } + + return [ + 'message' => $exception->getMessage(), + 'type' => get_class($exception), + 'previous' => $this->exceptionToArray($exception->getPrevious()), + ]; + } } diff --git a/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache b/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache index e28cb5908e1a..19b65101ac12 100644 --- a/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache +++ b/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache @@ -57,15 +57,15 @@ class {{controllerName}} extends Controller { {{#queryParams}} // Handle query params - ${{paramName}} = $request->query->get('{{paramName}}'); + ${{paramName}} = $this->fromQuery($request->query->get('{{paramName}}'), '{{dataType}}'); {{/queryParams}} {{#headerParams}} // Handle header params - ${{paramName}} = $request->headers->get('{{paramName}}'); + ${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}'); {{/headerParams}} {{#pathParams}} // Handle path params - ${{paramName}} = $request->attributes->get('{{paramName}}'); + ${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}'); {{/pathParams}} {{#formParams}} {{#isFile}} @@ -74,7 +74,7 @@ class {{controllerName}} extends Controller {{/isFile}} {{^isFile}} // Handle form params - ${{paramName}} = $request->request->get('{{paramName}}'); + ${{paramName}} = $this->fromForm($request->request->get('{{paramName}}'), '{{dataType}}'); {{/isFile}} {{/formParams}} {{#bodyParams}}