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

Add custom error message and review folder architecture #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,32 @@ try {
}

```
You can also customize the validation error messages

```php
$data = [
'username' => 'BlakvGhost',
];

$validator = new Validator(
$data,
[
'username' => 'required|string',
],
[
'username' => [
'required' => 'Votre nom d\'utilisateur doit être présent',
'string' => 'Votre nom d\'utilisateur doit forcément être une chaîne de caractère',
],
]
);
```

## Features

- `Predefined Rules`: PHPValidator comes with a set of predefined validation rules such as required, string, email, maxLength etc.

- `Custom Rules`: Easily create custom validation rules by implementing the `RuleInterface`.
- `Custom Rules`: Easily create custom validation rules by implementing the `Rule` Interface.

- `Multilingual Support`: Customize validation error messages based on the application's language using the `LangManager`.

Expand Down Expand Up @@ -259,18 +279,17 @@ PHPValidator provides a variety of predefined rules that you can use for data va
```
## Custom Rule

In addition to the predefined rules, you can create custom validation rules by implementing the `RuleInterface`. Here's an example of how to create and use a custom rule:
In addition to the predefined rules, you can create custom validation rules by implementing the `Rule` Interface. Here's an example of how to create and use a custom rule:

### CustomPasswordRule.php

```php
// CustomPasswordRule.php
namespace YourNameSpace\Rules;

use BlakvGhost\PHPValidator\Rules\RuleInterface;
use BlakvGhost\PHPValidator\LangManager;
use BlakvGhost\PHPValidator\Contracts\Rule;

class CustomPasswordRule implements RuleInterface
class CustomPasswordRule implements Rule
{
protected $field;

Expand Down Expand Up @@ -300,6 +319,7 @@ class CustomPasswordRule implements RuleInterface

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

use YourNameSpace\CustomPasswordRule;


Expand Down Expand Up @@ -334,7 +354,8 @@ class CustomPasswordRule implements RuleInterface

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use BlakvGhost\PHPValidator\RulesMaped;
use BlakvGhost\PHPValidator\Mapping\RulesMaped;

use YourNameSpace\CustomPasswordRule;

// Add your rule here using an alias and the full namespace of your custom class
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/RuleInterface.php → src/Contracts/Rule.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

/**
* RuleInterface - Interface for defining validation rules in the PHPValidator package.
* Rule - Interface for defining validation rules in the PHPValidator package.
*
* @package BlakvGhost\PHPValidator\Rules
* @package BlakvGhost\PHPValidator\Contracts
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Rules;
namespace BlakvGhost\PHPValidator\Contracts;

interface RuleInterface
interface Rule
{
/**
* Constructor of the RuleInterface.
Expand Down
44 changes: 5 additions & 39 deletions src/LangManager.php → src/Lang/Lang.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php

/**
* LangManager - A simple language manager for handling translations in the PHPValidator package.
* Lang - This class provides a convenient mapping of rules errors messages for different languages.
*
* @package BlakvGhost\PHPValidator
* @package BlakvGhost\PHPValidator\Lang
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator;
namespace BlakvGhost\PHPValidator\Lang;

class LangManager
trait Lang
{
/**
* Translations for different languages.
*
* @var array
*/
protected static $translations = [
private static $translations = [
'fr' => [
// French translations
'validation.empty_data' => 'Les données de validation ne peuvent pas être vides.',
Expand Down Expand Up @@ -82,38 +82,4 @@ class LangManager
'validation.size' => "The :attribute field must have the required length :value.",
],
];


/**
* Get the current language.
*
* @return string Current language code.
*/
private static function getLocal(): string
{
// Get the current language from environment variables, defaulting to 'en' (English) if not set.
return $_ENV['local'] ?? 'en';
}

/**
* Get a translated message for the given key.
*
* @param string $key Translation key.
* @param array|null $parameters Placeholder values to replace in the translated message.
* @return string Translated message.
*/
public static function getTranslation(string $key, ?array $parameters = []): string
{
// Get the translation for the current language and the provided key, or use the key itself if not found.
$translation = self::$translations[self::getLocal()][$key] ?? $key;

// Replace placeholders in the translation with the provided values.
if ($parameters) {
foreach ($parameters as $placeholder => $value) {
$translation = str_replace(":$placeholder", $value, $translation);
}
}

return $translation;
}
}
51 changes: 51 additions & 0 deletions src/Lang/LangManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* LangManager - A simple language manager for handling translations in the PHPValidator package.
*
* @package BlakvGhost\PHPValidator\Lang
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Lang;

class LangManager
{

use Lang;

/**
* Get the current language.
*
* @return string Current language code.
*/
private static function getLocal(): string
{
// Get the current language from environment variables, defaulting to 'en' (English) if not set.
return $_ENV['local'] ?? 'en';
}

/**
* Get a translated message for the given key.
*
* @param string $key Translation key.
* @param array|null $parameters Placeholder values to replace in the translated message.
* @return string Translated message.
*/
public static function getTranslation(string $key, ?array $parameters = []): string
{
// Get the translation for the current language and the provided key, or use the key itself if not found.
$translation = self::$translations[self::getLocal()][$key] ?? $key;

// Replace placeholders in the translation with the provided values.
if ($parameters) {
foreach ($parameters as $placeholder => $value) {
$translation = str_replace(":$placeholder", $value, $translation);
}
}

return $translation;
}
}
51 changes: 4 additions & 47 deletions src/RulesMaped.php → src/Mapping/RulesAlias.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

/**
* RulesMaped - Class that maps all the validation rules with their aliases
* RulesAlias - Class that maps all the validation rules with their aliases
*
* This class provides a convenient mapping of rule aliases to their corresponding PHPValidator rules.
* It allows for easy reference and retrieval of validation rule classes.
*
* @package BlakvGhost\PHPValidator
* @package BlakvGhost\PHPValidator\Mapping
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator;
namespace BlakvGhost\PHPValidator\Mapping;

use BlakvGhost\PHPValidator\Rules\AcceptedIfRule;
use BlakvGhost\PHPValidator\Rules\AcceptedRule;
Expand Down Expand Up @@ -41,7 +40,7 @@
use BlakvGhost\PHPValidator\Rules\UrlRule;
use BlakvGhost\PHPValidator\Rules\ValidIpRule;

class RulesMaped
trait RulesAlias
{
/**
* @var array Mapping of rule aliases to their corresponding rule classes.
Expand Down Expand Up @@ -74,46 +73,4 @@ class RulesMaped
'url' => UrlRule::class,
'ip' => ValidIpRule::class,
];

/**
* Get the mapping of rule aliases to their corresponding rule classes.
*
* @return array
*/
protected static function getRules(): array
{
return self::$rules;
}

/**
* Get the rule class for a given alias.
*
* @param string $alias Rule alias to retrieve the corresponding rule class.
* @return string Rule class.
* @throws ValidatorException If the rule alias is not found.
*/
protected static function getRule(string $alias): string
{
if (isset(self::$rules[$alias]) && class_exists(self::$rules[$alias])) {
return self::$rules[$alias];
}

$translatedMessage = LangManager::getTranslation('validation.rule_not_found', [
'ruleName' => $alias,
]);

throw new ValidatorException($translatedMessage);
}

/**
* Add a new rule to the mapping.
*
* @param string $alias Rule alias.
* @param string $className Rule class name.
* @return void
*/
public static function addRule(string $alias, string $className): void
{
self::$rules[$alias] = $className;
}
}
62 changes: 62 additions & 0 deletions src/Mapping/RulesMaped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* RulesMaped - Class that allows for easy reference and retrieval of validation rule classes.
*
* @package BlakvGhost\PHPValidator\Mapping
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator\Mapping;

use BlakvGhost\PHPValidator\Lang\LangManager;
use BlakvGhost\PHPValidator\ValidatorException;

class RulesMaped
{
use RulesAlias;

/**
* Get the mapping of rule aliases to their corresponding rule classes.
*
* @return array
*/
protected static function getRules(): array
{
return self::$rules;
}

/**
* Get the rule class for a given alias.
*
* @param string $alias Rule alias to retrieve the corresponding rule class.
* @return string Rule class.
* @throws ValidatorException If the rule alias is not found.
*/
protected static function getRule(string $alias): string
{
if (isset(self::$rules[$alias]) && class_exists(self::$rules[$alias])) {
return self::$rules[$alias];
}

$translatedMessage = LangManager::getTranslation('validation.rule_not_found', [
'ruleName' => $alias,
]);

throw new ValidatorException($translatedMessage);
}

/**
* Add a new rule to the mapping.
*
* @param string $alias Rule alias.
* @param string $className Rule class name.
* @return void
*/
public static function addRule(string $alias, string $className): void
{
self::$rules[$alias] = $className;
}
}
9 changes: 5 additions & 4 deletions src/Rules/AcceptedIfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;
use BlakvGhost\PHPValidator\Contracts\Rule;
use BlakvGhost\PHPValidator\Lang\LangManager;

class AcceptedIfRule implements RuleInterface
class AcceptedIfRule implements Rule
{
/**
* The name of the field being validated.
Expand Down Expand Up @@ -60,8 +61,8 @@ public function passes(string $field, $value, array $data): bool
/**
* Get the validation error message for the accepted if rule.
*
* @return string Validation error message.
*/
* @return string Validation error message.
*/
public function message(): string
{
// Use LangManager to get a translated validation error message.
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/AcceptedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;
use BlakvGhost\PHPValidator\Contracts\Rule;
use BlakvGhost\PHPValidator\Lang\LangManager;

class AcceptedRule implements RuleInterface
class AcceptedRule implements Rule
{
/**
* The name of the field being validated.
Expand Down
Loading