Skip to content

Commit

Permalink
Merge pull request #6 from BlakvGhost/edit-test-format
Browse files Browse the repository at this point in the history
Edit test format for using Validator class #2
  • Loading branch information
BlakvGhost authored Nov 18, 2023
2 parents f468b78 + 2d2117a commit 74c1e5b
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 193 deletions.
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

0 comments on commit 74c1e5b

Please sign in to comment.