@@ -48,7 +48,7 @@ abstract class DataTablesEditor
4848 /**
4949 * List of custom editor actions.
5050 *
51- * @var string[]
51+ * @var array<array-key, string|class-string>
5252 */
5353 protected array $ customActions = [];
5454
@@ -89,25 +89,25 @@ abstract class DataTablesEditor
8989
9090 /**
9191 * Process dataTables editor action request.
92- *
93- * @return JsonResponse
94- *
95- * @throws DataTablesEditorException
9692 */
97- public function process (Request $ request ): mixed
93+ public function process (? Request $ request = null ): JsonResponse
9894 {
99- if ($ request ->get ('action ' ) && is_string ($ request ->get ('action ' ))) {
100- $ this ->action = $ request ->get ('action ' );
101- } else {
102- throw new DataTablesEditorException ('Invalid action requested! ' );
103- }
95+ $ request ??= request ();
96+ $ this ->action = $ request ->get ('action ' );
10497
105- if (! in_array ($ this ->action , array_merge ($ this ->actions , $ this ->customActions ))) {
106- throw new DataTablesEditorException (sprintf ('Requested action (%s) not supported! ' , $ this ->action ));
107- }
98+ throw_unless (
99+ $ this ->isValidAction ($ request ),
100+ DataTablesEditorException::class,
101+ 'Invalid action requested! '
102+ );
108103
109104 try {
110- return $ this ->{$ this ->action }($ request );
105+ if (method_exists ($ this , $ this ->action )) {
106+ return $ this ->{$ this ->action }($ request );
107+ }
108+
109+ // @phpstan-ignore-next-line method.nonObject
110+ return resolve ($ this ->customActions [$ this ->action ], ['editor ' => $ this ])->handle ($ request );
111111 } catch (Exception $ exception ) {
112112 $ error = config ('app.debug ' )
113113 ? '<strong>Server Error:</strong> ' .$ exception ->getMessage ()
@@ -119,6 +119,18 @@ public function process(Request $request): mixed
119119 }
120120 }
121121
122+ public function isValidAction (Request $ request ): bool
123+ {
124+ $ validActions = $ this ->actions ;
125+ foreach ($ this ->customActions as $ key => $ action ) {
126+ $ validActions [] = is_numeric ($ key ) ? $ action : $ key ;
127+ }
128+
129+ return in_array ($ this ->action , $ validActions )
130+ && $ request ->get ('action ' )
131+ && is_string ($ request ->get ('action ' ));
132+ }
133+
122134 protected function getUseFriendlyErrorMessage (): string
123135 {
124136 return 'An error occurs while processing your request. ' ;
@@ -127,7 +139,7 @@ protected function getUseFriendlyErrorMessage(): string
127139 /**
128140 * Display success data in dataTables editor format.
129141 */
130- protected function toJson (array $ data , array $ errors = [], string |array $ error = '' ): JsonResponse
142+ public function toJson (array $ data , array $ errors = [], string |array $ error = '' ): JsonResponse
131143 {
132144 $ code = 200 ;
133145
0 commit comments