Skip to content

Commit 13f44b1

Browse files
Merge pull request #92 from rawahamid/patch-3
Add request custom methods
2 parents 9e2e6cd + 4b821bf commit 13f44b1

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/LaravelRequestDocs.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function appendRequestRules(array $controllersInfo)
119119
$reflectionMethod = new ReflectionMethod($controller, $method);
120120
$params = $reflectionMethod->getParameters();
121121
$customRules = $this->customParamsDocComment($reflectionMethod->getDocComment());
122+
$controllersInfo[$index]['rules'] = [];
122123

123124
foreach ($params as $param) {
124125
if (!$param->getType()) {
@@ -133,18 +134,16 @@ public function appendRequestRules(array $controllersInfo)
133134
//throw $th;
134135
}
135136

136-
if ($requestClass && method_exists($requestClass, 'rules')) {
137-
try {
138-
$controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules());
139-
} catch (Throwable $e) {
140-
// disabled. This only works when the rules are defined as 'required|integer' and that too in single line
141-
// doesn't work well when the same rule is defined as array ['required', 'integer'] or in multiple lines such as
142-
// If your rules are not populated using this library, then fix your rule to only throw validation errors and not throw exceptions
143-
// such as 404, 500 inside the request class.
144-
$controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);
137+
foreach (config('request-docs.request_methods') as $requestMethod) {
138+
if ($requestClass && method_exists($requestClass, $requestMethod)) {
139+
try {
140+
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->flattenRules($requestClass->$requestMethod()));
145141

146-
if (config('request-docs.debug')) {
147-
throw $e;
142+
} catch (Throwable $e) {
143+
$controllersInfo[$index]['rules'] = array_merge($controllersInfo[$index]['rules'], $this->rulesByRegex($requestClassName, $requestMethod));
144+
if (config('request-docs.debug')) {
145+
throw $e;
146+
}
148147
}
149148
}
150149
}
@@ -211,9 +210,9 @@ public function flattenRules($mixedRules)
211210
return $rules;
212211
}
213212

214-
public function rulesByRegex($requestClassName)
213+
public function rulesByRegex($requestClassName, $methodName)
215214
{
216-
$data = new ReflectionMethod($requestClassName, 'rules');
215+
$data = new ReflectionMethod($requestClassName, $methodName);
217216
$lines = file($data->getFileName());
218217
$rules = [];
219218

0 commit comments

Comments
 (0)