Skip to content

Commit 7b6a300

Browse files
Merge pull request #64 from Mdhesari/custom-params
Custom param rules and make lrd documents available for all route methods
2 parents a07cf65 + 953d703 commit 7b6a300

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ Example of using it in controller
119119
{
120120
```
121121

122+
# Custom Params
123+
124+
You write extra params with rules with @QAparam comment line
125+
126+
```php
127+
/**
128+
* @QAparam search string
129+
*/
130+
public function index(MyIndexRequest $request): Resource
131+
{
132+
```
133+
134+
```php
135+
/**
136+
* @QAparam search string nullable max:32
137+
*/
138+
public function index(MyIndexRequest $request): Resource
139+
{
140+
```
141+
122142
# Testing
123143

124144
```bash

src/LaravelRequestDocs.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function appendRequestRules(array $controllersInfo)
117117
$method = $controllerInfo['method'];
118118
$reflectionMethod = new ReflectionMethod($controller, $method);
119119
$params = $reflectionMethod->getParameters();
120+
$customRules = $this->customParamsDocComment($reflectionMethod->getDocComment());
120121

121122
foreach ($params as $param) {
122123
if (!$param->getType()) {
@@ -144,8 +145,14 @@ public function appendRequestRules(array $controllersInfo)
144145
throw $e;
145146
}
146147
}
147-
$controllersInfo[$index]['docBlock'] = $this->lrdDocComment($reflectionMethod->getDocComment());
148148
}
149+
150+
$controllersInfo[$index]['docBlock'] = $this->lrdDocComment($reflectionMethod->getDocComment());
151+
152+
$controllersInfo[$index]['rules'] = array_merge(
153+
$controllersInfo[$index]['rules'] ?? [],
154+
$customRules,
155+
);
149156
}
150157
}
151158
return $controllersInfo;
@@ -236,4 +243,23 @@ public function rulesByRegex($requestClassName)
236243

237244
return $rules;
238245
}
246+
247+
private function customParamsDocComment($docComment): array
248+
{
249+
$params = [];
250+
251+
foreach (explode("\n", $docComment) as $comment) {
252+
if ( Str::contains($comment, '@QAparam') ) {
253+
$comment = trim(Str::replace(['@QAparam', '*'], '', $comment));
254+
255+
$comment = explode(' ', $comment);
256+
257+
if (count($comment) > 0) {
258+
$params[$comment[0]] = array_values(array_filter($comment, fn($item) => $item != $comment[0]));
259+
}
260+
}
261+
}
262+
263+
return $params;
264+
}
239265
}

0 commit comments

Comments
 (0)