Skip to content

Commit 91de2cb

Browse files
committed
wip
1 parent 77e9c6c commit 91de2cb

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/Recorders/ValidationErrors.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Http\Events\RequestHandled;
77
use Illuminate\Http\JsonResponse;
88
use Illuminate\Http\Request;
9+
use Illuminate\Support\Collection;
910
use Illuminate\Support\ViewErrorBag;
1011
use Laravel\Pulse\Pulse;
1112
use stdClass;
@@ -58,54 +59,58 @@ public function record(RequestHandled $event): void
5859
return;
5960
}
6061

61-
foreach ($this->parseValidationErrors($request, $response) as $name) {
62-
$this->pulse->record(
63-
'validation_error',
64-
json_encode([$request->method(), $path, $via, $name], flags: JSON_THROW_ON_ERROR),
65-
)->count();
66-
}
62+
$this->parseValidationErrors($request, $response)->each(fn ($name) => $this->pulse->record(
63+
'validation_error',
64+
json_encode([$request->method(), $path, $via, $name], flags: JSON_THROW_ON_ERROR),
65+
)->count());
6766
});
6867
}
6968

7069
/**
7170
* Parse validation errors.
7271
*
73-
* @return array<int, string>
72+
* @return \Illuminate\Support\Collection<int, string>
7473
*/
75-
protected function parseValidationErrors(Request $request, BaseResponse $response): array
74+
protected function parseValidationErrors(Request $request, BaseResponse $response): Collection
7675
{
7776
return $this->parseSessionValidationErrors($request, $response)
7877
?? $this->parseJsonValidationErrors($request, $response)
7978
?? $this->parseInertiaValidationErrors($request, $response)
8079
?? $this->parseUnknownValidationErrors($request, $response)
81-
?? [];
80+
?? collect([]);
8281
}
8382

8483
/**
8584
* Parse session validation errors.
8685
*
87-
* @return null|array<int, string>
86+
* @return null|\Illuminate\Support\Collection<int, string>
8887
*/
89-
protected function parseSessionValidationErrors(Request $request, BaseResponse $response): ?array
88+
protected function parseSessionValidationErrors(Request $request, BaseResponse $response): ?Collection
9089
{
9190
if (
9291
$response->getStatusCode() !== 302 ||
9392
! $request->hasSession() ||
94-
! $request->session()->get('errors', null) instanceof ViewErrorBag
93+
! ($errors = $request->session()->get('errors', null)) instanceof ViewErrorBag
9594
) {
9695
return null;
9796
}
9897

99-
// TODO: error bags
100-
return $request->session()->get('errors')->keys();
98+
if (count($bags = $errors->getBags()) === 1) {
99+
return collect($bags[0]->keys());
100+
}
101+
102+
// TODO test this
103+
return collect($bags)->flatMap(
104+
fn ($bag, $bagName) => collect($bags[0]->keys())->map(fn ($inputName) => "{$bagName}:{$inputName}")
105+
);
101106
}
102107

103108
/**
104109
* Parse JSON validation errors.
105110
*
106-
* @return null|array<int, string>
111+
* @return null|\Illuminate\Support\Collection<int, string>
107112
*/
108-
protected function parseJsonValidationErrors(Request $request, BaseResponse $response): ?array
113+
protected function parseJsonValidationErrors(Request $request, BaseResponse $response): ?Collection
109114
{
110115
if (
111116
$response->getStatusCode() !== 422 ||
@@ -118,15 +123,15 @@ protected function parseJsonValidationErrors(Request $request, BaseResponse $res
118123
return null;
119124
}
120125

121-
return array_keys($response->original['errors']);
126+
return collect($response->original['errors'])->keys();
122127
}
123128

124129
/**
125130
* Parse Inertia validation errors.
126131
*
127-
* @return null|array<int, string>
132+
* @return null|\Illuminate\Support\Collection<int, string>
128133
*/
129-
protected function parseInertiaValidationErrors(Request $request, BaseResponse $response): ?array
134+
protected function parseInertiaValidationErrors(Request $request, BaseResponse $response): ?Collection
130135
{
131136
if (
132137
! $request->header('X-Inertia') ||
@@ -140,20 +145,21 @@ protected function parseInertiaValidationErrors(Request $request, BaseResponse $
140145
return null;
141146
}
142147

143-
return array_keys((array) $response->original['props']['errors']);
148+
// TODO error bags
149+
return collect($response->original['props']['errors'])->keys();
144150
}
145151

146152
/**
147153
* Parse unknown validation errors.
148154
*
149-
* @return null|array<int, string>
155+
* @return null|\Illuminate\Support\Collection<int, string>
150156
*/
151-
protected function parseUnknownValidationErrors(Request $request, BaseResponse $response): ?array
157+
protected function parseUnknownValidationErrors(Request $request, BaseResponse $response): ?Collection
152158
{
153159
if ($response->getStatusCode() !== 422) {
154160
return null;
155161
}
156162

157-
return ['__unknown'];
163+
return collect(['__unknown']);
158164
}
159165
}

0 commit comments

Comments
 (0)