Skip to content

Commit

Permalink
Merge pull request #7 from BlakvGhost/map-rules
Browse files Browse the repository at this point in the history
Map rules
  • Loading branch information
BlakvGhost authored Nov 18, 2023
2 parents 74c1e5b + 61124ca commit 0c143ec
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 72 deletions.
102 changes: 70 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ try {
$validator = new Validator($data, [
'username' => 'required|string',
'email' => 'required|email',
'score' => ['required','max_length:200', new CustomRule()],
'score' => ['required','max:200', new CustomRule()],
'password' => new CustomRule(),
]);

Expand Down Expand Up @@ -87,7 +87,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va
- Specifies the maximum length of a string field.

```php
'username' => 'max_length:25'
'username' => 'max:25'
```

5. **Confirmed Rule**
Expand Down Expand Up @@ -143,14 +143,14 @@ PHPValidator provides a variety of predefined rules that you can use for data va
- Validates that a field contains only lowercase alphabetic characters.

```php
'username' => 'lower'
'username' => 'lowercase'
```

13. **Uppercase Rule**
- Validates that a field contains only uppercase alphabetic characters.

```php
'username' => 'upper'
'username' => 'uppercase'
```

14. **In Rule**
Expand Down Expand Up @@ -184,7 +184,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va
- Specifies the minimum length of a string field.

```php
'username' => 'min_length:8'
'username' => 'min:8'
```
19. **Not In Rule**
- Validates that a field's value is not in a specified set.
Expand All @@ -204,7 +204,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va
- Validates that a field's value is a valid IP address.

```php
'client_ip' => 'valid_ip',
'client_ip' => 'ip',
```

22. **Json Rule**
Expand All @@ -221,17 +221,19 @@ PHPValidator provides a variety of predefined rules that you can use for data va
'website' => 'url',
```
24. **Alpha Numeric Rule**

- Validates that a field's value contains only alphanumeric characters.

```php
'pseudo' => 'alpha_numeric',
'pseudo' => 'alpha_num',
```

25. **Boolean Rule**

- Validates that a field's value is a boolean.

```php
'is_admin' => 'boolean',
'is_admin' => 'bool',
```

26. **Size Rule**
Expand Down Expand Up @@ -282,38 +284,74 @@ class CustomPasswordRule implements RuleInterface
```
### Usage in Validator

```php
- Use your custom class directly

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use YourNameSpace\CustomPasswordRule;
```php

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use YourNameSpace\CustomPasswordRule;

// ...

try {
// ...

$data = [
'password' => '42',
'confirm_password' => '142',
];
// or
// $data = $_POST;
try {

$validator = new Validator($data, [
'password' => ['required', new CustomPasswordRule()],
]);
$data = [
'password' => '42',
'confirm_password' => '142',
];
// or
// $data = $_POST;

if ($validator->isValid()) {
echo "Validation passed!";
} else {
$errors = $validator->getErrors();
print_r($errors);
$validator = new Validator($data, [
'password' => ['required', new CustomPasswordRule()],
]);

if ($validator->isValid()) {
echo "Validation passed!";
} else {
$errors = $validator->getErrors();
print_r($errors);
}
} catch (ValidatorException $e) {
echo "Validation error: " . $e->getMessage();
}
} catch (ValidatorException $e) {
echo "Validation error: " . $e->getMessage();
}
```
```
- Add your custom class to the rules list and use it as if it were native

```php

use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;
use BlakvGhost\PHPValidator\RulesMaped;
use YourNameSpace\CustomPasswordRule;

// Add your rule here using an alias and the full namespace of your custom class
RulesMaped::addRule('c_password', CustomPasswordRule::class);

try {

$data = [
'password' => '42',
'confirm_password' => '142',
];

$validator = new Validator($data, [
'password' => 'required|c_password',
]);

if ($validator->isValid()) {
echo "Validation passed!";
} else {
$errors = $validator->getErrors();
print_r($errors);
}
} catch (ValidatorException $e) {
echo "Validation error: " . $e->getMessage();
}
```

In this example, we created a CustomPasswordRule that checks if the password is equal to confirm_password. You can customize the passes method to implement your specific validation logic.

## Contributing
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/ActiveURLRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/Rules/LowerRule.php → src/Rules/LowerCaseRule.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* LowercaseRule - A validation rule implementation for checking if a field's value is lowercase.
* LowerCaseRule - A validation rule implementation for checking if a field's value is lowercase.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Kabirou ALASSANE
Expand All @@ -13,7 +13,7 @@

use BlakvGhost\PHPValidator\LangManager;

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

/**
* Constructor of the LowercaseRule class.
* Constructor of the LowerCaseRule class.
*
* @param array $parameters Parameters for the rule (not used in this rule).
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/UpperRule.php → src/Rules/UpperCaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use BlakvGhost\PHPValidator\LangManager;

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

/**
* Constructor of the UppercaseRule class.
* Constructor of the UpperCaseRule class.
*
* @param array $parameters Parameters for the rule (not used in this rule).
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/UrlRule.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* InRule - A validation rule implementation for checking if a value is a valid url.
* UrlRule - A validation rule implementation for checking if a value is a valid url.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Fortunatus KIDJE (v1p3r75)
Expand All @@ -22,7 +22,7 @@ class UrlRule implements RuleInterface
protected $field;

/**
* Constructor of the InRule class.
* Constructor of the UrlRule class.
*
* @param array $parameters Parameters for the rule, specifying the list of valid values.
*/
Expand Down
119 changes: 119 additions & 0 deletions src/RulesMaped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* RulesMaped - 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
* @author Kabirou ALASSANE
* @website https://kabirou-alassane.com
* @github https://github.com/BlakvGhost
*/

namespace BlakvGhost\PHPValidator;

use BlakvGhost\PHPValidator\Rules\AcceptedIfRule;
use BlakvGhost\PHPValidator\Rules\AcceptedRule;
use BlakvGhost\PHPValidator\Rules\ActiveURLRule;
use BlakvGhost\PHPValidator\Rules\AlphaNumericRule;
use BlakvGhost\PHPValidator\Rules\AlphaRule;
use BlakvGhost\PHPValidator\Rules\BooleanRule;
use BlakvGhost\PHPValidator\Rules\ConfirmedRule;
use BlakvGhost\PHPValidator\Rules\EmailRule;
use BlakvGhost\PHPValidator\Rules\FileRule;
use BlakvGhost\PHPValidator\Rules\InRule;
use BlakvGhost\PHPValidator\Rules\JsonRule;
use BlakvGhost\PHPValidator\Rules\LowerCaseRule;
use BlakvGhost\PHPValidator\Rules\MaxLengthRule;
use BlakvGhost\PHPValidator\Rules\MinLengthRule;
use BlakvGhost\PHPValidator\Rules\NotInRule;
use BlakvGhost\PHPValidator\Rules\NullableRule;
use BlakvGhost\PHPValidator\Rules\NumericRule;
use BlakvGhost\PHPValidator\Rules\PasswordRule;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use BlakvGhost\PHPValidator\Rules\RequiredWithRule;
use BlakvGhost\PHPValidator\Rules\SameRule;
use BlakvGhost\PHPValidator\Rules\SizeRule;
use BlakvGhost\PHPValidator\Rules\StringRule;
use BlakvGhost\PHPValidator\Rules\UpperCaseRule;
use BlakvGhost\PHPValidator\Rules\UrlRule;
use BlakvGhost\PHPValidator\Rules\ValidIpRule;

class RulesMaped
{
/**
* @var array Mapping of rule aliases to their corresponding rule classes.
*/
private static $rules = [
'accepted' => AcceptedRule::class,
'accepted_if' => AcceptedIfRule::class,
'active_url' => ActiveURLRule::class,
'alpha' => AlphaRule::class,
'alpha_num' => AlphaNumericRule::class,
'bool' => BooleanRule::class,
'confirmed' => ConfirmedRule::class,
'email' => EmailRule::class,
'file' => FileRule::class,
'in' => InRule::class,
'not_in' => NotInRule::class,
'string' => StringRule::class,
'required' => RequiredRule::class,
'required_with' => RequiredWithRule::class,
'lowercase' => LowerCaseRule::class,
'uppercase' => UpperCaseRule::class,
'json' => JsonRule::class,
'max' => MaxLengthRule::class,
'min' => MinLengthRule::class,
'nullable' => NullableRule::class,
'numeric' => NumericRule::class,
'password' => PasswordRule::class,
'same' => SameRule::class,
'size' => SizeRule::class,
'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;
}
}
Loading

0 comments on commit 0c143ec

Please sign in to comment.