Skip to content

Commit 94e15b3

Browse files
authored
Merge pull request #93 from yajra/actions
feat: custom action class
2 parents 7c05aa1 + 5dd2f72 commit 94e15b3

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

src/DataTablesEditor.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/Editors/Remove2FA.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Editors;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Request;
7+
8+
class Remove2FA
9+
{
10+
public function handle(Request $request): JsonResponse
11+
{
12+
return new JsonResponse([
13+
'message' => '2FA has been removed successfully.',
14+
]);
15+
}
16+
}

tests/Editors/UsersDataTableEditor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class UsersDataTableEditor extends DataTablesEditor
1313
{
1414
protected $model = User::class;
1515

16+
protected array $customActions = [
17+
'remove2fa' => Remove2FA::class,
18+
];
19+
1620
/**
1721
* Get create action validation rules.
1822
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Feature;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Yajra\DataTables\Tests\TestCase;
7+
8+
class DataTablesEditorActionTest extends TestCase
9+
{
10+
#[Test]
11+
public function it_can_process_custom_action_request()
12+
{
13+
$response = $this->postJson('users', [
14+
'action' => 'remove2fa',
15+
'data' => [],
16+
]);
17+
18+
$data = $response->json();
19+
$this->assertArrayHasKey('message', $data);
20+
$this->assertEquals('2FA has been removed successfully.', $data['message']);
21+
}
22+
}

tests/Feature/DataTablesEditorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DataTablesEditorTest extends TestCase
1515
public function it_throws_exception_on_invalid_action()
1616
{
1717
$this->expectException(DataTablesEditorException::class);
18-
$this->expectExceptionMessage('Requested action (invalid) not supported!');
18+
$this->expectExceptionMessage('Invalid action requested!');
1919

2020
$editor = new UsersDataTableEditor;
2121
request()->merge(['action' => 'invalid']);

0 commit comments

Comments
 (0)