Skip to content

Commit accb81e

Browse files
committed
Enable default response transformation with bound transformers
1 parent 05299b0 commit accb81e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Http/Response/Factory.php

Lines changed: 17 additions & 3 deletions
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,6 +106,12 @@ public function collection(Collection $collection, $transformer, $parameters = [
106106
$parameters = [];
107107
}
108108

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

111117
return new Response($collection, 200, [], $binding);
@@ -130,7 +136,11 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul
130136
$parameters = [];
131137
}
132138

133-
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
139+
if($transformer !== null) {
140+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
141+
} else {
142+
$binding = $this->transformer->getBinding($item);
143+
}
134144

135145
return new Response($item, 200, [], $binding);
136146
}
@@ -153,7 +163,11 @@ public function paginator(Paginator $paginator, $transformer, array $parameters
153163
$class = get_class($paginator->first());
154164
}
155165

156-
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
166+
if($transformer !== null) {
167+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
168+
} else {
169+
$binding = $this->transformer->getBinding($paginator->first());
170+
}
157171

158172
return new Response($paginator, 200, [], $binding);
159173
}

src/Transformer/Factory.php

Lines changed: 1 addition & 1 deletion
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)