Skip to content

Commit

Permalink
Merge pull request #3 from ckrack/develop
Browse files Browse the repository at this point in the history
v0.3
  • Loading branch information
ckrack authored May 26, 2020
2 parents b1fb1af + 1c071e8 commit 3948e7a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ options:
```php
/**
* @Route("/users/{user}")
*
* @ParamConverter("user", options={"optimus" = "user"})
*/
public function getAction(int $user)
Expand All @@ -110,7 +109,8 @@ Based on the example above:

```php
/**
* @Route("/users/{optimus}")
* @Route("/users/{user}")
* @ParamConverter("user")
*/
public function getAction(User $user)
{
Expand Down
16 changes: 13 additions & 3 deletions spec/ParamConverter/OptimusParamConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,26 @@ public function it_decodes_when_optimus_is_in_request()
expect($request->attributes->get('optimus'))->toBe(20);
}

public function it_decodes_when_optimus_is_named_in_ParamConverter_options()
public function it_decodes_when_optimus_is_named_differently_in_ParamConverter_options()
{
$request = new Request([], [], ['controllerArgument' => '1795633817']);
$configuration = new ParamConverter(['options' => ['optimus' => 'controllerArgument']]);
$request = new Request([], [], ['id' => '1795633817']);
$configuration = new ParamConverter(['name' => 'controllerArgument', 'options' => ['optimus' => 'id']]);

$this->supports($configuration)->shouldReturn(true);
$this->apply($request, $configuration)->shouldReturn(true);
expect($request->attributes->get('controllerArgument'))->toBe(20);
}

public function it_decodes_when_optimus_is_named_in_ParamConverter_options()
{
$request = new Request([], [], ['id' => '1795633817']);
$configuration = new ParamConverter(['name' => 'id', 'options' => ['optimus' => 'id']]);

$this->supports($configuration)->shouldReturn(true);
$this->apply($request, $configuration)->shouldReturn(true);
expect($request->attributes->get('id'))->toBe(20);
}

public function it_does_not_decode_when_there_is_no_optimus()
{
$request = new Request([], [], ['controllerArgument' => '1795633817']);
Expand Down
8 changes: 7 additions & 1 deletion src/ParamConverter/OptimusParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function supports(ParamConverter $configuration): bool

private function setOptimus(Request $request, ParamConverter $configuration): void
{
$name = $configuration->getName();

$identifier = $this->getIdentifier(
$request,
array_replace(['optimus' => null], $configuration->getOptions()),
Expand All @@ -53,9 +55,13 @@ private function setOptimus(Request $request, ParamConverter $configuration): vo
return;
}

if ($name == null && $identifier == 'optimus') {
$name = 'optimus';
}

try {
$optimus = $this->optimus->decode((int) $request->attributes->get($identifier));
$request->attributes->set($identifier, $optimus);
$request->attributes->set($name, $optimus);
} catch (\Throwable $th) {
return;
}
Expand Down

0 comments on commit 3948e7a

Please sign in to comment.