-
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.
Merge pull request #3 from v1p3r75/main
Added NotIn, RequiredWith, ValidIp, Json, Url, AlphaNumeric, Boolean, Size (int, string, array, file) rules
- Loading branch information
Showing
11 changed files
with
715 additions
and
3 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,62 @@ | ||
<?php | ||
|
||
/** | ||
* InRule - A validation rule implementation for checking if a value is an alpha numeric. | ||
* | ||
* @package BlakvGhost\PHPValidator\Rules | ||
* @author Fortunatus KIDJE (v1p3r75) | ||
* @github https://github.com/v1p3r75 | ||
*/ | ||
|
||
namespace BlakvGhost\PHPValidator\Rules; | ||
|
||
use BlakvGhost\PHPValidator\LangManager; | ||
|
||
class AlphaNumericRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the InRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the list of valid values. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the value is an alpha numeric. | ||
* | ||
* @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 is an alpha numeric, false otherwise. | ||
*/ | ||
public function passes(string $field, $value, array $data): bool | ||
{ | ||
// Set the field property for use in the message method. | ||
$this->field = $field; | ||
|
||
return ctype_alnum($value); | ||
} | ||
|
||
/** | ||
* Get the validation error message for this rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.alpha_numeric', [ | ||
'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,62 @@ | ||
<?php | ||
|
||
/** | ||
* InRule - A validation rule implementation for checking if a value is a boolean. | ||
* | ||
* @package BlakvGhost\PHPValidator\Rules | ||
* @author Fortunatus KIDJE (v1p3r75) | ||
* @github https://github.com/v1p3r75 | ||
*/ | ||
|
||
namespace BlakvGhost\PHPValidator\Rules; | ||
|
||
use BlakvGhost\PHPValidator\LangManager; | ||
|
||
class BooleanRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the InRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the list of valid values. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the value is a boolean. | ||
* | ||
* @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 is a boolean, false otherwise. | ||
*/ | ||
public function passes(string $field, $value, array $data): bool | ||
{ | ||
// Set the field property for use in the message method. | ||
$this->field = $field; | ||
|
||
return is_bool($value); | ||
} | ||
|
||
/** | ||
* Get the validation error message for this rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.boolean', [ | ||
'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
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,66 @@ | ||
<?php | ||
|
||
/** | ||
* InRule - A validation rule implementation for checking if a value is a valid json. | ||
* | ||
* @package BlakvGhost\PHPValidator\Rules | ||
* @author Fortunatus KIDJE (v1p3r75) | ||
* @github https://github.com/v1p3r75 | ||
*/ | ||
|
||
namespace BlakvGhost\PHPValidator\Rules; | ||
|
||
use BlakvGhost\PHPValidator\LangManager; | ||
|
||
class JsonRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the InRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the list of valid values. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the value is a valid json. | ||
* | ||
* @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 is valid json, false otherwise. | ||
*/ | ||
public function passes(string $field, $value, array $data): bool | ||
{ | ||
// Set the field property for use in the message method. | ||
$this->field = $field; | ||
|
||
if (!empty($value)) { | ||
return is_string($value) && | ||
is_array(json_decode($value, true)) ? true : false; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the validation error message for this rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.json', [ | ||
'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,63 @@ | ||
<?php | ||
|
||
/** | ||
* InRule - A validation rule implementation for checking if a value is not present in a given list of values. | ||
* | ||
* @package BlakvGhost\PHPValidator\Rules | ||
* @author Fortunatus KIDJE (v1p3r75) | ||
* @github https://github.com/v1p3r75 | ||
*/ | ||
|
||
namespace BlakvGhost\PHPValidator\Rules; | ||
|
||
use BlakvGhost\PHPValidator\LangManager; | ||
|
||
class NotInRule implements RuleInterface | ||
{ | ||
/** | ||
* Name of the field being validated. | ||
* | ||
* @var string | ||
*/ | ||
protected $field; | ||
|
||
/** | ||
* Constructor of the InRule class. | ||
* | ||
* @param array $parameters Parameters for the rule, specifying the list of valid values. | ||
*/ | ||
public function __construct(protected array $parameters) | ||
{ | ||
// No specific logic needed in the constructor for this rule. | ||
} | ||
|
||
/** | ||
* Check if the value is not present in the given list of valid values. | ||
* | ||
* @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 is not in the list of valid values, false otherwise. | ||
*/ | ||
public function passes(string $field, $value, array $data): bool | ||
{ | ||
// Set the field property for use in the message method. | ||
$this->field = $field; | ||
|
||
return !in_array($value, $this->parameters); | ||
} | ||
|
||
/** | ||
* Get the validation error message for this rule. | ||
* | ||
* @return string Validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
// Use LangManager to get a translated validation error message. | ||
return LangManager::getTranslation('validation.not_in_rule', [ | ||
'attribute' => $this->field, | ||
'values' => implode(', ', $this->parameters), | ||
]); | ||
} | ||
} |
Oops, something went wrong.