diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index dafa2f110..578b859fe 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -148,6 +148,38 @@ public function item($item, $transformer = null, $parameters = [], Closure $afte return new Response($item, 200, [], $binding); } + /** + * Bind an arbitrary array to a transformer and start building a response. + * + * @param array $array + * @param $transformer + * @param array $parameters + * @param Closure|null $after + * + * @return Response + */ + public function array(array $array, $transformer = null, $parameters = [], Closure $after = null) + { + if ($parameters instanceof \Closure) { + $after = $parameters; + $parameters = []; + } + + // For backwards compatibility, allow no transformer + if ($transformer) { + // Use the PHP stdClass for this purpose, as a work-around, since we need to register a class binding + $class = 'stdClass'; + // This will convert the array into an stdClass + $array = (object) $array; + + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = null; + } + + return new Response($array, 200, [], $binding); + } + /** * Bind a paginator to a transformer and start building a response. *