Skip to content

Commit 669a5a9

Browse files
Merge pull request #1724 from specialtactics/v2
Implement a proper array response function, which uses a transformer
2 parents 1ff4d3a + dad84c2 commit 669a5a9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Http/Response/Factory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,38 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul
140140
return new Response($item, 200, [], $binding);
141141
}
142142

143+
/**
144+
* Bind an arbitrary array to a transformer and start building a response.
145+
*
146+
* @param array $array
147+
* @param $transformer
148+
* @param array $parameters
149+
* @param Closure|null $after
150+
*
151+
* @return Response
152+
*/
153+
public function array(array $array, $transformer = null, $parameters = [], Closure $after = null)
154+
{
155+
if ($parameters instanceof \Closure) {
156+
$after = $parameters;
157+
$parameters = [];
158+
}
159+
160+
// For backwards compatibility, allow no transformer
161+
if ($transformer) {
162+
// Use the PHP stdClass for this purpose, as a work-around, since we need to register a class binding
163+
$class = 'stdClass';
164+
// This will convert the array into an stdClass
165+
$array = (object) $array;
166+
167+
$binding = $this->transformer->register($class, $transformer, $parameters, $after);
168+
} else {
169+
$binding = null;
170+
}
171+
172+
return new Response($array, 200, [], $binding);
173+
}
174+
143175
/**
144176
* Bind a paginator to a transformer and start building a response.
145177
*

0 commit comments

Comments
 (0)