diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index 776c06ec6..aeecbfbb4 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -93,7 +93,7 @@ public function noContent() * * @return \Dingo\Api\Http\Response */ - public function collection(Collection $collection, $transformer, $parameters = [], Closure $after = null) + public function collection(Collection $collection, $transformer = null, $parameters = [], Closure $after = null) { if ($collection->isEmpty()) { $class = get_class($collection); @@ -106,7 +106,11 @@ public function collection(Collection $collection, $transformer, $parameters = [ $parameters = []; } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); + if ($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($collection); + } return new Response($collection, 200, [], $binding); } @@ -135,7 +139,11 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul $parameters = []; } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); + if ($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($item); + } return new Response($item, 200, [], $binding); } @@ -158,7 +166,11 @@ public function paginator(Paginator $paginator, $transformer, array $parameters $class = get_class($paginator->first()); } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); + if ($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($paginator->first()); + } return new Response($paginator, 200, [], $binding); } diff --git a/src/Transformer/Factory.php b/src/Transformer/Factory.php index fb3f72a15..ce8676bbe 100644 --- a/src/Transformer/Factory.php +++ b/src/Transformer/Factory.php @@ -110,7 +110,7 @@ public function transformableType($value) * * @return \Dingo\Api\Transformer\Binding */ - protected function getBinding($class) + public function getBinding($class) { if ($this->isCollection($class) && ! $class->isEmpty()) { return $this->getBindingFromCollection($class);