66use Illuminate \Foundation \Http \Events \RequestHandled ;
77use Illuminate \Http \JsonResponse ;
88use Illuminate \Http \Request ;
9+ use Illuminate \Support \Collection ;
910use Illuminate \Support \ViewErrorBag ;
1011use Laravel \Pulse \Pulse ;
1112use 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