Skip to content

Commit 429c735

Browse files
Bring this forward from v2
1 parent 7355bb6 commit 429c735

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
@@ -148,6 +148,38 @@ public function item($item, $transformer = null, $parameters = [], Closure $afte
148148
return new Response($item, 200, [], $binding);
149149
}
150150

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

0 commit comments

Comments
 (0)