diff --git a/src/Fractal.php b/src/Fractal.php index e6393e2..ff1cbfc 100644 --- a/src/Fractal.php +++ b/src/Fractal.php @@ -6,6 +6,7 @@ use League\Fractal\Manager; use League\Fractal\Pagination\CursorInterface; use League\Fractal\Pagination\PaginatorInterface; +use League\Fractal\TransformerAbstract; use Spatie\Fractalistic\Exceptions\InvalidTransformation; use Spatie\Fractalistic\Exceptions\NoTransformerSpecified; use Traversable; @@ -21,7 +22,7 @@ class Fractal implements JsonSerializable /** @var string|\League\Fractal\Serializer\SerializerAbstract */ protected $serializer; - /** @var string|callable|\League\Fractal\TransformerAbstract */ + /** @var string|callable|\League\Fractal\TransformerAbstract|null */ protected $transformer; /** @var \League\Fractal\Pagination\PaginatorInterface */ @@ -177,7 +178,7 @@ protected function determineDataType($data) /** * Set the class or function that will perform the transform. * - * @param string|callable|\League\Fractal\TransformerAbstract $transformer + * @param string|callable|\League\Fractal\TransformerAbstract|null $transformer * * @return $this */ @@ -463,6 +464,16 @@ public function jsonSerialize(): ?array return $this->toArray(); } + /** + * Get the transformer. + * + * @return string|callable|\League\Fractal\TransformerAbstract|null + */ + public function getTransformer() + { + return $this->transformer; + } + /** * Support for magic methods to included data. * diff --git a/tests/FractalTest.php b/tests/FractalTest.php index fb5c410..af5753a 100644 --- a/tests/FractalTest.php +++ b/tests/FractalTest.php @@ -228,3 +228,27 @@ assertEquals($expectedArray, $jsonSerialized); }); + +it('can get the transformer', function ($transformer): void { + $fractal = Fractal::create(); + $fractal->transformWith($transformer); + + $result = $fractal->getTransformer(); + + expect($result)->toBe($transformer); +})->with([ + 'instance' => [ + new TestTransformer(), + ], + 'fqcn' => [ + TestTransformer::class, + ], + 'callable' => [ + function (): array { + return []; + }, + ], + 'null' => [ + null, + ] +]);