Skip to content

Commit

Permalink
Merge pull request #1760 from specialtactics/feature/forward-port-res…
Browse files Browse the repository at this point in the history
…ponse-array

Bring this forward from v2
  • Loading branch information
specialtactics authored Nov 26, 2020
2 parents 7355bb6 + 429c735 commit ef9dc3e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Http/Response/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit ef9dc3e

Please sign in to comment.