-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lowercase, uppercase and file rule and their tests
- Loading branch information
1 parent
19f33ae
commit d6fed37
Showing
5 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters