Skip to content

Commit

Permalink
Merge pull request #1711 from xwiz/enable_bindings
Browse files Browse the repository at this point in the history
Enable default response transformation with bound transformers
  • Loading branch information
specialtactics authored Mar 23, 2020
2 parents f12bce2 + 8c0fff4 commit 42c5b78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/Http/Response/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 42c5b78

Please sign in to comment.