Skip to content

Commit 378e92d

Browse files
committed
Merge pull request #5 from ray-di/default
default property
2 parents f76528e + 4b9caa2 commit 378e92d

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

src/Annotation/AbstractWebContextParam.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ abstract class AbstractWebContextParam
2424
* @var string
2525
*/
2626
public $param;
27+
28+
/**
29+
* @var string
30+
*/
31+
public $default;
2732
}

src/WebContextParamInterceptor.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,13 @@ private function getMeta(\ReflectionMethod $method)
7171
foreach ($annotations as $annotation) {
7272
if ($annotation instanceof AbstractWebContextParam) {
7373
$pos = $this->getPos($annotation, $method);
74-
$meta[$pos] = [$annotation::GLOBAL_KEY, $annotation->key];
74+
$meta[$pos] = [$annotation::GLOBAL_KEY, $annotation->key, $annotation->default];
7575
}
7676
}
7777

7878
return $meta;
7979
}
8080

81-
/**
82-
* @param array $meta
83-
* @param int $i
84-
*
85-
* @return array
86-
*/
87-
private function getParam(array $meta, $i)
88-
{
89-
list($globalKey, $key) = $meta[$i];
90-
$webContext = $this->webContext->get($globalKey);
91-
if (isset($webContext[$key])) {
92-
return [true, $webContext[$key]];
93-
}
94-
95-
return [false, null];
96-
}
97-
9881
/**
9982
* @param AbstractWebContextParam $annotation
10083
* @param \ReflectionMethod $method
@@ -124,10 +107,30 @@ private function getPos(AbstractWebContextParam $annotation, \ReflectionMethod $
124107
private function setArg(Arguments $args, array $meta, $i)
125108
{
126109
if (isset($meta[$i]) && (! isset($args[$i]))) {
127-
list($hasParam, $param) = $this->getParam($meta, $i);
110+
list($hasParam, $param, $default) = $this->getParam($meta, $i);
128111
if ($hasParam) {
129112
$args[$i] = $param;
130113
}
114+
if ($default) {
115+
$args[$i] = $default;
116+
}
131117
}
132118
}
119+
120+
/**
121+
* @param array $meta
122+
* @param int $i
123+
*
124+
* @return array
125+
*/
126+
private function getParam(array $meta, $i)
127+
{
128+
list($globalKey, $key, $default) = $meta[$i];
129+
$webContext = $this->webContext->get($globalKey);
130+
if (isset($webContext[$key])) {
131+
return [true, $webContext[$key], $default];
132+
}
133+
134+
return [false, null, $default];
135+
}
133136
}

tests/Fake/FakeConsumer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ public function keyOnly($id)
3434
{
3535
$this->id = $id;
3636
}
37+
38+
/**
39+
* @QueryParam(key="id", param="id", default="_deffault_by_interceptor_")
40+
*/
41+
public function useDefault($id)
42+
{
43+
$this->id = $id;
44+
}
3745
}

tests/WebParamInjectInterceptorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function testKeyOnly()
7878
$this->assertSame($expected, $obj->id);
7979
}
8080

81+
public function testDefaultProperty()
82+
{
83+
$obj = new FakeConsumer;
84+
$invocation = $this->factory($obj, 'useDefault', [], new QueryParam, []);
85+
$invocation->proceed();
86+
$expected = '_deffault_by_interceptor_';
87+
$this->assertSame($expected, $obj->id);
88+
}
89+
8190
public function testCookieParam()
8291
{
8392
$this->assertSame('_COOKIE', CookieParam::GLOBAL_KEY);

0 commit comments

Comments
 (0)