Skip to content

Commit

Permalink
Fix Settings::has() not working with entries that pointed to arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed May 4, 2022
1 parent f004318 commit c929968
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Application/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Settings implements SettingsInterface {
*/
private array $envVar = [];

private function getPath(string $entry) {
private function getPathEntry(string $entry) {
$path = explode('.', $entry);
$walk = $this->config;
while (count($path)) {
Expand All @@ -27,11 +27,16 @@ private function getPath(string $entry) {
$walk = $walk[$item];
}

if (is_array($walk)) {
return $walk;
}

private function getPathValue(string $entry) {
$value = $this->getPathEntry($entry);
if ($value === null || is_array($value)) {
return null;
}

return $walk;
return $value;
}

private function normalize(string $entry): string {
Expand Down Expand Up @@ -70,8 +75,8 @@ public function __construct(array $config) {
}

public function has(string $entry): bool {
$value = $this->getPath($entry);
if ($value === null) {
$entry = $this->getPathEntry($entry);
if ($entry === null) {
$varName = $this->normalize($entry);

return isset($this->envVar[$varName]);
Expand All @@ -81,7 +86,7 @@ public function has(string $entry): bool {
}

public function getString(string $entry, string $default = ''): string {
$value = $this->getPath($entry);
$value = $this->getPathValue($entry);
if ($value === null) {
$varName = $this->normalize($entry);
if (isset($this->envVar[$varName])) {
Expand All @@ -95,7 +100,7 @@ public function getString(string $entry, string $default = ''): string {
}

public function getInt(string $entry, int $default = 0): int {
$value = $this->getPath($entry);
$value = $this->getPathValue($entry);
if ($value === null) {
$varName = $this->normalize($entry);
if (isset($this->envVar[$varName])) {
Expand All @@ -109,7 +114,7 @@ public function getInt(string $entry, int $default = 0): int {
}

public function getFloat(string $entry, float $default = 0.0): float {
$value = $this->getPath($entry);
$value = $this->getPathValue($entry);
if ($value === null) {
$varName = $this->normalize($entry);
if (isset($this->envVar[$varName])) {
Expand All @@ -123,7 +128,7 @@ public function getFloat(string $entry, float $default = 0.0): float {
}

public function getBool(string $entry, bool $default = false): bool {
$value = $this->getPath($entry);
$value = $this->getPathValue($entry);
if ($value === null) {
$varName = $this->normalize($entry);
if (isset($this->envVar[$varName])) {
Expand Down

0 comments on commit c929968

Please sign in to comment.