Skip to content

Use trait instead of implement for transformer binding #36

@Propaganistas

Description

@Propaganistas

Hi

Just an idea: transformers currently have 2 ways to be injected:

  1. using a dedicated class and an API::transform call
  2. by implementing Dingo\Api\Transformer\TransformableInterface in the model

In my opinion the second option is more clean as I this seems to me like some sort of model definition similar to getAttribute().
However, how about defining the implementation as a trait? This avoids collision when a model needs multiple interfaces. (Actually I think packages never should define interfaces for models just for this reason)

Some code suggestions:

Change Dingo\API\Transformer\TransformableInterface to Dingo\API\Transformer\TransformableTrait:

trait TransformableTrait {
    /**
     * Get the transformer instance.
     * Should be overridden in the implementing class.
     * 
     * @return mixed
     */
    public function getTransformer() {
        return array();
    }
}

In Dingo\API\Transformer\Factory:

/**
 * Determine if the class is bound by the transformable contract.
 *
 * @param  string|object  $class
 * @return bool
 */
protected function boundByContract($class)
{
    return is_object($class) and in_array('TransformableTrait', class_uses($class));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions