Skip to content

Commit

Permalink
add lowercase, uppercase and file rule and their tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakvGhost committed Nov 17, 2023
1 parent 19f33ae commit d6fed37
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
64 changes: 64 additions & 0 deletions src/Rules/FileRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* FileRule - A validation rule implementation for checking if a field's value is a valid file.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;

class FileRule implements RuleInterface
{
/**
* The name of the field being validated.
*
* @var string
*/
protected $field;

/**
* Constructor of the FileRule class.
*
* @param array $parameters Parameters for the rule (not used in this rule).
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check if the field's value is a valid file.
*
* @param string $field The name of the field being validated.
* @param mixed $value The value of the field being validated.
* @param array $data All validation data.
* @return bool True if the value is a valid file, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->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,
]);
}
}
64 changes: 64 additions & 0 deletions src/Rules/LowerRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* LowercaseRule - A validation rule implementation for checking if a field's value is lowercase.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;

class LowerRule implements RuleInterface
{
/**
* The name of the field being validated.
*
* @var string
*/
protected $field;

/**
* Constructor of the LowercaseRule class.
*
* @param array $parameters Parameters for the rule (not used in this rule).
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check if the field's value is lowercase.
*
* @param string $field The name of the field being validated.
* @param mixed $value The value of the field being validated.
* @param array $data All validation data.
* @return bool True if the value is lowercase, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->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,
]);
}
}
64 changes: 64 additions & 0 deletions src/Rules/UpperRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* UppercaseRule - A validation rule implementation for checking if a field's value is uppercase.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;

class UpperRule implements RuleInterface
{
/**
* The name of the field being validated.
*
* @var string
*/
protected $field;

/**
* Constructor of the UppercaseRule class.
*
* @param array $parameters Parameters for the rule (not used in this rule).
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check if the field's value is uppercase.
*
* @param string $field The name of the field being validated.
* @param mixed $value The value of the field being validated.
* @param array $data All validation data.
* @return bool True if the value is uppercase, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->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,
]);
}
}
43 changes: 43 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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([]);
Expand Down Expand Up @@ -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',
])
);
});

0 comments on commit d6fed37

Please sign in to comment.