cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
780 stars 109 forks source link

Allowing creating a GenericAdapter with magic methods for relations #424

Closed contrerasojuanc closed 5 years ago

contrerasojuanc commented 5 years ago

I'm trying to create a GenericAdapter (Also GenericSchema and GenericValidators) with magic method _call for relations, so, adding new models won't need any more code than the Eloquent models definition, but method methodForRelation on vendor/cloudcreativity/laravel-json-api/src/Adapter/AbstractResourceAdapter.php is using method_exists witch will return false as those relations methods are managed by the magic method. In this case, is it possible to change the methodForRelation on the library to something like?:

protected function methodForRelation($field)
    {
        if (method_exists($this, $field)) {
            return $field;
        }
        $method = Str::camelize($field);
       if (method_exists($this, $method)) {
            return $method;
        }
     return is_callable([$this, $field]) ? $field : null;
}

If it is possible, can I make the PR? If not, do we have any way to achive something similar?

Thanks

lindyhopchris commented 5 years ago

I'm not keen to add this because the method matches how it is implemented in this package. Saying that I've made that method protected for a reason... i.e. to allow the developer to overload it with anything they want. I.e. the package already supports what you want to do via method overloading.

As a side note, I would not recommend making a generic schema. We used to do that but it is highly inefficient when serializing lots of records at once. While it might feel like you're making your life easier as a developer, you'll be slowing your API down a lot, and it's not worth that cost.

contrerasojuanc commented 5 years ago

Thanks for the advice @lindyhopchris overriding that method works.