Skip to content

Commit 05cc461

Browse files
committed
WIP
1 parent a0359ce commit 05cc461

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

app/Models/Request.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ public static function create(array $data): self
2525
}
2626

2727
if (!empty($data['data'])) {
28-
$request->data = $data['data'];
28+
dump($data['data']);
29+
// TODO: handle JSON payload
30+
// -d 'foo=bar&qui[]=1' -d 'qui[]=2'
31+
$request->data = collect($data['data'])
32+
->flatMap(function ($data) {
33+
$output = [];
34+
parse_str($data, $output);
35+
36+
return $output;
37+
})
38+
->all();
39+
40+
$dataAsArrays = array_map();
41+
$data = array_merge_recursive(...$dataAsArrays);
2942
}
3043

3144
if (!empty($data['fields'])) {

app/Support/HttpCall.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,43 @@
22

33
namespace App\Support;
44

5+
use App\Models\Request;
6+
use Illuminate\Support\Str;
7+
58
class HttpCall
69
{
7-
public static function format(\App\Models\Request $request)
10+
public static function format(Request $request): string
11+
{
12+
return sprintf(
13+
'Http::%s%s(%s);',
14+
self::generateOptions($request),
15+
Str::lower($request->method()),
16+
self::generateRequest($request),
17+
);
18+
}
19+
20+
private static function generateOptions(Request $request): string
821
{
9-
// TODO:
10-
return 'Http::get(' . $request->url() . ');';
22+
$options = [];
23+
24+
if ($request->headers()) {
25+
// TODO: what about headers that have Http helper methods, for example: `acceptJson`
26+
$options[] = 'withHeaders(' . var_export($request->headers(), true) . ')';
27+
}
28+
29+
if (empty($options)) {
30+
return '';
31+
}
32+
33+
return implode(PHP_EOL . ' ->', $options) . PHP_EOL . ' ->';
34+
}
35+
36+
private static function generateRequest(Request $request): string
37+
{
38+
if (empty($request->data())) {
39+
return "'" . $request->url() . "'";
40+
}
41+
42+
return sprintf('\'%s\', %s', $request->url(), var_export($request->data(), true));
1143
}
1244
}

0 commit comments

Comments
 (0)