From 57518b03cbf19b8a714d6fcb573f71cb921005e3 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Mon, 25 Sep 2023 17:29:52 +0300 Subject: Implement `$fieldNames`, a way to overwrite field names to be more human friendly in error messages. --- README.md | 11 ++++++++++- src/Hird.php | 22 +++++++++++++++++++++- src/Validators/DateFormatValidator.php | 8 +++++++- src/Validators/EmailValidator.php | 8 +++++++- src/Validators/LenValidator.php | 8 +++++++- src/Validators/RequiredValidator.php | 8 +++++++- tests/HirdTest.php | 18 ++++++++++++++++++ 7 files changed, 77 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0eec3d4..1e39400 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,9 @@ An example usage of Hird looks like this: use Askonomm\Hird\Hird; $fields = ['email' => 'asko@asko.dev']; +$fieldNames = ['email' => 'E-mail']; $rules = ['email' => 'required|email|len:5']; -$hird = new Hird($fields, $rules); +$hird = new Hird($fields, $rules, $fieldNames); if ($hird->fails()) { return $hird->errors(); @@ -48,6 +49,8 @@ You can also get the first error rather than all errors by using the method `$hi If you wish to run the validation without needing to call `$hird->fails()`, you can instead call `$hird->validate()`. +Another thing you may notice is the presence of `$fieldNames`, which is a way overwriting the field names for use within the error messages, so that `email` could become `E-mail` when shown to the user. If you don't care about this then you can entirely skip this as only the `$fields` and `$rules` are required for Hird to work. + ## Built-in validators There are a number of built-in validators available for use by default. If you want to remove a built-in validator, you can remove one using the `$hird->removeValidator('rule-name')` method. @@ -113,6 +116,12 @@ use Asko\Hird\Validators\Validator; class EmailValidator implements Validator { + public function __construct( + private array $fields, // all fields data + private array $fieldNames, // names of fields + ){ + } + /** * Returns a boolean `true` when given `$value` is a valid e-mail * address. Returns `false` otherwise. 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(); } @@ -68,6 +71,23 @@ class Hird $this->registerValidator('date-format', DateFormatValidator::class); } + /** + * 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`. * @@ -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."; } } diff --git a/tests/HirdTest.php b/tests/HirdTest.php index 9e49467..35f4895 100644 --- a/tests/HirdTest.php +++ b/tests/HirdTest.php @@ -88,3 +88,21 @@ test('Validate an incorrect correct date format', function () { 'date does not match the required date format Y-m-d H:i:s.', ]); }); + +test('Validate using a overwritten field name', function () { + $fields = ['date' => '2020-09-17 15:00']; + $fieldNames = ['date' => 'Date']; + $rules = ['date' => 'date-format:Y-m-d H:i:s']; + + $hird = new Hird( + fields: $fields, + rules: $rules, + fieldNames: $fieldNames + ); + + $hird->fails(); + + expect($hird->errors())->toBe([ + 'Date does not match the required date format Y-m-d H:i:s.', + ]); +}); -- cgit v1.2.3