Skip to content
This repository was archived by the owner on Mar 29, 2022. It is now read-only.

Commit ac9d8e6

Browse files
author
Changwan Jun
committed
add getMany in Parameter
1 parent dfd6ea0 commit ac9d8e6

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

src/Contracts/ParameterInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public function setFallback(ParameterInterface $fallback);
1414
*/
1515
public function toArray();
1616

17+
/**
18+
* @param array $keyOrDefaults
19+
* @return array
20+
*/
21+
public function getMany(array $keyOrDefaults = []);
22+
1723
/**
1824
* @param string $key
1925
* @param mixed $default

src/Parameters/Parameter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public function toArray()
4545
return $arrayToReturn;
4646
}
4747

48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getMany(array $keyOrDefaults = [])
52+
{
53+
$dataToReturn = [];
54+
foreach ($keyOrDefaults as $key => $value) {
55+
if (is_integer($key)) {
56+
if ($this->has($value)) {
57+
$dataToReturn[$value] = $this->get($value);
58+
}
59+
} else {
60+
$dataToReturn[$key] = $this->get($key, $value);
61+
}
62+
}
63+
return $dataToReturn;
64+
}
65+
4866
/**
4967
* {@inheritdoc}
5068
*/

tests/Contracts/ParameterInterfaceTestTrait.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,48 @@ public function testHasWithFallback()
9191
$this->assertTrue($params->has('fallback'));
9292
$this->assertFalse($params->has('undefined'));
9393
}
94-
}
94+
95+
96+
public function testGetMany()
97+
{
98+
$params = $this->param1;
99+
100+
$this->assertSame(
101+
[
102+
'string' => 'string!',
103+
'number' => '10'
104+
],
105+
$params->getMany(['string', 'number'])
106+
);
107+
108+
$this->assertSame(
109+
[
110+
'string' => 'string!',
111+
],
112+
$params->getMany(['string'])
113+
);
114+
115+
$this->assertSame(
116+
[
117+
'string' => 'string!',
118+
],
119+
$params->getMany(['string', 'unknown'])
120+
);
121+
122+
$this->assertSame(
123+
[
124+
'string' => 'string!',
125+
'unknown' => null,
126+
],
127+
$params->getMany(['string', 'unknown' => null])
128+
);
129+
130+
$this->assertSame(
131+
[
132+
'string' => 'string!',
133+
'unknown' => false,
134+
],
135+
$params->getMany(['string' => false, 'unknown' => false])
136+
);
137+
}
138+
}

tests/Parameters/ParameterTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Mockery;
55
use PHPUnit_Framework_TestCase;
66
use Wandu\Http\Contracts\ParameterInterfaceTestTrait;
7-
use Wandu\Http\Support\CastProviderTrait;
87

98
class ParameterTest extends PHPUnit_Framework_TestCase
109
{
@@ -36,6 +35,5 @@ public function setUp()
3635
'string1' => 'string 1 fallback!',
3736
'fallback' => 'fallback!',
3837
]));
39-
4038
}
4139
}

0 commit comments

Comments
 (0)