Skip to content

Commit

Permalink
feat(Validation): Add support for hierarchical keys using dot-notation
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Aug 14, 2022
1 parent 4c15d5e commit 70c0b80
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Validation/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Phabalicious\Validation;

use Phabalicious\Utilities\Utilities;
use Phabalicious\Validation\ValidationErrorBagInterface;

class ValidationService
Expand Down Expand Up @@ -32,7 +33,7 @@ public function getErrorBag() : ValidationErrorBagInterface
}
public function hasKey(string $key, string $message): bool
{
if (!isset($this->config[$key])) {
if (is_null(Utilities::getProperty($this->config, $key, null))) {
$this->errors->addError($key, 'Missing key '. $key . ' in ' . $this->prefixMessage . ': ' . $message);
return false;
}
Expand All @@ -49,7 +50,7 @@ public function hasKeys(array $keys)
public function deprecate(array $keys)
{
foreach ($keys as $key => $message) {
if (isset($this->config[$key])) {
if (!is_null(Utilities::getProperty($this->config, $key, null))) {
$this->errors->addWarning($key, $message);
}
}
Expand All @@ -70,12 +71,20 @@ public function isArray(string $key, string $message)

public function isOneOf(string $key, array $candidates)
{
if ($this->hasKey($key, 'Candidates: ' . implode(', ', $candidates))
&& !in_array($this->config[$key], $candidates)) {
if (!$this->hasKey($key, 'Candidates: ' . implode(', ', $candidates))) {
return false;
}
$value = Utilities::getProperty($this->config, $key);
if (!in_array($value, $candidates)) {
$this->errors->addError(
$key,
'key '. $key . ' has unrecognized value: ' .
$this->config[$key] . ' in ' . $this->prefixMessage . ': Candidates are ' . implode(', ', $candidates)
sprintf(
'key %s has unrecognized value: `%s` in %s: Candidates are %s',
$key,
$value,
$this->prefixMessage,
implode(', ', $candidates)
)
);
}
}
Expand All @@ -85,10 +94,11 @@ public function checkForValidFolderName(string $key)
if (!$this->hasKey($key, 'Missing key')) {
return false;
}
if ($this->config[$key] !== '/' && substr($this->config[$key], -1) === DIRECTORY_SEPARATOR) {
$root_folder = Utilities::getProperty($this->config, $key);
if ($root_folder !== '/' && substr($root_folder, -1) === DIRECTORY_SEPARATOR) {
$this->errors->addError(
$key,
sprintf('key %s is ending with a directory separator, please change!', $key)
sprintf('key `%s` is ending with a directory separator, please change!', $key)
);
return false;
}
Expand Down

0 comments on commit 70c0b80

Please sign in to comment.