Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accessing the transformer #80

Merged

Conversation

Mohammad-Alavi
Copy link
Contributor

@Mohammad-Alavi Mohammad-Alavi commented Dec 6, 2024

Summary

Addded a public method to be able to access the transformer.

Reasoning

TL;DR

Using my own extended Fractal facade, this allows me to call the ok method both with and without setting a transformer.

  • Fractal::create($this->user, UserTransformer::class)->ok();
  • Fractal::ok();

Otherwise, I get a NoTransformerSpecified exception.

Yes, I can just create a 200 response using a thousand other ways but I woud like to be consistence and use the Fractal facade to generate all of my responses.

Explanation

Background

I am using your awesome larave-fractal package which has the Spatie\Fractal\Fractal that extends the Spatie\Fractalistic\Fractal.

Glossary

When I say I mean
LaravelFractal Spatie\Fractal\Fractal
FractalisticFractal Spatie\Fractalistic\Fractal
MyFractal My class that extends Spatie\Fractal\Fractal

Why

LaravelFractal class has a convenient respond method that helps with genarating responses.
In my project I have added some other convenient methods to MyFractal. For example,

class MyFractal extends LaravelFractal
{
    public function ok(): JsonResponse
    {
        // notice here, I am using the proposed method
        if (is_null($this->getTransformer())) {
            // `empty` is just another convenient method of mine.
            // It returns a transformer that returns an empty array, [], never mind it!
            $this->transformWith(Transformer::empty());
        }
    
        return $this->respond(200); // I am using the `LaravelFractal::respond`
    }
}

Using my own facade, this allows me to call the ok method both with and without setting a transformer.

  • MyFractal::create($this->user, UserTransformer::class)->ok();
  • MyFractal::ok();

Otherwise, I get a NoTransformerSpecified exception.
This happens because MyFractal::ok method uses the LaravelFractal::respond method underneath. And in turn,
LaravelFractal::respond method calls the FractalisticFractal::createData method and this method throws an exception if it is called without a transfromer. (which is the case with MyFractal::ok mthod)

    public function createData()
    {
        if (is_null($this->transformer)) {
            throw new NoTransformerSpecified();
        }
        // ...
    }

@@ -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 */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just update the outdated docblock, because the transformer can be null already.

@Mohammad-Alavi Mohammad-Alavi marked this pull request as ready for review December 6, 2024 09:39
@Nielsvanpach
Copy link
Member

Thanks!

@Nielsvanpach Nielsvanpach merged commit 9925495 into spatie:main Dec 17, 2024
9 checks passed
@Mohammad-Alavi Mohammad-Alavi deleted the allow-accessing-the-transformer branch December 17, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants