Skip to content

Commit

Permalink
Merge pull request #80 from Mohammad-Alavi/allow-accessing-the-transf…
Browse files Browse the repository at this point in the history
…ormer

Allow accessing the transformer
  • Loading branch information
Nielsvanpach authored Dec 17, 2024
2 parents 44e7fe0 + 1d452f7 commit 9925495
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Fractal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/FractalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
]);

0 comments on commit 9925495

Please sign in to comment.