Skip to content

Commit

Permalink
Added several rules and their tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakvGhost committed Nov 17, 2023
1 parent aabcfa6 commit 777f26b
Show file tree
Hide file tree
Showing 12 changed files with 865 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ class LangManager
'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."
'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.nullable_rule' => "Le champ :attribute peut être nul.",
'validation.in_rule' => "Le champ :attribute doit ê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.",
],
'en' => [
// English translations
Expand All @@ -39,9 +47,18 @@ class LangManager
'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.",
'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.nullable_rule' => "The :attribute field can be null.",
'validation.in_rule' => "The :attribute field must be one of the following values: :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.",
],
];


/**
* Get the current language.
Expand Down
73 changes: 73 additions & 0 deletions src/Rules/AcceptedIf.php
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,
]);
}
}
64 changes: 64 additions & 0 deletions src/Rules/AcceptedRule.php
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,
]);
}
}
64 changes: 64 additions & 0 deletions src/Rules/ActiveURL.php
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,
]);
}
}
64 changes: 64 additions & 0 deletions src/Rules/AlphaRule.php
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,
]);
}
}
65 changes: 65 additions & 0 deletions src/Rules/ConfirmedRule.php
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] ?? '',
]);
}
}
Loading

0 comments on commit 777f26b

Please sign in to comment.