Skip to content

Commit 9b1fc7b

Browse files
committed
Format arrays with symfony/var-exporter
1 parent 66cc5e9 commit 9b1fc7b

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

app/Support/HttpCall.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Request;
66
use Illuminate\Support\Str;
7+
use Symfony\Component\VarExporter\VarExporter;
78

89
class HttpCall
910
{
@@ -94,24 +95,8 @@ private static function generateRequest(Request $request): string
9495
return sprintf('\'%s\', %s', $request->url(), self::prettyPrintArray($request->data()));
9596
}
9697

97-
private static function prettyPrintArray(array $data, $assoc = true)
98+
private static function prettyPrintArray(array $data)
9899
{
99-
$output = var_export($data, true);
100-
// $patterns = [
101-
// "/array \(/" => '[',
102-
// "/^([ ]*)\)(,?)$/m" => '$1]$2',
103-
// "/=>[ ]?\n[ ]+\[/" => '=> [',
104-
// "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3',
105-
// "/([ ]*)\d+ => ([\[\'])/" => '$1$2',
106-
// ];
107-
// $output = preg_replace(array_keys($patterns), $patterns, $output);
108-
$output = preg_replace('/^\s+/m', ' ', $output);
109-
$output = preg_replace(['/^array \(/', '/\)$/'], ['[', ' ]'], $output);
110-
111-
if (!$assoc) {
112-
$output = preg_replace('/^(\s+)[^=]+=>\s+/m', '$1', $output);
113-
}
114-
115-
return trim(str_replace("\n", PHP_EOL, $output));
100+
return trim(preg_replace('/^/m', ' ', VarExporter::export($data)));
116101
}
117102
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"guzzlehttp/guzzle": "^7.2",
1010
"laravel/framework": "^9.19",
1111
"laravel/sanctum": "^3.0",
12-
"laravel/tinker": "^2.7"
12+
"laravel/tinker": "^2.7",
13+
"symfony/var-exporter": "^6.1"
1314
},
1415
"require-dev": {
1516
"fakerphp/faker": "^1.9.1",

tests/fixtures/mailgun-example.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ Http::asMultipart()
22
->withBasicAuth('api', '0987654321')
33
->post('https://api.mailgun.net/v3/example.com/messages', [
44
'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>',
5-
'to' => ['YOU@YOUR_DOMAIN_NAME', '[email protected]'],
5+
'to' => [
6+
'YOU@YOUR_DOMAIN_NAME',
7+
8+
],
69
'subject' => 'Hello',
710
'text' => 'Testing some Mailgun awesomeness!',
811
]);

tests/fixtures/post-with-data.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Http::asForm()
22
->post('https://example.com', [
33
'foo' => 'bar',
4-
'rho' => [1, 2],
4+
'rho' => [
5+
1,
6+
2,
7+
],
58
'baz' => 'qui',
69
]);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Http::asMultipart()
22
->post('https://example.com', [
33
'foo' => 'bar',
4-
'rho' => [1, 2],
4+
'rho' => [
5+
1,
6+
2,
7+
],
58
'baz' => 'qui',
69
]);

tests/fixtures/stripe-example.out

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ Http::asForm()
44
'amount' => 2000,
55
'currency' => 'usd',
66
'source' => 'tok_visa',
7-
'metadata[order_id]' => 6735,
8-
]);
7+
'metadata' => [
8+
'order_id' => 6735,
9+
],
10+
]);

0 commit comments

Comments
 (0)