From 3a3539c84d79fc39554a7f6a31f85ce0b5043482 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 9 Dec 2023 14:15:56 +0100 Subject: [PATCH] Add feature to add default language for validation message --- README.md | 34 ++++++++++++++++++++-------------- src/Lang/Lang.php | 4 ++-- src/Lang/LangManager.php | 4 +++- src/Validator.php | 16 ++++++++++++---- tests/Feature/RuleTest.php | 2 +- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d45e7a7..027e221 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ - ## About PHPValidator PHPValidator is a modern PHP library for data validation in your PHP applications. It provides a flexible and extensible way to validate data using predefined rules or by creating custom validation rules. @@ -58,7 +57,8 @@ try { } ``` -You can also customize the validation error messages + +You can also customize the validation error messages or `specify the default language` ```php $data = [ @@ -77,6 +77,9 @@ $validator = new Validator( ], ] ); + + // For the default language + $validator = new Validator($data, $rules, lang: 'fr'); ``` ## Features @@ -87,7 +90,6 @@ $validator = new Validator( - `Multilingual Support`: Customize validation error messages based on the application's language using the `LangManager`. - ## List of Predefined Rules PHPValidator provides a variety of predefined rules that you can use for data validation. Here is a list of some commonly used rules along with examples of their usage: @@ -210,25 +212,27 @@ PHPValidator provides a variety of predefined rules that you can use for data va ```php 'password_confirmation' => 'same:password' ``` + 18. **Max Length Rule** - Specifies the minimum length of a string field. ```php 'username' => 'min:8' - ``` + ``` + 19. **Not In Rule** - Validates that a field's value is not in a specified set. ```php 'value' => 'not_in:foo,bar' - ``` + ``` + 20. **Required With Rule** - Requires the field to be present if another specified field is present. - ```php 'firstname' => 'required_with:lastname', - ``` + ``` 21. **Valid IP Rule** - Validates that a field's value is a valid IP address. @@ -242,21 +246,22 @@ PHPValidator provides a variety of predefined rules that you can use for data va ```php 'config' => 'json', - ``` + ``` 23. **URL Rule** - Validates that a field's value is a valid URL. ```php 'website' => 'url', - ``` + ``` + 24. **Alpha Numeric Rule** - Validates that a field's value contains only alphanumeric characters. ```php 'pseudo' => 'alpha_num', - ``` + ``` 25. **Boolean Rule** @@ -264,7 +269,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va ```php 'is_admin' => 'bool', - ``` + ``` 26. **Size Rule** - Validates that the size of a string, integer, array, or file is equal to a specified value. @@ -276,7 +281,8 @@ PHPValidator provides a variety of predefined rules that you can use for data va 'array' =>'size:7', // count(array) == 7 'file' =>'size:512', // file size (kb) == 512 ] - ``` + ``` + ## Custom Rule In addition to the predefined rules, you can create custom validation rules by implementing the `Rule` Interface. Here's an example of how to create and use a custom rule: @@ -311,6 +317,7 @@ class CustomPasswordRule implements Rule } } ``` + ### Usage in Validator - Use your custom class directly @@ -348,6 +355,7 @@ class CustomPasswordRule implements Rule echo "Validation error: " . $e->getMessage(); } ``` + - Add your custom class to the rules list and use it as if it were native ```php @@ -393,7 +401,6 @@ If you would like to contribute to PHPValidator, please follow our [Contribution - [Kabirou ALASSANE](https://github.com/BlakvGhost) - ## Support For support, you can reach out to me by email at . Feel free to contact me if you have any questions or need assistance with PHPValidator. @@ -401,4 +408,3 @@ For support, you can reach out to me by email at . Fee ## License PHPValidator is open-source software licensed under the MIT license. - diff --git a/src/Lang/Lang.php b/src/Lang/Lang.php index ea01b2b..6ba7182 100644 --- a/src/Lang/Lang.php +++ b/src/Lang/Lang.php @@ -29,7 +29,7 @@ trait Lang 'validation.required_with' => "Le champ :attribute est requis avec le champ :value.", 'validation.max_length_rule' => "Le champ :attribute ne doit pas dépasser :max caractères.", 'validation.min_length_rule' => "Le champ :attribute doit dépasser :min caractères.", - 'validation.email_rule' => "Le champ :attribute doit être un e-mail.", + 'validation.email_rule' => "Le champ :attribute doit être un e-mail valide.", 'validation.accepted' => "Le champ :attribute doit être accepté.", 'validation.accepted_if' => "Le champ :attribute doit être accepté uniquement si :other existe et vrai.", 'validation.same_rule' => "Le champ :attribute doit être identique au champ :otherAttribute.", @@ -60,7 +60,7 @@ trait Lang 'validation.required_with' => "The :attribute field is required along with the :value field.", 'validation.max_length_rule' => "The :attribute field must not exceed :max characters.", 'validation.min_length_rule' => "The :attribute field must exceed :min characters.", - 'validation.email_rule' => "The :attribute field must be an email.", + 'validation.email_rule' => "The :attribute field must be a valid email.", 'validation.accepted' => "The :attribute field must be accepted.", 'validation.accepted_if' => "The :attribute field must be accepted if :other.", 'validation.same_rule' => "The :attribute field must be identical to the :otherAttribute field.", diff --git a/src/Lang/LangManager.php b/src/Lang/LangManager.php index b014855..088213c 100644 --- a/src/Lang/LangManager.php +++ b/src/Lang/LangManager.php @@ -16,6 +16,8 @@ class LangManager use Lang; + static public $lang = 'en'; + /** * Get the current language. * @@ -24,7 +26,7 @@ class LangManager private static function getLocal(): string { // Get the current language from environment variables, defaulting to 'en' (English) if not set. - return $_ENV['local'] ?? 'en'; + return self::$lang ?? $_ENV['local']; } /** diff --git a/src/Validator.php b/src/Validator.php index 7984a8b..2120f18 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -19,7 +19,9 @@ class Validator extends RulesMaped { - private $errors = []; + private array $errors = []; + + private const LANGUAGE = 'en'; /** * Constructor of the Validator class. @@ -31,8 +33,10 @@ class Validator extends RulesMaped public function __construct( private array $data, private array $rules, - private array $messages = [] + private array $messages = [], + private string $lang = self::LANGUAGE, ) { + LangManager::$lang = $this->lang; $this->validateConstructorInputs(); $this->validate(); } @@ -137,11 +141,15 @@ protected function checkPasses(mixed $validator, string $field, ?string $ruleNam protected function validateConstructorInputs() { if (empty($this->data)) { - throw new ValidatorException(LangManager::getTranslation('validation.empty_data')); + throw new ValidatorException( + LangManager::getTranslation('validation.empty_data') + ); } if (empty($this->rules)) { - throw new ValidatorException(LangManager::getTranslation('validation.empty_rules')); + throw new ValidatorException( + LangManager::getTranslation('validation.empty_rules') + ); } } diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index c80709b..b9ce6fe 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -38,7 +38,7 @@ $validator = new Validator(['email' => 'test@example.com'], ['email' => 'email']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['email' => 'invalid-email'], ['email' => 'email']); + $validator = new Validator(['email' => 'invalid-email'], ['email' => 'email'], lang:'fr'); expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['email'][0])->toBe(