Skip to content

Commit

Permalink
Merge pull request #3 from v1p3r75/main
Browse files Browse the repository at this point in the history
Added NotIn, RequiredWith, ValidIp, Json, Url, AlphaNumeric, Boolean, Size (int, string, array, file) rules
  • Loading branch information
BlakvGhost authored Nov 17, 2023
2 parents d5cdf5f + 4d8bf7a commit ce37d4a
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,28 @@ class LangManager
'validation.rule_not_found' => "La règle de validation ':ruleName' n'existe pas.",
'validation.string_rule' => "Le champ :attribute doit être une chaîne de caractères.",
'validation.required_rule' => "Le champ :attribute est requis.",
'validation.required_with' => "Le champ :attribute est requis avec le champ :value.",
'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.",
'validation.accepted' => "Le champ :attribute doit être accepté.",
'validation.same_rule' => "Le champ :attribute doit être identique au champ :otherAttribute.",
'validation.password_rule' => "Le champ :attribute doit répondre aux critères de mot de passe.",
'validation.numeric_rule' => "Le champ :attribute doit être numérique.",
'validation.alpha_numeric' => "Le champ :attribute doit être alpha numérique.",
'validation.nullable_rule' => "Le champ :attribute peut être nul.",
'validation.in_rule' => "Le champ :attribute doit être l'une des valeurs suivantes :values.",
'validation.not_in_rule' => "Le champ :attribute ne doit pas ê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.",
'validation.boolean' => "Le champ :attribute doit être un booléen.",
'validation.json' => "Le champ :attribute doit être un json valide.",
'validation.url' => "Le champ :attribute doit être un url valide.",
'validation.valid_ip' => "Le champ :attribute doit être une addresse ip valide.",
'validation.size' => "Le champ :attribute doit avoir la longeur réquise :value.",
],
'en' => [
// English translations
Expand All @@ -48,20 +56,28 @@ class LangManager
'validation.rule_not_found' => "Validation rule ':ruleName' not found.",
'validation.string_rule' => "The :attribute field must be a string.",
'validation.required_rule' => "The :attribute field is required.",
'validation.required_with' => "The :attribute field is required along with the :value field.",
'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 an email.",
'validation.accepted' => "The :attribute field must be accepted.",
'validation.same_rule' => "The :attribute field must be identical to the :otherAttribute field.",
'validation.password_rule' => "The :attribute field must meet password requirements.",
'validation.numeric_rule' => "The :attribute field must be numeric.",
'validation.alpha_numeric' => "The :attribute field must be alphanumeric.",
'validation.nullable_rule' => "The :attribute field can be null.",
'validation.in_rule' => "The :attribute field must be one of the following values: :values.",
'validation.not_in_rule' => "The :attribute field must not be one of the following :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.",
'validation.boolean' => "The :attribute field must be a boolean.",
'validation.json' => "The :attribute field must be a valid json.",
'validation.url' => "The :attribute field must be a valid url.",
'validation.valid_ip' => "The :attribute field must be a valid IP address.",
'validation.size' => "The :attribute field must have the required length :value.",
],
];

Expand Down
62 changes: 62 additions & 0 deletions src/Rules/AlphaNumericRule.php
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,
]);
}
}
62 changes: 62 additions & 0 deletions src/Rules/BooleanRule.php
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,
]);
}
}
5 changes: 2 additions & 3 deletions src/Rules/EmailRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ public function __construct(protected array $parameters)
* @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 is required and not empty, false otherwise.
* @return bool True if the field is a valid email, 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 field is set in the data and not empty.
return (bool)filter_var($value, FILTER_VALIDATE_EMAIL);
}

/**
* Get the validation error message for the required rule.
* Get the validation error message for this rule.
*
* @return string Validation error message.
*/
Expand Down
66 changes: 66 additions & 0 deletions src/Rules/JsonRule.php
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,
]);
}
}
63 changes: 63 additions & 0 deletions src/Rules/NotInRule.php
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),
]);
}
}
Loading

0 comments on commit ce37d4a

Please sign in to comment.