Skip to content

Commit 6802941

Browse files
[11.x] Add typed getters for configuration (#50140)
* feat: add typed getters * refactor: use builtin type functions instead of webmozart assertions * style: fix for styleci * feat(config): add missing methods in contract * fix: remove contract from methods and add phpdoc * style: remove doubled empty lines * formatting * import class * add default values --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8648a4a commit 6802941

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

src/Illuminate/Config/Repository.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Config\Repository as ConfigContract;
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Traits\Macroable;
9+
use InvalidArgumentException;
910

1011
class Repository implements ArrayAccess, ConfigContract
1112
{
@@ -77,6 +78,106 @@ public function getMany($keys)
7778
return $config;
7879
}
7980

81+
/**
82+
* Get the specified string configuration value.
83+
*
84+
* @param string $key
85+
* @param mixed $default
86+
* @return string
87+
*/
88+
public function string(string $key, $default = null): string
89+
{
90+
$value = $this->get($key, $default);
91+
92+
if (! is_string($value)) {
93+
throw new InvalidArgumentException(
94+
sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))
95+
);
96+
}
97+
98+
return $value;
99+
}
100+
101+
/**
102+
* Get the specified integer configuration value.
103+
*
104+
* @param string $key
105+
* @param mixed $default
106+
* @return int
107+
*/
108+
public function integer(string $key, $default = null): int
109+
{
110+
$value = $this->get($key, $default);
111+
112+
if (! is_int($value)) {
113+
throw new InvalidArgumentException(
114+
sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value))
115+
);
116+
}
117+
118+
return $value;
119+
}
120+
121+
/**
122+
* Get the specified float configuration value.
123+
*
124+
* @param string $key
125+
* @param mixed $default
126+
* @return float
127+
*/
128+
public function float(string $key, $default = null): float
129+
{
130+
$value = $this->get($key, $default);
131+
132+
if (! is_float($value)) {
133+
throw new InvalidArgumentException(
134+
sprintf('Configuration value for key [%s] must be a float, %s given.', $key, gettype($value))
135+
);
136+
}
137+
138+
return $value;
139+
}
140+
141+
/**
142+
* Get the specified boolean configuration value.
143+
*
144+
* @param string $key
145+
* @param mixed $default
146+
* @return bool
147+
*/
148+
public function boolean(string $key, $default = null): bool
149+
{
150+
$value = $this->get($key, $default);
151+
152+
if (! is_bool($value)) {
153+
throw new InvalidArgumentException(
154+
sprintf('Configuration value for key [%s] must be a boolean, %s given.', $key, gettype($value))
155+
);
156+
}
157+
158+
return $value;
159+
}
160+
161+
/**
162+
* Get the specified array configuration value.
163+
*
164+
* @param string $key
165+
* @param mixed $default
166+
* @return array<array-key, mixed>
167+
*/
168+
public function array(string $key, $default = null): array
169+
{
170+
$value = $this->get($key, $default);
171+
172+
if (! is_array($value)) {
173+
throw new InvalidArgumentException(
174+
sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value))
175+
);
176+
}
177+
178+
return $value;
179+
}
180+
80181
/**
81182
* Set a given configuration value.
82183
*

tests/Config/RepositoryTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Config;
44

55
use Illuminate\Config\Repository;
6+
use InvalidArgumentException;
67
use PHPUnit\Framework\TestCase;
78

89
class RepositoryTest extends TestCase
@@ -25,6 +26,8 @@ protected function setUp(): void
2526
'baz' => 'bat',
2627
'null' => null,
2728
'boolean' => true,
29+
'integer' => 1,
30+
'float' => 1.1,
2831
'associate' => [
2932
'x' => 'xxx',
3033
'y' => 'yyy',
@@ -252,4 +255,79 @@ public function testsItIsMacroable()
252255

253256
$this->assertSame('macroable', $this->repository->foo());
254257
}
258+
259+
public function testItGetsAsString(): void
260+
{
261+
$this->assertSame(
262+
$this->repository->string('a.b'), 'c'
263+
);
264+
}
265+
266+
public function testItThrowsAnExceptionWhenTryingToGetNonStringValueAsString(): void
267+
{
268+
$this->expectException(InvalidArgumentException::class);
269+
$this->expectExceptionMessageMatches('#^Configuration value for key \[a\] must be a string, (.*) given.#');
270+
271+
$this->repository->string('a');
272+
}
273+
274+
public function testItGetsAsArray(): void
275+
{
276+
$this->assertSame(
277+
$this->repository->array('array'), ['aaa', 'zzz']
278+
);
279+
}
280+
281+
public function testItThrowsAnExceptionWhenTryingToGetNonArrayValueAsArray(): void
282+
{
283+
$this->expectException(InvalidArgumentException::class);
284+
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be an array, (.*) given.#');
285+
286+
$this->repository->array('a.b');
287+
}
288+
289+
public function testItGetsAsBoolean(): void
290+
{
291+
$this->assertTrue(
292+
$this->repository->boolean('boolean')
293+
);
294+
}
295+
296+
public function testItThrowsAnExceptionWhenTryingToGetNonBooleanValueAsBoolean(): void
297+
{
298+
$this->expectException(InvalidArgumentException::class);
299+
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be a boolean, (.*) given.#');
300+
301+
$this->repository->boolean('a.b');
302+
}
303+
304+
public function testItGetsAsInteger(): void
305+
{
306+
$this->assertSame(
307+
$this->repository->integer('integer'), 1
308+
);
309+
}
310+
311+
public function testItThrowsAnExceptionWhenTryingToGetNonIntegerValueAsInteger(): void
312+
{
313+
$this->expectException(InvalidArgumentException::class);
314+
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be an integer, (.*) given.#');
315+
316+
$this->repository->integer('a.b');
317+
}
318+
319+
public function testItGetsAsFloat(): void
320+
{
321+
$this->assertSame(
322+
$this->repository->float('float'), 1.1
323+
);
324+
}
325+
326+
public function testItThrowsAnExceptionWhenTryingToGetNonFloatValueAsFloat(): void
327+
{
328+
$this->expectException(InvalidArgumentException::class);
329+
$this->expectExceptionMessageMatches('#^Configuration value for key \[a.b\] must be a float, (.*) given.#');
330+
331+
$this->repository->float('a.b');
332+
}
255333
}

0 commit comments

Comments
 (0)