-
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.
- Loading branch information
1 parent
aabcfa6
commit 777f26b
Showing
12 changed files
with
865 additions
and
6 deletions.
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,73 @@ | ||
<?php | ||
|
||
/** | ||
* AcceptedIfRule - A validation rule implementation for checking if a value is considered "accepted" based on another field. | ||
* | ||
* @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 AcceptedIfRule implements RuleInterface | ||
{ | ||
/** | ||
* The name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* The other field that determines if the value is considered "accepted". | ||
* | ||
* @var string | ||
*/ | ||
protected $otherField; | ||
|
||
/** | ||
* Constructor of the AcceptedIfRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the other field. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// Set the other field from the parameters. | ||
$this->otherField = $parameters[0] ?? ''; | ||
} | ||
|
||
/** | ||
* Check if the given value is considered "accepted" based on another field. | ||
* | ||
* @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 considered "accepted", 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 other field is equal to a truthy value. | ||
return $data[$this->otherField] == true; | ||
} | ||
|
||
/** | ||
* Get the validation error message for the accepted if rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.accepted_if', [ | ||
'attribute' => $this->field, | ||
'other' => $this->otherField, | ||
]); | ||
} | ||
} |
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 | ||
|
||
/** | ||
* AcceptedRule - A validation rule implementation for checking if a value is considered "accepted". | ||
* | ||
* @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 AcceptedRule implements RuleInterface | ||
{ | ||
/** | ||
* The name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the AcceptedRule class. | ||
* | ||
* @param array $parameters Parameters for the rule (none needed for this rule). | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the given value is considered "accepted". | ||
* | ||
* @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 considered "accepted", 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 "1", "true", "on", or "yes". | ||
return in_array(strtolower($value), ['1', 'true', 'on', 'yes'], true); | ||
} | ||
|
||
/** | ||
* Get the validation error message for the accepted rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.accepted', [ | ||
'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 | ||
|
||
/** | ||
* ActiveURLRule - A validation rule implementation for checking if a URL is active. | ||
* | ||
* @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 ActiveURLRule implements RuleInterface | ||
{ | ||
/** | ||
* The name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the ActiveURLRule class. | ||
* | ||
* @param array $parameters Parameters for the rule (none needed for this rule). | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the given URL is active. | ||
* | ||
* @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 URL is active, 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 URL and has an active DNS record. | ||
return filter_var($value, FILTER_VALIDATE_URL) !== false && checkdnsrr(parse_url($value, PHP_URL_HOST)); | ||
} | ||
|
||
/** | ||
* Get the validation error message for the active URL rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.active_url', [ | ||
'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 | ||
|
||
/** | ||
* AlphaRule - A validation rule implementation for checking if a value contains only alphabetic characters. | ||
* | ||
* @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 AlphaRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the AlphaRule class. | ||
* | ||
* @param array $parameters No specific parameters needed for this rule. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the value contains only alphabetic characters. | ||
* | ||
* @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 value contains only alphabetic characters, 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 string and contains only alphabetic characters. | ||
return is_string($value) && ctype_alpha($value); | ||
} | ||
|
||
/** | ||
* Get the validation error message for the alpha rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.alpha_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,65 @@ | ||
<?php | ||
|
||
/** | ||
* ConfirmedRule - A validation rule implementation for checking if a field's value is confirmed by another field. | ||
* | ||
* @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 ConfirmedRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the ConfirmedRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the confirmation field name. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the field's value is confirmed by another field. | ||
* | ||
* @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 field's value is confirmed, 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 confirmation field is present and its value matches the field's value. | ||
return isset($data[$this->parameters[0]]) && $value === $data[$this->parameters[0]]; | ||
} | ||
|
||
/** | ||
* Get the validation error message for the confirmed rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.confirmed_rule', [ | ||
'attribute' => $this->field, | ||
'confirmedAttribute' => $this->parameters[0] ?? '', | ||
]); | ||
} | ||
} |
Oops, something went wrong.