diff --git a/src/LangManager.php b/src/LangManager.php index 846f1bf..0324b53 100644 --- a/src/LangManager.php +++ b/src/LangManager.php @@ -37,6 +37,9 @@ class LangManager 'validation.in_rule' => "Le champ :attribute doit être l'une des valeurs suivantes :values.", 'validation.confirmed_rule' => "Le champ :attribute doit être confirmé par le champ :confirmedAttribute.", 'validation.active_url' => "Le champ :attribute doit être une URL active.", + 'validation.lowercase_rule' => "Le champ :attribute doit être en minuscules.", + 'validation.uppercase_rule' => "Le champ :attribute doit être en majuscules.", + 'validation.file_rule' => "Le champ :attribute doit être un fichier.", ], 'en' => [ // English translations @@ -56,9 +59,12 @@ class LangManager 'validation.in_rule' => "The :attribute field must be one of the following values: :values.", 'validation.confirmed_rule' => "The :attribute field must be confirmed by the :confirmedAttribute field.", 'validation.active_url' => "The :attribute field must be an active URL.", + 'validation.lowercase_rule' => "The :attribute field must be lowercase.", + 'validation.uppercase_rule' => "The :attribute field must be uppercase.", + 'validation.file_rule' => "The :attribute field must be a file.", ], ]; - + /** * Get the current language. diff --git a/src/Rules/FileRule.php b/src/Rules/FileRule.php new file mode 100644 index 0000000..1989873 --- /dev/null +++ b/src/Rules/FileRule.php @@ -0,0 +1,64 @@ +field = $field; + + // Check if the value is a valid file. + return is_file($value); + } + + /** + * Get the validation error message for the file rule. + * + * @return string Validation error message. + */ + public function message(): string + { + // Use LangManager to get a translated validation error message. + return LangManager::getTranslation('validation.file_rule', [ + 'attribute' => $this->field, + ]); + } +} diff --git a/src/Rules/LowerRule.php b/src/Rules/LowerRule.php new file mode 100644 index 0000000..af9034d --- /dev/null +++ b/src/Rules/LowerRule.php @@ -0,0 +1,64 @@ +field = $field; + + // Check if the value is lowercase. + return mb_strtolower($value, 'UTF-8') === $value; + } + + /** + * Get the validation error message for the lowercase rule. + * + * @return string Validation error message. + */ + public function message(): string + { + // Use LangManager to get a translated validation error message. + return LangManager::getTranslation('validation.lowercase_rule', [ + 'attribute' => $this->field, + ]); + } +} diff --git a/src/Rules/UpperRule.php b/src/Rules/UpperRule.php new file mode 100644 index 0000000..e9203cd --- /dev/null +++ b/src/Rules/UpperRule.php @@ -0,0 +1,64 @@ +field = $field; + + // Check if the value is uppercase. + return mb_strtoupper($value, 'UTF-8') === $value; + } + + /** + * Get the validation error message for the uppercase rule. + * + * @return string Validation error message. + */ + public function message(): string + { + // Use LangManager to get a translated validation error message. + return LangManager::getTranslation('validation.uppercase_rule', [ + 'attribute' => $this->field, + ]); + } +} diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 63004c1..1facab1 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -7,7 +7,10 @@ use BlakvGhost\PHPValidator\Rules\AlphaRule; use BlakvGhost\PHPValidator\Rules\ConfirmedRule; use BlakvGhost\PHPValidator\Rules\EmailRule; +use BlakvGhost\PHPValidator\Rules\FileRule; use BlakvGhost\PHPValidator\Rules\InRule; +use BlakvGhost\PHPValidator\Rules\LowercaseRule; +use BlakvGhost\PHPValidator\Rules\LowerRule; use BlakvGhost\PHPValidator\Rules\StringRule; use BlakvGhost\PHPValidator\Rules\RequiredRule; use BlakvGhost\PHPValidator\Rules\MaxLengthRule; @@ -16,6 +19,7 @@ use BlakvGhost\PHPValidator\Rules\NumericRule; use BlakvGhost\PHPValidator\Rules\PasswordRule; use BlakvGhost\PHPValidator\Rules\SameRule; +use BlakvGhost\PHPValidator\Rules\UpperRule; it('validates required rule successfully', function () { $validator = new RequiredRule([]); @@ -234,3 +238,42 @@ ]) ); }); + +it('validates lowercase rule successfully', function () { + $validator = new LowerRule([]); + + expect($validator->passes('field', 'lowercase', []))->toBeTrue(); + expect($validator->passes('field', 'UPPERCASE', []))->toBeFalse(); + + expect($validator->message())->toBe( + LangManager::getTranslation('validation.lowercase_rule', [ + 'attribute' => 'field', + ]) + ); +}); + +it('validates uppercase rule successfully', function () { + $validator = new UpperRule([]); + + expect($validator->passes('field', 'lowercase', []))->toBeFalse(); + expect($validator->passes('field', 'UPPERCASE', []))->toBeTrue(); + + expect($validator->message())->toBe( + LangManager::getTranslation('validation.uppercase_rule', [ + 'attribute' => 'field', + ]) + ); +}); + +it('validates file rule successfully', function () { + $validator = new FileRule([]); + + expect($validator->passes('field', __FILE__, []))->toBeTrue(); + expect($validator->passes('field', 'nonexistentfile.txt', []))->toBeFalse(); + + expect($validator->message())->toBe( + LangManager::getTranslation('validation.file_rule', [ + 'attribute' => 'field', + ]) + ); +}); \ No newline at end of file