summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAsko Nõmm <asko@repl.ee>2023-09-25 17:29:52 +0300
committerAsko Nõmm <asko@repl.ee>2023-09-25 17:29:52 +0300
commit57518b03cbf19b8a714d6fcb573f71cb921005e3 (patch)
tree240509fd84f023f289a83577a735fa82fac879f4 /src
parent8ebb9ca9fbce0982e27296d5016d17d8b20962d6 (diff)
Implement `$fieldNames`, a way to overwrite field names to be more human friendly in error messages.
Diffstat (limited to 'src')
-rw-r--r--src/Hird.php22
-rw-r--r--src/Validators/DateFormatValidator.php8
-rw-r--r--src/Validators/EmailValidator.php8
-rw-r--r--src/Validators/LenValidator.php8
-rw-r--r--src/Validators/RequiredValidator.php8
5 files changed, 49 insertions, 5 deletions
diff --git a/src/Hird.php b/src/Hird.php
index d1b85cc..f60eca8 100644
--- a/src/Hird.php
+++ b/src/Hird.php
@@ -47,11 +47,14 @@ class Hird
{
private array $errors = [];
private array $validators = [];
+ private array $fieldNames = [];
public function __construct(
private array $fields,
private array $rules,
+ array $fieldNames = []
) {
+ $this->composeFieldNames($fieldNames);
$this->registerDefaultValidators();
}
@@ -69,6 +72,23 @@ class Hird
}
/**
+ * Composes the field names array.
+ *
+ * @param array $fieldNames
+ * @return void
+ */
+ private function composeFieldNames(array $fieldNames): void
+ {
+ $updatedFieldNames = [];
+
+ foreach ($this->fields as $field => $value) {
+ $updatedFieldNames[$field] = $fieldNames[$field] ?? $field;
+ }
+
+ $this->fieldNames = $updatedFieldNames;
+ }
+
+ /**
* Registers a validator to a `$ruleName`.
*
* @param string $ruleName
@@ -81,7 +101,7 @@ class Hird
$instance = null;
if ($class->getConstructor() !== null) {
- $instance = $class->newInstanceArgs([$this->fields]);
+ $instance = $class->newInstanceArgs([$this->fields, $this->fieldNames]);
} else {
$instance = $class->newInstance();
}
diff --git a/src/Validators/DateFormatValidator.php b/src/Validators/DateFormatValidator.php
index 50312ea..259ffd1 100644
--- a/src/Validators/DateFormatValidator.php
+++ b/src/Validators/DateFormatValidator.php
@@ -13,6 +13,12 @@ namespace Asko\Hird\Validators;
*/
class DateFormatValidator implements Validator
{
+ public function __construct(
+ private array $fields,
+ private array $fieldNames,
+ ) {
+ }
+
/**
* Returns a boolean `true` when given `$value` is a valid e-mail
* address. Returns `false` otherwise.
@@ -42,6 +48,6 @@ class DateFormatValidator implements Validator
*/
public function composeError(string $field, mixed $modifier = null): string
{
- return "{$field} does not match the required date format {$modifier}.";
+ return "{$this->fieldNames[$field]} does not match the required date format {$modifier}.";
}
}
diff --git a/src/Validators/EmailValidator.php b/src/Validators/EmailValidator.php
index bbb237f..fac4c04 100644
--- a/src/Validators/EmailValidator.php
+++ b/src/Validators/EmailValidator.php
@@ -12,6 +12,12 @@ namespace Asko\Hird\Validators;
*/
class EmailValidator implements Validator
{
+ public function __construct(
+ private array $fields,
+ private array $fieldNames,
+ ) {
+ }
+
/**
* Returns a boolean `true` when given `$value` is a valid e-mail
* address. Returns `false` otherwise.
@@ -34,6 +40,6 @@ class EmailValidator implements Validator
*/
public function composeError(string $field, mixed $modifier = null): string
{
- return "{$field} is not a valid e-mail address.";
+ return "{$this->fieldNames[$field]} is not a valid e-mail address.";
}
}
diff --git a/src/Validators/LenValidator.php b/src/Validators/LenValidator.php
index ca4e486..3fa6fbf 100644
--- a/src/Validators/LenValidator.php
+++ b/src/Validators/LenValidator.php
@@ -12,6 +12,12 @@ namespace Asko\Hird\Validators;
*/
class LenValidator implements Validator
{
+ public function __construct(
+ private array $fields,
+ private array $fieldNames,
+ ) {
+ }
+
/**
* Returns a boolean `true` when given `$value` is as long as
* required. Returns `false` otherwise.
@@ -43,6 +49,6 @@ class LenValidator implements Validator
*/
public function composeError(string $field, mixed $modifier = null): string
{
- return "{$field} is shorter than the required {$modifier} characters.";
+ return "{$this->fieldNames[$field]} is shorter than the required {$modifier} characters.";
}
}
diff --git a/src/Validators/RequiredValidator.php b/src/Validators/RequiredValidator.php
index 48bc28b..404b189 100644
--- a/src/Validators/RequiredValidator.php
+++ b/src/Validators/RequiredValidator.php
@@ -12,6 +12,12 @@ namespace Asko\Hird\Validators;
*/
class RequiredValidator implements Validator
{
+ public function __construct(
+ private array $fields,
+ private array $fieldNames,
+ ) {
+ }
+
/**
* Returns a boolean `true` when given `$value` is present
* and not empty. Returns `false` otherwise.
@@ -34,6 +40,6 @@ class RequiredValidator implements Validator
*/
public function composeError(string $field, mixed $modifier = null): string
{
- return "{$field} is required.";
+ return "{$this->fieldNames[$field]} is required.";
}
}