Skip to content

Commit a0aaef8

Browse files
authored
Merge pull request mpociot#700 from jantoniomartin/patch-array-params
fixes indentation problems in @lucianobargmann's PR for mpociot#637
2 parents b8eccbe + 398fe67 commit a0aaef8

File tree

8 files changed

+68
-36
lines changed

8 files changed

+68
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ docs/_build
1313
docs/make.bat
1414
tests/public/docs/
1515
tests/resources/**
16+
.phpunit.result.cache

.styleci.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
preset: laravel
1+
preset: psr12
22

3-
enabled:
4-
- phpdoc_order
5-
- phpdoc_separation
6-
- unalign_double_arrow
7-
8-
disabled:
9-
- short_list_syntax
10-
- simplified_null_return

resources/views/partials/example-requests/bash.blade.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
```bash
22
curl -X {{$route['methods'][0]}} \
3-
{{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['cleanQueryParameters']))?@foreach($route['cleanQueryParameters'] as $parameter => $value)
4-
{{ urlencode($parameter) }}={{ urlencode($value) }}@if(!$loop->last)&@endif
5-
@endforeach
6-
@endif" @if(count($route['headers']))\
3+
{{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['cleanQueryParameters']))?{!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsString($route['cleanQueryParameters']) !!}@endif" @if(count($route['headers']))\
74
@foreach($route['headers'] as $header => $value)
85
-H "{{$header}}: {{ addslashes($value) }}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
96
@endif

resources/views/partials/example-requests/javascript.blade.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
);
55
@if(count($route['cleanQueryParameters']))
66

7-
let params = {
8-
@foreach($route['cleanQueryParameters'] as $parameter => $value)
9-
"{{ $parameter }}": "{{ $value }}",
10-
@endforeach
11-
};
7+
let params = {!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsKeyValue($route['cleanQueryParameters'], "\"", ":", 4, "{}") !!};
128
Object.keys(params)
139
.forEach(key => url.searchParams.append(key, params[key]));
1410
@endif

resources/views/partials/example-requests/php.blade.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
'headers' => {!! \Mpociot\ApiDoc\Tools\Utils::printPhpValue($route['headers'], 8) !!},
1010
@endif
1111
@if(!empty($route['cleanQueryParameters']))
12-
'query' => [
13-
@foreach($route['cleanQueryParameters'] as $parameter => $value)
14-
'{{$parameter}}' => '{{$value}}',
15-
@endforeach
16-
],
12+
'query' => {!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsKeyValue($route['cleanQueryParameters'], "'", "=>", 12, "[]", 8) !!},
1713
@endif
1814
@if(!empty($route['cleanBodyParameters']))
1915
'json' => {!! \Mpociot\ApiDoc\Tools\Utils::printPhpValue($route['cleanBodyParameters'], 8) !!},

resources/views/partials/example-requests/python.blade.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
payload = {!! json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT) !!}
88
@endif
99
@if(count($route['cleanQueryParameters']))
10-
params = {
11-
@foreach($route['cleanQueryParameters'] as $parameter => $value)
12-
'{{ $parameter }}': '{{ $value }}'@if(!($loop->last)),
13-
@endif
14-
@endforeach
15-
16-
}
10+
params = {!! \Mpociot\ApiDoc\Tools\Utils::printQueryParamsAsKeyValue($route['cleanQueryParameters'], "'", ":", 2, "{}") !!}
1711
@endif
1812
@if(!empty($route['headers']))
1913
headers = {

src/Tools/Utils.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function getRouteClassAndMethodNames($routeOrAction)
5454
public static function replaceUrlParameterPlaceholdersWithValues(string $uri, array $urlParameters)
5555
{
5656
$matches = preg_match_all('/{.+?}/i', $uri, $parameterPaths);
57-
if (! $matches) {
57+
if (!$matches) {
5858
return $uri;
5959
}
6060

@@ -89,7 +89,8 @@ public static function dumpException(\Exception $e)
8989

9090
public static function deleteDirectoryAndContents($dir)
9191
{
92-
$adapter = new Local(realpath(__DIR__.'/../../'));
92+
$dir = ltrim($dir, '/');
93+
$adapter = new Local(realpath(__DIR__ . '/../../'));
9394
$fs = new Filesystem($adapter);
9495
$fs->deleteDir($dir);
9596
}
@@ -98,21 +99,76 @@ public static function deleteDirectoryAndContents($dir)
9899
* @param mixed $value
99100
* @param int $indentationLevel
100101
*
102+
* @return string
101103
* @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
102104
*
103-
* @return string
104105
*/
105-
public static function printPhpValue($value, $indentationLevel = 0)
106+
public static function printPhpValue($value, int $indentationLevel = 0): string
106107
{
107108
$output = VarExporter::export($value);
108109
// Padding with x spaces so they align
109110
$split = explode("\n", $output);
110111
$result = '';
111112
$padWith = str_repeat(' ', $indentationLevel);
112113
foreach ($split as $index => $line) {
113-
$result .= ($index == 0 ? '' : "\n$padWith").$line;
114+
$result .= ($index == 0 ? '' : "\n$padWith") . $line;
114115
}
115116

116117
return $result;
117118
}
119+
120+
public static function printQueryParamsAsString(array $cleanQueryParams): string
121+
{
122+
$qs = '';
123+
foreach ($cleanQueryParams as $parameter => $value) {
124+
$paramName = urlencode($parameter);
125+
126+
if (!is_array($value)) {
127+
$qs .= "$paramName=" . urlencode($value) . "&";
128+
} else {
129+
if (array_keys($value)[0] === 0) {
130+
// List query param (eg filter[]=haha should become "filter[]": "haha")
131+
$qs .= "$paramName" . '[]=' . urlencode($value[0]) . '&';
132+
} else {
133+
// Hash query param (eg filter[name]=john should become "filter[name]": "john")
134+
foreach ($value as $item => $itemValue) {
135+
$qs .= "$paramName" . '[' . urlencode($item) . ']=' . urlencode($itemValue) . '&';
136+
}
137+
}
138+
}
139+
}
140+
141+
return rtrim($qs, '&');
142+
}
143+
144+
public static function printQueryParamsAsKeyValue(
145+
array $cleanQueryParams,
146+
string $quote = "\"",
147+
string $delimiter = ":",
148+
int $spacesIndentation = 4,
149+
string $braces = "{}",
150+
int $closingBraceIndentation = 0
151+
): string {
152+
$output = "{$braces[0]}\n";
153+
foreach ($cleanQueryParams as $parameter => $value) {
154+
if (!is_array($value)) {
155+
$output .= str_repeat(" ", $spacesIndentation);
156+
$output .= "$quote$parameter$quote$delimiter $quote$value$quote,\n";
157+
} else {
158+
if (array_keys($value)[0] === 0) {
159+
// List query param (eg filter[]=haha should become "filter[]": "haha")
160+
$output .= str_repeat(" ", $spacesIndentation);
161+
$output .= "$quote$parameter" . "[]$quote$delimiter $quote$value[0]$quote,\n";
162+
} else {
163+
// Hash query param (eg filter[name]=john should become "filter[name]": "john")
164+
foreach ($value as $item => $itemValue) {
165+
$output .= str_repeat(" ", $spacesIndentation);
166+
$output .= "$quote$parameter" . "[$item]$quote$delimiter $quote$itemValue$quote,\n";
167+
}
168+
}
169+
}
170+
}
171+
172+
return $output . str_repeat(" ", $closingBraceIndentation) . "{$braces[1]}";
173+
}
118174
}

tests/Fixtures/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ let params = {
233233
"user_id": "me",
234234
"page": "4",
235235
"filters": "consequatur",
236-
"url_encoded": "+ []&=",
236+
"url_encoded": "+ []&=",
237237
};
238238
Object.keys(params)
239239
.forEach(key => url.searchParams.append(key, params[key]));

0 commit comments

Comments
 (0)