Skip to content

Commit 42c5b78

Browse files
Merge pull request #1711 from xwiz/enable_bindings
Enable default response transformation with bound transformers
2 parents f12bce2 + 8c0fff4 commit 42c5b78

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Http/Response/Factory.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function noContent()
9393
*
9494
* @return \Dingo\Api\Http\Response
9595
*/
96-
public function collection(Collection $collection, $transformer, $parameters = [], Closure $after = null)
96+
public function collection(Collection $collection, $transformer = null, $parameters = [], Closure $after = null)
9797
{
9898
if ($collection->isEmpty()) {
9999
$class = get_class($collection);
@@ -106,7 +106,11 @@ public function collection(Collection $collection, $transformer, $parameters = [
106106
$parameters = [];
107107
}
108108

109-
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
109+
if ($transformer !== null) {
110+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
111+
} else {
112+
$binding = $this->transformer->getBinding($collection);
113+
}
110114

111115
return new Response($collection, 200, [], $binding);
112116
}
@@ -135,7 +139,11 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul
135139
$parameters = [];
136140
}
137141

138-
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
142+
if ($transformer !== null) {
143+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
144+
} else {
145+
$binding = $this->transformer->getBinding($item);
146+
}
139147

140148
return new Response($item, 200, [], $binding);
141149
}
@@ -158,7 +166,11 @@ public function paginator(Paginator $paginator, $transformer, array $parameters
158166
$class = get_class($paginator->first());
159167
}
160168

161-
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
169+
if ($transformer !== null) {
170+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
171+
} else {
172+
$binding = $this->transformer->getBinding($paginator->first());
173+
}
162174

163175
return new Response($paginator, 200, [], $binding);
164176
}

src/Transformer/Factory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function transformableType($value)
110110
*
111111
* @return \Dingo\Api\Transformer\Binding
112112
*/
113-
protected function getBinding($class)
113+
public function getBinding($class)
114114
{
115115
if ($this->isCollection($class) && ! $class->isEmpty()) {
116116
return $this->getBindingFromCollection($class);

0 commit comments

Comments
 (0)