Skip to content

Commit

Permalink
Merge pull request #1724 from specialtactics/v2
Browse files Browse the repository at this point in the history
Implement a proper array response function, which uses a transformer
  • Loading branch information
specialtactics committed Mar 19, 2020
2 parents 1ff4d3a + dad84c2 commit 669a5a9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Http/Response/Factory.php
Expand Up @@ -140,6 +140,38 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul
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 669a5a9

Please sign in to comment.