diff --git a/src/ParamResolver.php b/src/ParamResolver.php index 1b3f7e8..bc53ee0 100644 --- a/src/ParamResolver.php +++ b/src/ParamResolver.php @@ -137,14 +137,9 @@ private function resolveString(string $value, array $resolving = []): mixed return $match[0]; } - $resolved = $this->get($key); - - if (!(is_numeric($resolved) || is_string($resolved))) { - throw new ParamResolverException("A string value must be composed of strings and/or numbers."); - } + $resolved = Validator::validateString($this->get($key)); $resolving[$key] = true; - $resolved = (string)$resolved; return $this->resolveString($resolved, $resolving); }, $value); @@ -195,11 +190,8 @@ private function unescapeValue(mixed $value): mixed private function get(int|string $propertyKey): mixed { $value = $this->findValue($propertyKey, $this->config); - if (!$value->valid()) { - throw new ParamResolverException("Parameter '$propertyKey' not found."); - } - return $value->current(); + return Validator::validateGenerator($value, $propertyKey)->current(); } /** @@ -237,13 +229,7 @@ private function parseEnvironmentParams(string $value): ?string if (!str_starts_with($value, 'env.')) { return null; } - $env = substr($value, 4); - $envParam = getenv($env); - - if ($envParam === false) { - throw new ParamResolverException("Environment variable '$env' is not defined."); - } - return $envParam; + return Validator::validateEnvParam(substr($value, 4)); } } diff --git a/src/Validator.php b/src/Validator.php new file mode 100644 index 0000000..44c5ed4 --- /dev/null +++ b/src/Validator.php @@ -0,0 +1,63 @@ +valid()) { + throw new ParamResolverException("Parameter '$key' not found."); + } + + return $value; + } + + /** + * Check if an environment variable exists. + */ + public static function validateEnvParam(string $env): string + { + $envParam = getenv($env); + + if ($envParam === false) { + throw new ParamResolverException("Environment variable '$env' is not defined."); + } + + return $envParam; + } +}