Skip to content

Commit 0bdd0af

Browse files
committed
Allow the third and fourth parameters to be interchangeable for a cleaner API. More test coverage for the transformer factory.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 771f81d commit 0bdd0af

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/Transformer/Binding.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function resolveTransformer()
8888
*
8989
* @return void
9090
*/
91-
public function fireCallback($parameters)
91+
public function fireCallback($parameters = null)
9292
{
9393
if (is_callable($this->callback)) {
9494
call_user_func_array($this->callback, func_get_args());

src/Transformer/Factory.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,23 @@ public function __construct(Container $container, Adapter $adapter)
5151
*
5252
* @param string $class
5353
* @param string|callable|object $resolver
54-
* @param array $parameters
55-
* @param \Closure $after
54+
* @param array|\Closure $third
55+
* @param \Closure $fourth
5656
*
5757
* @return \Dingo\Api\Transformer\Binding
5858
*/
59-
public function register($class, $resolver, array $parameters = [], Closure $after = null)
59+
public function register($class, $resolver, $third = null, $fourth = null)
6060
{
61+
if (func_num_args() == 4) {
62+
list($parameters, $after) = array_slice(func_get_args(), 2);
63+
} elseif (is_array($third)) {
64+
list($parameters, $after) = [$third, null];
65+
} elseif ($third instanceof Closure) {
66+
list($parameters, $after) = [[], $third];
67+
} else {
68+
list($parameters, $after) = [[], null];
69+
}
70+
6171
return $this->bindings[$class] = $this->createBinding($resolver, $parameters, $after);
6272
}
6373

tests/Transformer/FactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ public function testResponseIsTransformable()
3636
$this->assertTrue($this->factory->transformableResponse(new UserStub, new UserTransformerStub));
3737
}
3838

39+
public function testRegisterParameterOrder()
40+
{
41+
// Third parameter is parameters and fourth is callback.
42+
$binding = $this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub, ['foo' => 'bar'], function ($foo) {
43+
$this->assertEquals('foo', $foo);
44+
});
45+
46+
$binding->fireCallback('foo');
47+
$this->assertEquals(['foo' => 'bar'], $binding->getParameters());
48+
49+
// Third parameter is parameters and fourth is null.
50+
$binding = $this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub, ['foo' => 'bar']);
51+
52+
$this->assertEquals(['foo' => 'bar'], $binding->getParameters());
53+
54+
// Third parameter is callback and fourth is null.
55+
$binding = $this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub, function ($foo) {
56+
$this->assertEquals('foo', $foo);
57+
});
58+
59+
$binding->fireCallback('foo');
60+
}
61+
3962
public function testResponseIsTransformableType()
4063
{
4164
$this->assertFalse($this->factory->transformableType(['foo' => 'bar']));

0 commit comments

Comments
 (0)