From e0afdc1ce73b24babcddc6b54f7b32632b46f285 Mon Sep 17 00:00:00 2001 From: Rawa Hamid Date: Tue, 11 Oct 2022 10:22:19 +0300 Subject: [PATCH 1/2] Add request custom methods --- src/LaravelRequestDocs.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/LaravelRequestDocs.php b/src/LaravelRequestDocs.php index 60c8d34..41f7563 100644 --- a/src/LaravelRequestDocs.php +++ b/src/LaravelRequestDocs.php @@ -119,6 +119,7 @@ public function appendRequestRules(array $controllersInfo) $reflectionMethod = new ReflectionMethod($controller, $method); $params = $reflectionMethod->getParameters(); $customRules = $this->customParamsDocComment($reflectionMethod->getDocComment()); + $controllersInfo[$index]['rules'] = []; foreach ($params as $param) { if (!$param->getType()) { @@ -133,18 +134,16 @@ public function appendRequestRules(array $controllersInfo) //throw $th; } - if ($requestClass && method_exists($requestClass, 'rules')) { - try { - $controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules()); - } catch (Throwable $e) { - // disabled. This only works when the rules are defined as 'required|integer' and that too in single line - // doesn't work well when the same rule is defined as array ['required', 'integer'] or in multiple lines such as - // If your rules are not populated using this library, then fix your rule to only throw validation errors and not throw exceptions - // such as 404, 500 inside the request class. - $controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName); + foreach (config('request-docs.request_methods') as $rquestMethod) { + if ($requestClass && method_exists($requestClass, $rquestMethod)) { + try { + $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$rquestMethod())); - if (config('request-docs.debug')) { - throw $e; + } catch (Throwable $e) { + $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $rquestMethod)); + if (config('request-docs.debug')) { + throw $e; + } } } } @@ -211,9 +210,9 @@ public function flattenRules($mixedRules) return $rules; } - public function rulesByRegex($requestClassName) + public function rulesByRegex($requestClassName, $methodName) { - $data = new ReflectionMethod($requestClassName, 'rules'); + $data = new ReflectionMethod($requestClassName, $methodName); $lines = file($data->getFileName()); $rules = []; From 4b821bfbb4b103d85e46e7fad4afafe041a2c0c4 Mon Sep 17 00:00:00 2001 From: Rawa Hamid Date: Tue, 11 Oct 2022 10:38:10 +0300 Subject: [PATCH 2/2] Spell check done --- src/LaravelRequestDocs.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LaravelRequestDocs.php b/src/LaravelRequestDocs.php index 41f7563..c469530 100644 --- a/src/LaravelRequestDocs.php +++ b/src/LaravelRequestDocs.php @@ -134,13 +134,13 @@ public function appendRequestRules(array $controllersInfo) //throw $th; } - foreach (config('request-docs.request_methods') as $rquestMethod) { - if ($requestClass && method_exists($requestClass, $rquestMethod)) { + foreach (config('request-docs.request_methods') as $requestMethod) { + if ($requestClass && method_exists($requestClass, $requestMethod)) { try { - $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$rquestMethod())); + $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$requestMethod())); } catch (Throwable $e) { - $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $rquestMethod)); + $controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $requestMethod)); if (config('request-docs.debug')) { throw $e; }