-
Notifications
You must be signed in to change notification settings - Fork 2
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
WIP v2 #5
base: master
Are you sure you want to change the base?
WIP v2 #5
Conversation
Few things as I think of them: It would be cool if the transformer was set as:
Then it could be chained set in the constructor as in your example or chained inline:
Also we could take this same idea further and remove the resource key from the response and instead chain it:
Would make the method signature of respond with item simpler and we could just default the resource key to false if it wasn't explicitly set. |
Oh awesome I like the ideas!
…On Mon, Feb 13, 2017 at 19:13 Josh Forbes ***@***.***> wrote:
Few things as I think of them:
It would be cool if the transformer was set as:
$this-setTransformer($whatever)
Then it could be chained set in the constructor as in your example or
chained inline:
$this-setTransformer($transformer)->respondWithItem($course)
Also we could take this same idea further and remove the resource key from
the response and instead chain it:
$this-setTransformer($transformer)->setResourceKey('course')->respondWithItem($course)
Would make the method signature of respond with item simpler and we could
just default the resource key to false if it wasn't explicitly set.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAbacH6A8LieYNRnKRYKz5RgTPtM7XXeks5rcPHCgaJpZM4L-i3n>
.
|
There is one thing I am not fond of. Supporting the setting of a default model to $this-model in the constructor and then it making it optional to the resondWith methods. It is such a narrow use case that you would want an endpoint that just returns all of the items with no restrictions. To me it just makes everything more readable to specify it inline.
or
or
That being said I think it could be appropriate to have it work with '$this-model' in the template controller you are planning on implementing. |
I am really liking this sytax overall. I think the way I would typically use it would be:
I think that's super readable. Of course I would set the transformer and resource key in the constructor if it was going to be the same for all the endpoints in a controller. |
Also as a note so I don't forget. We should make sure that all of these 'respondWith' methods can take either a query builder or a collection/item. If a builder is passed in then we automatically try to eager load available includes. If a collection/item is passed in then we just skip the eager load step and go straight to transformation. I guess the only exception would be the 'respondWithPaginatedCollection' method - which really needs a builder to do pagination efficiently. If we get really ambition we could could make it fall back on less efficient pagination if a collection is passed in (it does not do this currently). |
I guess you can just let me know what parts you'd like me to implement and
I'll go for it. It all sounds good to me.
…On Tue, Feb 14, 2017 at 19:51 Josh Forbes ***@***.***> wrote:
Also as a note so I don't forget. We should make sure that all of these
'respondWith' methods can take either a builder or a collection/item. If a
builder is passed in then we automatically try to eager load available
includes but if a collection/item is passed in then we just skip that step.
I guess the only exception would be the 'respondWithPaginatedCollection'
method - which really needs a builder to do pagination efficiently. If we
get really ambition we could could make it fall back on less efficient
pagination if a collection is passed in (it does not do this currently).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAbacDTErDPrOrNuGqIyVIcXb0V4aUwEks5rckv0gaJpZM4L-i3n>
.
|
Here's the latest. I added everything except checking if we can accept a builder or an item, and also a couple methods havent been updated either. But it works for respondwithcollection and item ;)
We can't extend and overwrite a class because it enforces you to have the same function parameters of whatever it is you overwrite, so we have to use traits. Would love to find a way to do this without four use statements... lmk what you think. |
… to a serializer. The proper way to override resource key behavior by default is to have a custom serializer per project. see: http://fractal.thephpleague.com/serializers/
…lso a bit of cleanup.
@@ -34,7 +34,7 @@ public function collection($resourceKey, array $data) | |||
*/ | |||
public function item($resourceKey, array $data) | |||
{ | |||
if ($resourceKey === false) { | |||
if (!$resourceKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this change. Few things:
- If you do this - then the remaining lines of this file do not execute correctly.
- Breaks some tests as we were making assertions about the expected structure of the data.
- The way it is setup before your change allows the most configurable default (false for no key, null for data, and any value to set that as the key)
- Your change only ever allows it to be no key or whatever value - but that is misleading as this class is called 'dataArraySerializer'. The general point of this class is to serialize with a data key unless the user overrides it.
- I know what you are getting at - in your projects you don't ever want a 'data' key. Did you know that as it is currently setup it is as simply as doing an artisan config:publish and then changing the 'default_serializer' config option in transmit.php to 'arraySerializer'. Or alternatively make your own super simple serializer on a project by project basis and just give the path to it in the config file.
Now that I am thinking about it - I think I will update the arraySerializer class to basically do what you have here. Since even with that serializer the user would need the option to set a resourcekey when they were doing pagination.
Changes
Here's my proposed syntax changes:
From this:
to this
All that is required is setting the model and transformer in your constructor:
Need custom logic like searching for filter by a company id? Just pass in the modified builder:
Respond with item:
From:
to: