Skip to content

Commit

Permalink
Introduce Validator class
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc72 committed Aug 11, 2024
1 parent a3d5231 commit 55df8ad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/ParamResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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));
}
}
63 changes: 63 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);
/*
* Copyright (c) Cristiano Cinotti 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Susina\ParamResolver;

use Generator;
use Susina\ParamResolver\Exception\ParamResolverException;

/**
* Utility class to perform validations.
*/
class Validator
{
/**
* Check if a value is only numeric or string.
*/
public static function validateString(mixed $value): string
{
if (!(is_numeric($value) || is_string($value))) {
throw new ParamResolverException("A string value must be composed of strings and/or numbers.");
}

return (string) $value;
}

/**
* Check if a generator is correctly closed.
*/
public static function validateGenerator(Generator $value, int|string $key): Generator
{
if (!$value->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;
}
}

0 comments on commit 55df8ad

Please sign in to comment.