Skip to content

Commit de5d1df

Browse files
committed
Correctly detect and transform collections. Fixes #30.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent b17bd08 commit de5d1df

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Transformer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public function transform($class, $transformer)
9191
*/
9292
public function transformResponse($response)
9393
{
94-
$transformer = $this->resolveTransformer($this->getTransformer($response));
95-
9694
$this->setRequestedScopes();
9795

96+
$transformer = $this->resolveTransformer($this->getTransformer($response));
97+
9898
$resource = $this->createResource($response, $transformer);
9999

100100
// If the response is a paginator then we'll create a new paginator
@@ -197,6 +197,11 @@ protected function getTransformer($class)
197197
*/
198198
protected function hasTransformer($class)
199199
{
200+
if ($this->isCollection($class))
201+
{
202+
$class = $class->first();
203+
}
204+
200205
$class = is_object($class) ? get_class($class) : $class;
201206

202207
return isset($this->transformers[$class]);

tests/TransformerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function testDeterminingIfResponseIsTransformable()
3535
$this->assertFalse($this->transformer->transformableResponse(true));
3636
$this->assertFalse($this->transformer->transformableResponse(false));
3737
$this->assertFalse($this->transformer->transformableResponse(31.1));
38+
$this->assertFalse($this->transformer->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
3839
$this->transformer->transform('Foo', 'Bar');
3940
$this->assertTrue($this->transformer->transformableResponse('Foo'));
41+
$this->assertTrue($this->transformer->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
4042
}
4143

4244

0 commit comments

Comments
 (0)