Skip to content

Commit 316eed5

Browse files
committed
Fix closure transformer
1 parent c5f40b9 commit 316eed5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Transformers/FractalTransformer.php

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

33
namespace Yajra\DataTables\Transformers;
44

5+
use Closure;
56
use Illuminate\Support\Collection as LaravelCollection;
67
use League\Fractal\Manager;
78
use League\Fractal\Resource\Collection;
@@ -82,11 +83,11 @@ protected function createSerializer(SerializerAbstract|string $serializer): Seri
8283
* Get or create transformer instance.
8384
*
8485
* @param \Closure|class-string|TransformerAbstract $transformer
85-
* @return \League\Fractal\TransformerAbstract
86+
* @return \Closure|\League\Fractal\TransformerAbstract
8687
*/
87-
protected function createTransformer(\Closure|string|TransformerAbstract $transformer): TransformerAbstract
88+
protected function createTransformer(Closure|string|TransformerAbstract $transformer): Closure|TransformerAbstract
8889
{
89-
if ($transformer instanceof TransformerAbstract || $transformer instanceof \Closure) {
90+
if ($transformer instanceof TransformerAbstract || $transformer instanceof Closure) {
9091
return $transformer;
9192
}
9293

tests/FractalTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ public function it_can_transform_response()
2525
$this->assertIsString($json['data'][0]['name']);
2626
}
2727

28+
/** @test */
29+
public function it_works_with_closure()
30+
{
31+
$json = $this->getAjax('/closure');
32+
33+
$json->assertJson([
34+
'draw' => 0,
35+
'recordsTotal' => 20,
36+
'recordsFiltered' => 20,
37+
]);
38+
39+
$this->assertIsInt($json['data'][0]['id']);
40+
$this->assertIsString($json['data'][0]['name']);
41+
}
42+
2843
protected function setUp(): void
2944
{
3045
parent::setUp();
@@ -34,5 +49,16 @@ protected function setUp(): void
3449
->setTransformer(UserTransformer::class)
3550
->toJson();
3651
});
52+
53+
$this->app['router']->get('/closure', function () {
54+
return datatables()->eloquent(User::query())
55+
->setTransformer(function (User $user) {
56+
return [
57+
'id' => (int) $user->id,
58+
'name' => $user->name,
59+
];
60+
})
61+
->toJson();
62+
});
3763
}
3864
}

0 commit comments

Comments
 (0)