Giter Club home page Giter Club logo

Comments (6)

flugg avatar flugg commented on May 18, 2024

In my own projects I usually have a one to one mapping with models and transformers, and if I want to expose different fields based on different criterias I would add some conditionals to the transform() method in the transformer. So that's basically the reasoning. However, I appreciate that others might have a need for this and will look into an elegant way of handling it.

Do you have a concrete example of where it would make sense to have multiple transformers for one model?

from laravel-responder.

jjdrake avatar jjdrake commented on May 18, 2024

It may be a bit of personal preference but I've taken the approach of only sending attributes which are immediately necessary through my JSON responses and consequentially my use cases are quite varied. More concretely, I may want to respond with users' id's and usernames in one case where in another location, (or for a more privileged end-user), I may want to also send first name, last name, and email attributes.

from laravel-responder.

flugg avatar flugg commented on May 18, 2024

Yeah, that makes sense. I'm experimenting with a few options now. What do you think of being able to return an associative array in the transformer() method on the models. So for instance a User model could have 3 different transformers:

public static function transformer()
    {
        return [
            'admin' => AdminUserTransformer::class,
            'moderator' => ModeratorUserTransformer::class,
            'regular' => RegularUserTransformer::class
        ];
    }

Then I haven't decided for a good API to choose which transformer to use. Since there are already quite a lot of (optional) parameters on the success() method, I'm considering the ability to be able to chain a transformer() method on the responder:

Responder::transformer('admin')->success($users, 200);

In that case an exception would be thrown if you don't specify the transformer type. Not completely satisfied with it, I'll think about it some more over the weekend.

If you have a better suggestion, please let me know :)

from laravel-responder.

flugg avatar flugg commented on May 18, 2024

I've been experimenting some more, and decided to allow an alternative syntax to build your success responses. A bit more similar to how the spatie/laravel-fractal package works, the above example would look like this:

Responder::transform($users, 'admin')->respond(200);

You can then also be explicit and instantiate a transformer directly:

Responder::transform($users, new UserTransformer)->respond();

The success() method is also getting refactored to call on these methods behind the scene, and will basically work as a shorthand.

This alternative syntax will also allow for some control:

Responder::transform($users, 'admin')->serialize(new DataArraySerializer)->respond();
Responder::transform($users, 'admin')->respond(201, [ 'meta' => 'data' ]);
Responder::transform($users, 'admin')->toArray();
Responder::transform($users, 'admin')->toResource();

Implementing this means a bit reworking of a few core pieces of the package, but hopefully shouldn't take too long. I'll keep you posted.

from laravel-responder.

flugg avatar flugg commented on May 18, 2024

Thought I'd let you know the status of the issue. Got my hands quite full with work related stuff at the moment, but working on a major rewrite of the package to allow a more flexible way of building responses. Quite similar to the code examples above.

I'm guessing it should be done by the end of the week, perhaps a few days longer.

from laravel-responder.

flugg avatar flugg commented on May 18, 2024

Hey! Sorry for the delay, ended up being a bit more work than expected. A few parts of the package has been completely rewritten from scratch, for the better anyway. I've also gone through the documentation word for word and simplified it quite a bit. You can update to the latest version (v1.2.0+) using composer update.

You can read about the new API for explicitly transforming models here: https://github.com/flugger/laravel-responder#transformers.

Would love to hear what you think and wether or not it solves all your needs :)

from laravel-responder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.