@@ -44,7 +44,7 @@ public function __construct(Container $container)
4444 * Register a transformer binding resolver for a class.
4545 *
4646 * @param string $class
47- * @param string|callable $resolver
47+ * @param string|callable|object $resolver
4848 * @return \Dingo\Api\Transformer\Factory
4949 */
5050 public function transform ($ class , $ resolver )
@@ -75,7 +75,7 @@ public function transformResponse($response)
7575 */
7676 public function transformableResponse ($ response )
7777 {
78- return $ this ->transformableType ($ response ) and $ this ->hasBinding ($ response );
78+ return $ this ->transformableType ($ response ) and ( $ this ->hasBinding ($ response) or $ this -> boundByContract ( $ response ) );
7979 }
8080
8181 /**
@@ -92,7 +92,7 @@ public function transformableType($value)
9292 /**
9393 * Resolve a transfomer binding instance.
9494 *
95- * @param string|callable $resolver
95+ * @param string|callable|object $resolver
9696 * @return mixed
9797 */
9898 protected function resolveTransformerBinding ($ resolver )
@@ -101,15 +101,19 @@ protected function resolveTransformerBinding($resolver)
101101 {
102102 return $ this ->container ->make ($ resolver );
103103 }
104-
105- return call_user_func ($ resolver , $ this ->container );
104+ elseif (is_callable ($ resolver ))
105+ {
106+ return call_user_func ($ resolver , $ this ->container );
107+ }
108+
109+ return $ resolver ;
106110 }
107111
108112 /**
109113 * Get a registered transformer binding.
110114 *
111115 * @param string|object $class
112- * @return string|callable
116+ * @return string|callable|object
113117 * @throws \RuntimeException
114118 */
115119 protected function getBinding ($ class )
@@ -119,6 +123,11 @@ protected function getBinding($class)
119123 return $ this ->getBindingFromCollection ($ class );
120124 }
121125
126+ if ($ this ->boundByContract ($ class ))
127+ {
128+ return $ class ->getTransformer ();
129+ }
130+
122131 $ class = is_object ($ class ) ? get_class ($ class ) : $ class ;
123132
124133 if ( ! $ this ->hasBinding ($ class ))
@@ -158,6 +167,17 @@ protected function hasBinding($class)
158167 return isset ($ this ->bindings [$ class ]);
159168 }
160169
170+ /**
171+ * Determine if the class is bound by the transformable contract.
172+ *
173+ * @param string|object $class
174+ * @return bool
175+ */
176+ protected function boundByContract ($ class )
177+ {
178+ return is_object ($ class ) and $ class instanceof TransformableInterface;
179+ }
180+
161181 /**
162182 * Determine if the instance is a collection.
163183 *
0 commit comments