Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit test format for using Validor class #5

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
162371c
Edit required rule test format to bind from Validator
BlakvGhost Nov 17, 2023
cbf60b1
Edit max length rule test format to bind from Validator
BlakvGhost Nov 17, 2023
3c74099
Merge branch 'main' of https://github.com/BlakvGhost/PHPValidator int…
BlakvGhost Nov 17, 2023
dd27d44
Fix singleton pattern persistance issues for testing
BlakvGhost Nov 17, 2023
286a43f
Edit max length rule test format to bind from Validator
BlakvGhost Nov 18, 2023
5084d35
Edit email rule test format to bind from Validator
BlakvGhost Nov 18, 2023
e3ae859
Edit string rule test format to bind from Validator
BlakvGhost Nov 18, 2023
1030ed6
Edit min length rule test format to bind from Validator
BlakvGhost Nov 18, 2023
cf814d1
Edit alpha length rule test format to bind from Validator
BlakvGhost Nov 18, 2023
fb04daf
Edit accepted if rule test format to bind from Validator
BlakvGhost Nov 18, 2023
0cc4ff6
Edit same rule test format to bind from Validator
BlakvGhost Nov 18, 2023
234a041
Fix accepted if rule test format to bind from Validator
BlakvGhost Nov 18, 2023
c9eb13d
Edit password rule test format to bind from Validator
BlakvGhost Nov 18, 2023
573061c
Edit numeric rule test format to bind from Validator
BlakvGhost Nov 18, 2023
d96e0ed
Edit nullable rule test format to bind from Validator
BlakvGhost Nov 18, 2023
8593bd0
Edit In rule test format to bind from Validator
BlakvGhost Nov 18, 2023
faf22d5
Edit NotIn rule test format to bind from Validator
BlakvGhost Nov 18, 2023
1d09b1b
Edit Confirmed rule test format to bind from Validator
BlakvGhost Nov 18, 2023
aa82de8
Edit ActiveUrl rule test format to bind from Validator
BlakvGhost Nov 18, 2023
afce8b9
Edit LowerCase rule test format to bind from Validator
BlakvGhost Nov 18, 2023
25c1140
Edit File and alpha_numeric rule test format to bind from Validator
BlakvGhost Nov 18, 2023
d0e70ed
Edit json rule test format to bind from Validator
BlakvGhost Nov 18, 2023
956dbd0
Edit url rule test format to bind from Validator
BlakvGhost Nov 18, 2023
cdef811
Edit size rule test format to bind from Validator
BlakvGhost Nov 18, 2023
fef5343
Fix "asserting that two strings are identical" bug on tests
BlakvGhost Nov 18, 2023
20aa771
Fix singleton pattern persistance issues for testing
BlakvGhost Nov 18, 2023
4ad6812
Fix "asserting that two strings are identical" bug on tests
BlakvGhost Nov 18, 2023
75675cf
Fix numeric rule tests failing issues
BlakvGhost Nov 18, 2023
76e0ed0
Fix Validation rule 'active_url' not found issues.
BlakvGhost Nov 18, 2023
d24421a
Fix Validation rule 'active_url' not found issues.
BlakvGhost Nov 18, 2023
69cdb3e
Fix Validation rule 'active_url' not found issues.
BlakvGhost Nov 18, 2023
2d2117a
Fix Validation rule 'active_url' not found issue.
BlakvGhost Nov 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LangManager
'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.accepted_if' => "Le champ :attribute doit être accepté uniquement si :other existe et vrai.",
'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.",
Expand Down Expand Up @@ -61,6 +62,7 @@ class LangManager
'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.accepted_if' => "The :attribute field must be accepted if :other.",
'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.",
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/ActiveURLRule.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* ActiveURLRule - A validation rule implementation for checking if a URL is active.
* ActiveUrlRule - A validation rule implementation for checking if a URL is active.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Kabirou ALASSANE
Expand All @@ -13,7 +13,7 @@

use BlakvGhost\PHPValidator\LangManager;

class ActiveURLRule implements RuleInterface
class ActiveUrlRule implements RuleInterface
{
/**
* The name of the field being validated.
Expand All @@ -23,7 +23,7 @@ class ActiveURLRule implements RuleInterface
protected $field;

/**
* Constructor of the ActiveURLRule class.
* Constructor of the ActiveUrlRule class.
*
* @param array $parameters Parameters for the rule (none needed for this rule).
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/InRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function message(): string
// Use LangManager to get a translated validation error message.
return LangManager::getTranslation('validation.in_rule', [
'attribute' => $this->field,
'values' => implode(', ', $this->parameters),
'values' => implode(',', $this->parameters),
]);
}
}
2 changes: 1 addition & 1 deletion src/Rules/NotInRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ 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),
'values' => implode(',', $this->parameters),
]);
}
}
2 changes: 1 addition & 1 deletion src/Rules/NumericRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function passes(string $field, $value, array $data): bool
$this->field = $field;

// Check if the value is numeric.
return is_numeric($value);
return ($value !== null) & is_numeric($value);
}

/**
Expand Down
34 changes: 18 additions & 16 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

class Validator
{
protected static $errors = [];

private $errors = [];

/**
* Constructor of the Validator class.
*
* @param array $data Data to be validated.
* @param array $rules Validation rules to apply.
*/
public function __construct(private array $data, protected array $rules)
public function __construct(private array $data, private array $rules)
{
$this->validateConstructorInputs();
$this->validate();
Expand Down Expand Up @@ -63,7 +64,7 @@ protected function resolveRuleClass($ruleName): string
$className = implode('', array_map('ucfirst', $ruleParts)) . 'Rule';

$fullClassName = "BlakvGhost\\PHPValidator\\Rules\\$className";

if (!class_exists($fullClassName)) {

$translatedMessage = LangManager::getTranslation('validation.rule_not_found', [
Expand All @@ -84,7 +85,7 @@ protected function resolveRuleClass($ruleName): string
*/
protected function addError($field, $message)
{
self::$errors[$field][] = $message;
$this->errors[$field][] = $message;
}

/**
Expand All @@ -103,15 +104,15 @@ protected function validate()
foreach ($rulesArray as $rule) {

if (is_a($rule, RuleInterface::class, true)) {
return $this->checkPasses($rule, $field);
}
$this->checkPasses($rule, $field);
} else {
list($ruleName, $parameters) = $this->parseRule($rule);
$ruleClass = $this->resolveRuleClass($ruleName);

list($ruleName, $parameters) = $this->parseRule($rule);
$ruleClass = $this->resolveRuleClass($ruleName);
$validator = new $ruleClass($parameters);

$validator = new $ruleClass($parameters);

$this->checkPasses($validator, $field);
$this->checkPasses($validator, $field);
}
}
}
}
Expand All @@ -124,11 +125,12 @@ protected function validate()
*/
protected function checkPasses(mixed $validator, string $field)
{
if (!$validator->passes($field, $this->data[$field] ?? null, $this->data)) {
if (isset($this->data[$field]) && !$validator->passes($field, $this->data[$field], $this->data)) {
$this->addError($field, $validator->message());
}
}


/**
* Validate constructor inputs to ensure required data and rules are provided.
*
Expand All @@ -150,18 +152,18 @@ protected function validateConstructorInputs()
*
* @return array List of errors.
*/
public static function getErrors(): array
public function getErrors(): array
{
return self::$errors;
return $this->errors;
}

/**
* Check if validation is successful (no errors).
*
* @return bool True if validation is successful, otherwise false.
*/
public static function isValid(): bool
public function isValid(): bool
{
return count(self::$errors) < 1;
return count($this->errors) < 1;
}
}
Loading
Loading