diff --git a/CHANGELOG.md b/CHANGELOG.md index 27ea28dd..d1a12fb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Removed Assets - Update to Laravel 10 - Update to PHPUnit 10 +- [Config] Methods `getBool`, `getString`, `getInt` & `getArray` now return `null` if key doesn't exist, to make it on par with parent `get` method. ### Alert - Messages are now translated at read time ([#1156](https://github.com/userfrosting/UserFrosting/pull/1156), [#811](https://github.com/userfrosting/UserFrosting/issues/811)). Messages will be translated when using `messages` and `getAndClearMessages`. `addMessage` now accept the optional placeholders, which will be stored with the alert message. `addMessageTranslated` is **deprecated**. diff --git a/src/Config/Config.php b/src/Config/Config.php index 85ce7fd0..b33a46f2 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -25,13 +25,13 @@ class Config extends Repository * @param string $key * @param bool|null $default * - * @return bool + * @return bool|null Returns null if the key is not found */ - public function getBool(string $key, ?bool $default = null): bool + public function getBool(string $key, ?bool $default = null): ?bool { $value = $this->get($key, $default); - if (!is_bool($value)) { + if (!is_bool($value) && !is_null($value)) { throw new TypeException("Config key '$key' doesn't return bool."); } @@ -44,13 +44,13 @@ public function getBool(string $key, ?bool $default = null): bool * @param string $key * @param string|null $default * - * @return string + * @return string|null Returns null if the key is not found */ - public function getString(string $key, ?string $default = null): string + public function getString(string $key, ?string $default = null): ?string { $value = $this->get($key, $default); - if (!is_string($value)) { + if (!is_string($value) && !is_null($value)) { throw new TypeException("Config key '$key' doesn't return string."); } @@ -63,13 +63,13 @@ public function getString(string $key, ?string $default = null): string * @param string $key * @param int|null $default * - * @return int + * @return int|null Returns null if the key is not found */ - public function getInt(string $key, ?int $default = null): int + public function getInt(string $key, ?int $default = null): ?int { $value = $this->get($key, $default); - if (!is_int($value)) { + if (!is_int($value) && !is_null($value)) { throw new TypeException("Config key '$key' doesn't return int."); } @@ -82,13 +82,13 @@ public function getInt(string $key, ?int $default = null): int * @param string $key * @param mixed[]|null $default * - * @return mixed[] + * @return mixed[]|null Returns null if the key is not found */ - public function getArray(string $key, ?array $default = null): array + public function getArray(string $key, ?array $default = null): ?array { $value = $this->get($key, $default); - if (!is_array($value)) { + if (!is_array($value) && !is_null($value)) { throw new TypeException("Config key '$key' doesn't return array."); } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index de3903b7..607ef857 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -19,7 +19,7 @@ class ConfigTest extends TestCase 'bool' => true, 'string' => 'foobar', 'int' => 92, - 'array' => [], + 'array' => ['foo', 'bar'], ]; public function testGetBool(): void @@ -27,6 +27,9 @@ public function testGetBool(): void $repo = new Config($this->data); $this->assertSame(true, $repo->getBool('bool')); + $this->assertSame(false, $repo->getBool('missing', false)); + $this->assertSame(null, $repo->getBool('missing')); + $this->assertSame($repo->get('missing'), $repo->getBool('missing')); // Same default behavior as "get" $this->expectException(TypeException::class); $repo->getBool('string'); } @@ -36,6 +39,9 @@ public function testGetString(): void $repo = new Config($this->data); $this->assertSame('foobar', $repo->getString('string')); + $this->assertSame('barfoo', $repo->getString('missing', 'barfoo')); + $this->assertSame(null, $repo->getString('missing')); + $this->assertSame($repo->get('missing'), $repo->getString('missing')); // Same default behavior as "get" $this->expectException(TypeException::class); $repo->getString('bool'); } @@ -45,6 +51,9 @@ public function testGetInt(): void $repo = new Config($this->data); $this->assertSame(92, $repo->getInt('int')); + $this->assertSame(29, $repo->getInt('missing', 29)); + $this->assertSame(null, $repo->getInt('missing')); + $this->assertSame($repo->get('missing'), $repo->getInt('missing')); // Same default behavior as "get" $this->expectException(TypeException::class); $repo->getInt('string'); } @@ -53,7 +62,10 @@ public function testGetArray(): void { $repo = new Config($this->data); - $this->assertSame([], $repo->getArray('array')); + $this->assertSame(['foo', 'bar'], $repo->getArray('array')); + $this->assertSame(['bar', 'foo'], $repo->getArray('missing', ['bar', 'foo'])); + $this->assertSame(null, $repo->getArray('missing')); + $this->assertSame($repo->get('missing'), $repo->getArray('missing')); // Same default behavior as "get" $this->expectException(TypeException::class); $repo->getArray('string'); }