| @@ 17-96 (lines=80) @@ | ||
| 14 | use ReflectionClass; |
|
| 15 | use ReflectionMethod; |
|
| 16 | ||
| 17 | class GetFromQueryParamTag extends Strategy |
|
| 18 | { |
|
| 19 | use ParamHelpers; |
|
| 20 | ||
| 21 | public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) |
|
| 22 | { |
|
| 23 | foreach ($method->getParameters() as $param) { |
|
| 24 | $paramType = $param->getType(); |
|
| 25 | if ($paramType === null) { |
|
| 26 | continue; |
|
| 27 | } |
|
| 28 | ||
| 29 | $parameterClassName = $paramType->getName(); |
|
| 30 | ||
| 31 | try { |
|
| 32 | $parameterClass = new ReflectionClass($parameterClassName); |
|
| 33 | } catch (\ReflectionException $e) { |
|
| 34 | continue; |
|
| 35 | } |
|
| 36 | ||
| 37 | // If there's a FormRequest, we check there for @queryParam tags. |
|
| 38 | if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class) |
|
| 39 | || class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) { |
|
| 40 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
|
| 41 | $queryParametersFromDocBlock = $this->getQueryParametersFromDocBlock($formRequestDocBlock->getTags()); |
|
| 42 | ||
| 43 | if (count($queryParametersFromDocBlock)) { |
|
| 44 | return $queryParametersFromDocBlock; |
|
| 45 | } |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| 49 | /** @var DocBlock $methodDocBlock */ |
|
| 50 | $methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; |
|
| 51 | ||
| 52 | return $this->getQueryParametersFromDocBlock($methodDocBlock->getTags()); |
|
| 53 | } |
|
| 54 | ||
| 55 | private function getQueryParametersFromDocBlock($tags) |
|
| 56 | { |
|
| 57 | $parameters = collect($tags) |
|
| 58 | ->filter(function ($tag) { |
|
| 59 | return $tag instanceof Tag && $tag->getName() === 'queryParam'; |
|
| 60 | }) |
|
| 61 | ->mapWithKeys(function (Tag $tag) { |
|
| 62 | // Format: |
|
| 63 | // @queryParam <name> <"required" (optional)> <description> |
|
| 64 | // Examples: |
|
| 65 | // @queryParam text string required The text. |
|
| 66 | // @queryParam user_id The ID of the user. |
|
| 67 | preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
|
| 68 | $content = preg_replace('/\s?No-example.?/', '', $content); |
|
| 69 | if (empty($content)) { |
|
| 70 | // this means only name was supplied |
|
| 71 | list($name) = preg_split('/\s+/', $tag->getContent()); |
|
| 72 | $required = false; |
|
| 73 | $description = ''; |
|
| 74 | } else { |
|
| 75 | list($_, $name, $required, $description) = $content; |
|
| 76 | $description = trim($description); |
|
| 77 | if ($description == 'required' && empty(trim($required))) { |
|
| 78 | $required = $description; |
|
| 79 | $description = ''; |
|
| 80 | } |
|
| 81 | $required = trim($required) == 'required' ? true : false; |
|
| 82 | } |
|
| 83 | ||
| 84 | list($description, $value) = $this->parseParamDescription($description, 'string'); |
|
| 85 | if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) { |
|
| 86 | $value = Str::contains($description, ['number', 'count', 'page']) |
|
| 87 | ? $this->generateDummyValue('integer') |
|
| 88 | : $this->generateDummyValue('string'); |
|
| 89 | } |
|
| 90 | ||
| 91 | return [$name => compact('description', 'required', 'value')]; |
|
| 92 | })->toArray(); |
|
| 93 | ||
| 94 | return $parameters; |
|
| 95 | } |
|
| 96 | } |
|
| 97 | ||
| @@ 17-96 (lines=80) @@ | ||
| 14 | use ReflectionClass; |
|
| 15 | use ReflectionMethod; |
|
| 16 | ||
| 17 | class GetFromUrlParamTag extends Strategy |
|
| 18 | { |
|
| 19 | use ParamHelpers; |
|
| 20 | ||
| 21 | public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) |
|
| 22 | { |
|
| 23 | foreach ($method->getParameters() as $param) { |
|
| 24 | $paramType = $param->getType(); |
|
| 25 | if ($paramType === null) { |
|
| 26 | continue; |
|
| 27 | } |
|
| 28 | ||
| 29 | $parameterClassName = $paramType->getName(); |
|
| 30 | ||
| 31 | try { |
|
| 32 | $parameterClass = new ReflectionClass($parameterClassName); |
|
| 33 | } catch (\ReflectionException $e) { |
|
| 34 | continue; |
|
| 35 | } |
|
| 36 | ||
| 37 | // If there's a FormRequest, we check there for @urlParam tags. |
|
| 38 | if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class) |
|
| 39 | || class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) { |
|
| 40 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
|
| 41 | $queryParametersFromDocBlock = $this->getUrlParametersFromDocBlock($formRequestDocBlock->getTags()); |
|
| 42 | ||
| 43 | if (count($queryParametersFromDocBlock)) { |
|
| 44 | return $queryParametersFromDocBlock; |
|
| 45 | } |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| 49 | /** @var DocBlock $methodDocBlock */ |
|
| 50 | $methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; |
|
| 51 | ||
| 52 | return $this->getUrlParametersFromDocBlock($methodDocBlock->getTags()); |
|
| 53 | } |
|
| 54 | ||
| 55 | private function getUrlParametersFromDocBlock($tags) |
|
| 56 | { |
|
| 57 | $parameters = collect($tags) |
|
| 58 | ->filter(function ($tag) { |
|
| 59 | return $tag instanceof Tag && $tag->getName() === 'urlParam'; |
|
| 60 | }) |
|
| 61 | ->mapWithKeys(function (Tag $tag) { |
|
| 62 | // Format: |
|
| 63 | // @urlParam <name> <"required" (optional)> <description> |
|
| 64 | // Examples: |
|
| 65 | // @urlParam id string required The id of the post. |
|
| 66 | // @urlParam user_id The ID of the user. |
|
| 67 | preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
|
| 68 | $content = preg_replace('/\s?No-example.?/', '', $content); |
|
| 69 | if (empty($content)) { |
|
| 70 | // this means only name was supplied |
|
| 71 | list($name) = preg_split('/\s+/', $tag->getContent()); |
|
| 72 | $required = false; |
|
| 73 | $description = ''; |
|
| 74 | } else { |
|
| 75 | list($_, $name, $required, $description) = $content; |
|
| 76 | $description = trim($description); |
|
| 77 | if ($description == 'required' && empty(trim($required))) { |
|
| 78 | $required = $description; |
|
| 79 | $description = ''; |
|
| 80 | } |
|
| 81 | $required = trim($required) == 'required' ? true : false; |
|
| 82 | } |
|
| 83 | ||
| 84 | list($description, $value) = $this->parseParamDescription($description, 'string'); |
|
| 85 | if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) { |
|
| 86 | $value = Str::contains($description, ['number', 'count', 'page']) |
|
| 87 | ? $this->generateDummyValue('integer') |
|
| 88 | : $this->generateDummyValue('string'); |
|
| 89 | } |
|
| 90 | ||
| 91 | return [$name => compact('description', 'required', 'value')]; |
|
| 92 | })->toArray(); |
|
| 93 | ||
| 94 | return $parameters; |
|
| 95 | } |
|
| 96 | } |
|
| 97 | ||