Skip to content

Commit a04777d

Browse files
committed
Transformable classes can now be bound by an interface instead of manually registering. Fixes dingo#34.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 4a7f503 commit a04777d

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

src/Transformer/Factory.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(Container $container)
4444
* Register a transformer binding resolver for a class.
4545
*
4646
* @param string $class
47-
* @param string|callable $resolver
47+
* @param string|callable|object $resolver
4848
* @return \Dingo\Api\Transformer\Factory
4949
*/
5050
public function transform($class, $resolver)
@@ -75,7 +75,7 @@ public function transformResponse($response)
7575
*/
7676
public function transformableResponse($response)
7777
{
78-
return $this->transformableType($response) and $this->hasBinding($response);
78+
return $this->transformableType($response) and ($this->hasBinding($response) or $this->boundByContract($response));
7979
}
8080

8181
/**
@@ -92,7 +92,7 @@ public function transformableType($value)
9292
/**
9393
* Resolve a transfomer binding instance.
9494
*
95-
* @param string|callable $resolver
95+
* @param string|callable|object $resolver
9696
* @return mixed
9797
*/
9898
protected function resolveTransformerBinding($resolver)
@@ -101,15 +101,19 @@ protected function resolveTransformerBinding($resolver)
101101
{
102102
return $this->container->make($resolver);
103103
}
104-
105-
return call_user_func($resolver, $this->container);
104+
elseif (is_callable($resolver))
105+
{
106+
return call_user_func($resolver, $this->container);
107+
}
108+
109+
return $resolver;
106110
}
107111

108112
/**
109113
* Get a registered transformer binding.
110114
*
111115
* @param string|object $class
112-
* @return string|callable
116+
* @return string|callable|object
113117
* @throws \RuntimeException
114118
*/
115119
protected function getBinding($class)
@@ -119,6 +123,11 @@ protected function getBinding($class)
119123
return $this->getBindingFromCollection($class);
120124
}
121125

126+
if ($this->boundByContract($class))
127+
{
128+
return $class->getTransformer();
129+
}
130+
122131
$class = is_object($class) ? get_class($class) : $class;
123132

124133
if ( ! $this->hasBinding($class))
@@ -158,6 +167,17 @@ protected function hasBinding($class)
158167
return isset($this->bindings[$class]);
159168
}
160169

170+
/**
171+
* Determine if the class is bound by the transformable contract.
172+
*
173+
* @param string|object $class
174+
* @return bool
175+
*/
176+
protected function boundByContract($class)
177+
{
178+
return is_object($class) and $class instanceof TransformableInterface;
179+
}
180+
161181
/**
162182
* Determine if the instance is a collection.
163183
*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace Dingo\Api\Transformer;
2+
3+
interface TransformableInterface {
4+
5+
/**
6+
* Get the transformer instance.
7+
*
8+
* @return mixed
9+
*/
10+
public function getTransformer();
11+
12+
}

tests/TransformerFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public function testDeterminingIfResponseIsTransformable()
3636
$this->assertFalse($this->transformerFactory->transformableResponse(false));
3737
$this->assertFalse($this->transformerFactory->transformableResponse(31.1));
3838
$this->assertFalse($this->transformerFactory->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
39-
$this->transformerFactory->transform('Foo', 'Bar');
39+
$this->transformerFactory->transform('Foo', 'FooTransformerStub');
4040
$this->assertTrue($this->transformerFactory->transformableResponse('Foo'));
41+
$this->assertTrue($this->transformerFactory->transformableResponse(new Bar));
4142
$this->assertTrue($this->transformerFactory->transformableResponse(new Illuminate\Support\Collection([new Foo, new Foo])));
4243
}
4344

tests/TransformerFractalTransformerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function testTransformingCollectionUsingCallback()
6363
public function testTransformingNestedRelationships()
6464
{
6565
$this->transformerFactory->transform('Bar', 'BarTransformerStub');
66-
$this->container->shouldReceive('make')->once()->with('BarTransformerStub')->andReturn(new BarTransformerStub);
6766
$this->transformerFactory->setRequest(Illuminate\Http\Request::create('/', 'GET', ['include' => 'foo']));
6867
$this->assertEquals(['data' => ['bar' => 'baz', 'foo' => ['data' => ['foo' => 'bar']]], 'embeds' => ['foo']], $this->transformerFactory->transformResponse(new Bar));
6968
}

tests/stubs.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ public function embedFoo(Bar $bar)
136136
}
137137

138138
class Foo {
139-
140-
}
141-
142-
class Bar {
143139

144140
}
145141

142+
class Bar implements Dingo\Api\Transformer\TransformableInterface {
143+
public function getTransformer()
144+
{
145+
return new BarTransformerStub;
146+
}
147+
}

0 commit comments

Comments
 (0)