From 70c0b80ca5a8c51a2f84cf7d81e67dafdb4f8f86 Mon Sep 17 00:00:00 2001 From: Stephan Huber Date: Sun, 14 Aug 2022 13:34:05 +0200 Subject: [PATCH] feat(Validation): Add support for hierarchical keys using dot-notation --- src/Validation/ValidationService.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Validation/ValidationService.php b/src/Validation/ValidationService.php index 336b22da..b816d428 100644 --- a/src/Validation/ValidationService.php +++ b/src/Validation/ValidationService.php @@ -3,6 +3,7 @@ namespace Phabalicious\Validation; +use Phabalicious\Utilities\Utilities; use Phabalicious\Validation\ValidationErrorBagInterface; class ValidationService @@ -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; } @@ -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); } } @@ -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) + ) ); } } @@ -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; }