Skip to content

Commit

Permalink
add min length rule and it test
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakvGhost committed Nov 17, 2023
1 parent 702a70c commit aabcfa6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LangManager
'validation.string_rule' => "Le champ :attribute doit être une chaîne de caractères.",
'validation.required_rule' => "Le champ :attribute est requis.",
'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."
],
'en' => [
Expand All @@ -37,6 +38,7 @@ class LangManager
'validation.string_rule' => "The :attribute field must be a string.",
'validation.required_rule' => "The :attribute field is required.",
'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 a email.",
],
];
Expand Down
68 changes: 68 additions & 0 deletions src/Rules/MinLengthRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* MinLengthRule - A validation rule implementation for checking if a string field does not exceed a mininum length.
*
* @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 MinLengthRule implements RuleInterface
{
/**
* Name of the field being validated.
*
* @var string
*/
protected $field;

/**
* Constructor of the MinLengthRule class.
*
* @param array $parameters Parameters for the rule, specifying the mininum length.
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check if the length of the given string field does not exceed the specified mininum length.
*
* @param string $field Name of the field being validated.
* @param mixed $value Value of the field being validated.
* @param array $data All validation data.
* @return bool True if the length is within the specified mininum, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->field = $field;

// Get the mininum length from the parameters, defaulting to 0 if not set.
$minLength = $this->parameters[0] ?? 0;

// Check if the value is a string and its length is within the specified mininum.
return is_string($value) && mb_strlen($value) >= $minLength;
}

/**
* Get the validation error message for the max length rule.
*
* @return string Validation error message.
*/
public function message(): string
{
// Use LangManager to get a translated validation error message.
return LangManager::getTranslation('validation.min_length_rule', [
'attribute' => $this->field,
'min' => $this->parameters[0] ?? 0,
]);
}
}
12 changes: 12 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use BlakvGhost\PHPValidator\LangManager;
use BlakvGhost\PHPValidator\Rules\EmailRule;
use BlakvGhost\PHPValidator\Rules\MaxLengthRule;
use BlakvGhost\PHPValidator\Rules\MinLengthRule;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use BlakvGhost\PHPValidator\Rules\StringRule;

Expand Down Expand Up @@ -49,4 +50,15 @@
expect($validator->message())->toBe(
LangManager::getTranslation('validation.string_rule', ['attribute' => 'field'])
);
});

it('validates min length rule successfully', function () {
$validator = new MinLengthRule([10]);

expect($validator->passes('field', 'toolongvalue', []))->toBeTrue();
expect($validator->passes('field', 'less', []))->toBeFalse();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.min_length_rule', ['attribute' => 'field', 'min' => 10])
);
});

0 comments on commit aabcfa6

Please sign in to comment.