File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change 22
33namespace Yajra \DataTables \Transformers ;
44
5+ use Closure ;
56use Illuminate \Support \Collection as LaravelCollection ;
67use League \Fractal \Manager ;
78use 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments