Skip to content

Commit

Permalink
Correctly detect and transform collections. Fixes #30.
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
  • Loading branch information
jasonlewis committed May 8, 2014
1 parent b17bd08 commit de5d1df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public function transform($class, $transformer)
*/
public function transformResponse($response)
{
$transformer = $this->resolveTransformer($this->getTransformer($response));

$this->setRequestedScopes();

$transformer = $this->resolveTransformer($this->getTransformer($response));

$resource = $this->createResource($response, $transformer);

// If the response is a paginator then we'll create a new paginator
Expand Down Expand Up @@ -197,6 +197,11 @@ protected function getTransformer($class)
*/
protected function hasTransformer($class)
{
if ($this->isCollection($class))
{
$class = $class->first();
}

$class = is_object($class) ? get_class($class) : $class;

return isset($this->transformers[$class]);
Expand Down
2 changes: 2 additions & 0 deletions tests/TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public function testDeterminingIfResponseIsTransformable()
$this->assertFalse($this->transformer->transformableResponse(true));
$this->assertFalse($this->transformer->transformableResponse(false));
$this->assertFalse($this->transformer->transformableResponse(31.1));
$this->assertFalse($this->transformer->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
$this->transformer->transform('Foo', 'Bar');
$this->assertTrue($this->transformer->transformableResponse('Foo'));
$this->assertTrue($this->transformer->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
}


Expand Down

0 comments on commit de5d1df

Please sign in to comment.