Skip to content

Commit

Permalink
Merge pull request #5 from ray-di/default
Browse files Browse the repository at this point in the history
default property
  • Loading branch information
koriym committed Mar 23, 2016
2 parents f76528e + 4b9caa2 commit 378e92d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/Annotation/AbstractWebContextParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ abstract class AbstractWebContextParam
* @var string
*/
public $param;

/**
* @var string
*/
public $default;
}
41 changes: 22 additions & 19 deletions src/WebContextParamInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,13 @@ private function getMeta(\ReflectionMethod $method)
foreach ($annotations as $annotation) {
if ($annotation instanceof AbstractWebContextParam) {
$pos = $this->getPos($annotation, $method);
$meta[$pos] = [$annotation::GLOBAL_KEY, $annotation->key];
$meta[$pos] = [$annotation::GLOBAL_KEY, $annotation->key, $annotation->default];
}
}

return $meta;
}

/**
* @param array $meta
* @param int $i
*
* @return array
*/
private function getParam(array $meta, $i)
{
list($globalKey, $key) = $meta[$i];
$webContext = $this->webContext->get($globalKey);
if (isset($webContext[$key])) {
return [true, $webContext[$key]];
}

return [false, null];
}

/**
* @param AbstractWebContextParam $annotation
* @param \ReflectionMethod $method
Expand Down Expand Up @@ -124,10 +107,30 @@ private function getPos(AbstractWebContextParam $annotation, \ReflectionMethod $
private function setArg(Arguments $args, array $meta, $i)
{
if (isset($meta[$i]) && (! isset($args[$i]))) {
list($hasParam, $param) = $this->getParam($meta, $i);
list($hasParam, $param, $default) = $this->getParam($meta, $i);
if ($hasParam) {
$args[$i] = $param;
}
if ($default) {
$args[$i] = $default;
}
}
}

/**
* @param array $meta
* @param int $i
*
* @return array
*/
private function getParam(array $meta, $i)
{
list($globalKey, $key, $default) = $meta[$i];
$webContext = $this->webContext->get($globalKey);
if (isset($webContext[$key])) {
return [true, $webContext[$key], $default];
}

return [false, null, $default];
}
}
8 changes: 8 additions & 0 deletions tests/Fake/FakeConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public function keyOnly($id)
{
$this->id = $id;
}

/**
* @QueryParam(key="id", param="id", default="_deffault_by_interceptor_")
*/
public function useDefault($id)
{
$this->id = $id;
}
}
9 changes: 9 additions & 0 deletions tests/WebParamInjectInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public function testKeyOnly()
$this->assertSame($expected, $obj->id);
}

public function testDefaultProperty()
{
$obj = new FakeConsumer;
$invocation = $this->factory($obj, 'useDefault', [], new QueryParam, []);
$invocation->proceed();
$expected = '_deffault_by_interceptor_';
$this->assertSame($expected, $obj->id);
}

public function testCookieParam()
{
$this->assertSame('_COOKIE', CookieParam::GLOBAL_KEY);
Expand Down

0 comments on commit 378e92d

Please sign in to comment.