Skip to content

Commit

Permalink
Fix #10 support for invokable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
fesor committed Jan 28, 2018
1 parent d9874ef commit d35b716
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/RequestObjectBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ private function matchActionArguments(callable $action)
if (is_array($action)) {
$classReflection = new \ReflectionClass($action[0]);
$actionReflection = $classReflection->getMethod($action[1]);
} else {
} elseif ($action instanceof \Closure || is_string($action)) {
$actionReflection = new \ReflectionFunction($action);
}
else {
$classReflection = new \ReflectionClass($action);
$actionReflection = $classReflection->getMethod('__invoke');
}

$matchedArguments = [];
$arguments = $actionReflection->getParameters();
Expand Down
13 changes: 13 additions & 0 deletions tests/RequestBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public function testRequestObjectBindingOnClosure()
self::assertInstanceOf(RequestObject\RequestObject::class, $this->request->attributes->get('requestObj'));
}

public function testRequestObjectBindingOnInvokableObject()
{
$action = new class() {
public function __invoke(RegisterUserRequest $requestObj)
{
}
};

(new RequestObjectBinder($this->payloadResolver, $this->validator))->bind($this->request, $action);
self::assertTrue($this->request->attributes->has('requestObj'));
self::assertInstanceOf(RequestObject\RequestObject::class, $this->request->attributes->get('requestObj'));
}

public function testPassErrorsToAction()
{
$this->validRequest();
Expand Down

0 comments on commit d35b716

Please sign in to comment.