Skip to content

Commit 92b6751

Browse files
committed
parameter generators
1 parent 4dbf4fd commit 92b6751

File tree

7 files changed

+218
-52
lines changed

7 files changed

+218
-52
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"laravel/framework": ">=5.0"
1313
},
1414
"autoload": {
15+
"files": [
16+
"src/helpers.php"
17+
],
1518
"psr-4": {
1619
"Mtrajano\\LaravelSwagger\\": "src/"
1720
}

src/Generator.php

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class Generator
1313

1414
protected $docs;
1515

16+
protected $uri;
17+
18+
protected $originalUri;
19+
20+
protected $method;
21+
22+
protected $action;
23+
1624
public function __construct($config)
1725
{
1826
$this->config = $config;
@@ -23,16 +31,19 @@ public function generate()
2331
$this->docs = $this->getBaseInfo();
2432

2533
foreach ($this->getAppRoutes() as $route) {
26-
$uri = $this->getRouteUri($route);
34+
$this->originalUri = $uri = $this->getRouteUri($route);
35+
$this->uri = strip_optional_char($uri);
36+
$this->action = $route->getAction('uses');
2737
$methods = $route->methods();
28-
$action = $route->getAction('uses');
2938

30-
if (!isset($this->docs['paths'][$uri])) {
31-
$this->docs['paths'][$uri] = [];
39+
if (!isset($this->docs['paths'][$this->uri])) {
40+
$this->docs['paths'][$this->uri] = [];
3241
}
3342

3443
foreach ($methods as $method) {
35-
$this->generatePath($uri, $method, $action);
44+
$this->method = strtolower($method);
45+
46+
$this->generatePath();
3647
}
3748
}
3849

@@ -70,28 +81,27 @@ protected function getRouteUri(Route $route)
7081
return $uri;
7182
}
7283

73-
protected function generatePath($uri, $method, $action)
84+
protected function generatePath()
7485
{
75-
$method = strtolower($method);
76-
$this->docs['paths'][$uri][$method] = [
77-
'description' => strtoupper($method) . ' ' . $uri,
86+
$methodDescription = strtoupper($this->method);
87+
88+
$this->docs['paths'][$this->uri][$this->method] = [
89+
'description' => "$methodDescription {$this->uri}",
7890
'responses' => [
7991
'200' => [
8092
'description' => 'OK'
8193
]
8294
],
8395
];
8496

85-
if ($rules = $this->getFormRules($action)) {
86-
$this->docs['paths'][$uri][$method]['parameters'] = $this->getActionParameters($method, $rules);
87-
}
97+
$this->addActionParameters();
8898
}
8999

90-
protected function getFormRules($action)
100+
protected function getFormRules()
91101
{
92-
if (!is_string($action)) return false;
102+
if (!is_string($this->action)) return false;
93103

94-
$parsedAction = Str::parseCallback($action);
104+
$parsedAction = Str::parseCallback($this->action);
95105

96106
$parameters = (new ReflectionMethod($parsedAction[0], $parsedAction[1]))->getParameters();
97107

@@ -104,50 +114,33 @@ protected function getFormRules($action)
104114
}
105115
}
106116

107-
protected function getActionParameters($method, $rules)
117+
protected function addActionParameters()
108118
{
109-
$params = [];
110-
111-
foreach ($rules as $param => $rule) {
112-
$paramRules = explode('|', $rule);
113-
114-
$params[] = [
115-
'in' => $this->getParamLocation($method),
116-
'name' => $param,
117-
'type' => $this->getParamType($paramRules),
118-
'required' => $this->isParamRequired($paramRules),
119-
'description' => '',
120-
];
121-
}
119+
$rules = $this->getFormRules() ?: [];
122120

123-
return $params;
124-
}
121+
$parameters = (new Parameters\PathParameterGenerator($this->method, $this->originalUri, $rules))->getParameters();
125122

126-
protected function getParamLocation($method)
127-
{
128-
return in_array($method, ['get', 'head', 'delete']) ?
129-
'query':
130-
'body';
131-
}
123+
if (!empty($rules)) {
124+
$parameterGenerator = $this->getParameterGenerator($rules);
132125

133-
protected function getParamType(array $paramRules)
134-
{
135-
if (in_array('integer', $paramRules)) {
136-
return 'integer';
137-
} else if (in_array('numeric', $paramRules)) {
138-
return 'number';
139-
} else if (in_array('boolean', $paramRules)) {
140-
return 'boolean';
141-
} else if (in_array('array', $paramRules)) {
142-
return 'array';
143-
} else {
144-
//date, ip, email, etc..
145-
return 'string';
126+
$parameters = array_merge($parameters, $parameterGenerator->getParameters());
127+
128+
}
129+
130+
if (!empty($parameters)) {
131+
$this->docs['paths'][$this->uri][$this->method]['parameters'] = $parameters;
146132
}
147133
}
148134

149-
protected function isParamRequired(array $paramRules)
135+
protected function getParameterGenerator($rules)
150136
{
151-
return in_array('required', $paramRules);
137+
switch($this->method) {
138+
case 'post':
139+
case 'put':
140+
case 'patch':
141+
return new Parameters\BodyParameterGenerator($this->method, $this->originalUri, $rules);
142+
default:
143+
return new Parameters\QueryParameterGenerator($this->method, $this->originalUri, $rules);
144+
}
152145
}
153146
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Parameters;
4+
5+
class BodyParameterGenerator extends ParameterGenerator
6+
{
7+
public function getParameters()
8+
{
9+
$required = [];
10+
$properties = [];
11+
12+
$params = [
13+
'in' => $this->getParamLocation(),
14+
'name' => 'body',
15+
'description' => '',
16+
'schema' => [
17+
'type' => 'object',
18+
],
19+
];
20+
21+
foreach ($this->rules as $param => $rule) {
22+
$paramRules = explode('|', $rule);
23+
24+
if ($this->isParamRequired($paramRules)) {
25+
$required[] = $param;
26+
}
27+
28+
$properties[$param] = [
29+
'type' => $this->getParamType($paramRules)
30+
];
31+
}
32+
33+
if (!empty($required)) {
34+
$params['schema']['required'] = $required;
35+
}
36+
37+
$params['schema']['properties'] = $properties;
38+
39+
return [$params];
40+
}
41+
42+
public function getParamLocation()
43+
{
44+
return 'body';
45+
}
46+
}

src/Parameters/ParameterGenerator.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Parameters;
4+
5+
abstract class ParameterGenerator
6+
{
7+
protected $method;
8+
9+
protected $uri;
10+
11+
protected $rules;
12+
13+
public function __construct($method, $uri, array $rules)
14+
{
15+
$this->method = $method;
16+
$this->uri = $uri;
17+
$this->rules = $rules;
18+
}
19+
20+
abstract public function getParameters();
21+
22+
abstract public function getParamLocation();
23+
24+
protected function getParamType(array $paramRules)
25+
{
26+
if (in_array('integer', $paramRules)) {
27+
return 'integer';
28+
} else if (in_array('numeric', $paramRules)) {
29+
return 'number';
30+
} else if (in_array('boolean', $paramRules)) {
31+
return 'boolean';
32+
} else if (in_array('array', $paramRules)) {
33+
return 'array';
34+
} else {
35+
//date, ip, email, etc..
36+
return 'string';
37+
}
38+
}
39+
40+
protected function isParamRequired(array $paramRules)
41+
{
42+
return in_array('required', $paramRules);
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Parameters;
4+
5+
class PathParameterGenerator extends ParameterGenerator
6+
{
7+
public function getParameters()
8+
{
9+
$params = [];
10+
$pathVariables = $this->getAllVariablesFromUri();
11+
12+
foreach ($pathVariables as $variable) {
13+
$params[] = [
14+
'in' => $this->getParamLocation(),
15+
'name' => strip_optional_char($variable),
16+
'type' => 'integer', //best guess for a variable in the path
17+
'required' => $this->isPathVariableRequired($variable),
18+
'description' => '',
19+
];
20+
}
21+
22+
return $params;
23+
}
24+
25+
private function getAllVariablesFromUri()
26+
{
27+
preg_match_all('/{(\w+\??)}/', $this->uri, $pathVariables);
28+
29+
return $pathVariables[1];
30+
}
31+
32+
public function getParamLocation()
33+
{
34+
return 'path';
35+
}
36+
37+
private function isPathVariableRequired($pathVariable)
38+
{
39+
return !str_contains($pathVariable, '?');
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Parameters;
4+
5+
class QueryParameterGenerator extends ParameterGenerator
6+
{
7+
public function getParameters()
8+
{
9+
$params = [];
10+
11+
foreach ($this->rules as $param => $rule) {
12+
$paramRules = explode('|', $rule);
13+
14+
$params[] = [
15+
'in' => $this->getParamLocation(),
16+
'name' => $param,
17+
'type' => $this->getParamType($paramRules),
18+
'required' => $this->isParamRequired($paramRules),
19+
'description' => '',
20+
];
21+
}
22+
23+
return $params;
24+
}
25+
26+
public function getParamLocation()
27+
{
28+
return 'query';
29+
}
30+
}

src/helpers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
if (! function_exists('strip_optional_char')) {
4+
5+
function strip_optional_char($uri)
6+
{
7+
return str_replace('?', '', $uri);
8+
}
9+
}

0 commit comments

Comments
 (0)