Skip to content

[11.x] Add typed getters for configuration #50140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/Illuminate/Config/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;

class Repository implements ArrayAccess, ConfigContract
{
Expand Down Expand Up @@ -77,6 +78,106 @@ public function getMany($keys)
return $config;
}

/**
* Get the specified string configuration value.
*
* @param string $key
* @param mixed $default
* @return string
*/
public function string(string $key, $default = null): string
{
$value = $this->get($key, $default);

if (! is_string($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))
);
}

return $value;
}

/**
* Get the specified integer configuration value.
*
* @param string $key
* @param mixed $default
* @return int
*/
public function integer(string $key, $default = null): int
{
$value = $this->get($key, $default);

if (! is_int($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value))
);
}

return $value;
}

/**
* Get the specified float configuration value.
*
* @param string $key
* @param mixed $default
* @return float
*/
public function float(string $key, $default = null): float
{
$value = $this->get($key, $default);

if (! is_float($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a float, %s given.', $key, gettype($value))
);
}

return $value;
}

/**
* Get the specified boolean configuration value.
*
* @param string $key
* @param mixed $default
* @return bool
*/
public function boolean(string $key, $default = null): bool
{
$value = $this->get($key, $default);

if (! is_bool($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a boolean, %s given.', $key, gettype($value))
);
}

return $value;
}

/**
* Get the specified array configuration value.
*
* @param string $key
* @param mixed $default
* @return array<array-key, mixed>
*/
public function array(string $key, $default = null): array
{
$value = $this->get($key, $default);

if (! is_array($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value))
);
}

return $value;
}

/**
* Set a given configuration value.
*
Expand Down
78 changes: 78 additions & 0 deletions tests/Config/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Config;

use Illuminate\Config\Repository;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class RepositoryTest extends TestCase
Expand All @@ -25,6 +26,8 @@ protected function setUp(): void
'baz' => 'bat',
'null' => null,
'boolean' => true,
'integer' => 1,
'float' => 1.1,
'associate' => [
'x' => 'xxx',
'y' => 'yyy',
Expand Down Expand Up @@ -252,4 +255,79 @@ public function testsItIsMacroable()

$this->assertSame('macroable', $this->repository->foo());
}

public function testItGetsAsString(): void
{
$this->assertSame(
$this->repository->string('a.b'), 'c'
);
}

public function testItThrowsAnExceptionWhenTryingToGetNonStringValueAsString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Configuration value for key \[a\] must be a string, (.*) given.#');

$this->repository->string('a');
}

public function testItGetsAsArray(): void
{
$this->assertSame(
$this->repository->array('array'), ['aaa', 'zzz']
);
}

public function testItThrowsAnExceptionWhenTryingToGetNonArrayValueAsArray(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be an array, (.*) given.#');

$this->repository->array('a.b');
}

public function testItGetsAsBoolean(): void
{
$this->assertTrue(
$this->repository->boolean('boolean')
);
}

public function testItThrowsAnExceptionWhenTryingToGetNonBooleanValueAsBoolean(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be a boolean, (.*) given.#');

$this->repository->boolean('a.b');
}

public function testItGetsAsInteger(): void
{
$this->assertSame(
$this->repository->integer('integer'), 1
);
}

public function testItThrowsAnExceptionWhenTryingToGetNonIntegerValueAsInteger(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#Configuration value for key \[a.b\] must be an integer, (.*) given.#');

$this->repository->integer('a.b');
}

public function testItGetsAsFloat(): void
{
$this->assertSame(
$this->repository->float('float'), 1.1
);
}

public function testItThrowsAnExceptionWhenTryingToGetNonFloatValueAsFloat(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Configuration value for key \[a.b\] must be a float, (.*) given.#');

$this->repository->float('a.b');
}
}