Skip to content

Commit

Permalink
add the ability to set a custom mobile validation class in configs
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Sep 20, 2023
1 parent 084ea99 commit 9ca5de5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ php artisan config:clear
```
Then, you can adjust the waiting_time, code_min, and code_max in the `config/otp.php`

## Custom Mobile Number Validation
The package comes with a default mobile number validator, but you can easily use your own.

Here's how you can do it:

1. Create a Custom Validator Class
First, create a class that implements `MobileValidatorInterface`. This interface expects you to define a validate method.
```bash
namespace YourNamespace;

use Salehhashemi\OtpManager\Contracts\MobileValidatorInterface;

class CustomMobileValidator implements MobileValidatorInterface
{
public function validate(string $mobile): void
{
// Your validation logic here
}
}
```
2. Update Configuration
Next, open your OTP configuration file and update the `mobile_validation_class` option to use your custom validator class:
```bash
'mobile_validation_class' => CustomMobileValidator::class,
```

### Exceptions
* `\InvalidArgumentException` will be thrown if the mobile number is empty.
Expand Down
34 changes: 34 additions & 0 deletions config/otp.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
<?php

use Salehhashemi\OtpManager\Validators\DefaultMobileValidator;

return [

/*
|--------------------------------------------------------------------------
| OTP Waiting Time
|--------------------------------------------------------------------------
|
| This option defines the number of seconds a user has to wait before
| being allowed to request a new OTP. Set it to a reasonable value
| to prevent abuse.
|
*/
'waiting_time' => 120,

/*
|--------------------------------------------------------------------------
| OTP Code Range
|--------------------------------------------------------------------------
|
| These options define the minimum and maximum range for the generated
| OTP codes. Adjust these values as per your security requirements.
|
*/
'code_min' => 111111,
'code_max' => 999999,

/*
|--------------------------------------------------------------------------
| Mobile Validation Class
|--------------------------------------------------------------------------
|
| This option defines the class responsible for validating mobile numbers.
| If you want to use your own validation logic, you can create your
| own class and replace the class name here.
|
*/
'mobile_validation_class' => DefaultMobileValidator::class,
];
16 changes: 16 additions & 0 deletions src/Contracts/MobileValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Salehhashemi\OtpManager\Contracts;

interface MobileValidatorInterface
{
/**
* Validates the provided mobile number.
*
* @param string $mobile The mobile number to validate.
* @return void
*
* @throws \InvalidArgumentException If the mobile number is empty.
*/
public function validate(string $mobile): void;
}
18 changes: 7 additions & 11 deletions src/OtpManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Salehhashemi\ConfigurableCache\ConfigurableCache;
use Salehhashemi\OtpManager\Contracts\MobileValidatorInterface;
use Salehhashemi\OtpManager\Dto\OtpDto;
use Salehhashemi\OtpManager\Dto\SentOtpDto;
use Salehhashemi\OtpManager\Events\OtpPrepared;
Expand All @@ -20,9 +21,14 @@ class OtpManager

private int $waitingTime;

protected MobileValidatorInterface $mobileValidator;

public function __construct()
{
$this->waitingTime = config('otp.waiting_time');

$mobileValidationClass = config('otp.mobile_validation_class');
$this->mobileValidator = app()->make($mobileValidationClass);
}

/**
Expand Down Expand Up @@ -134,10 +140,6 @@ public function getVerifyCode(string $mobile, string $type): ?OtpDto

$this->type = $type;

if (empty($mobile)) {
return null;
}

return ConfigurableCache::get($this->getCacheKey($mobile, 'value'), 'otp');
}

Expand All @@ -152,10 +154,6 @@ public function deleteVerifyCode(string $mobile, string $type): bool

$this->type = $type;

if (empty($mobile)) {
return false;
}

return ConfigurableCache::delete($this->getCacheKey($mobile, 'value'), 'otp');
}

Expand Down Expand Up @@ -257,8 +255,6 @@ protected function getCacheKey(string $mobile, string $for): string
*/
protected function validateMobile(string $mobile): void
{
if (empty($mobile)) {
throw new \InvalidArgumentException('Mobile number cannot be empty.');
}
$this->mobileValidator->validate($mobile);
}
}
18 changes: 18 additions & 0 deletions src/Validators/DefaultMobileValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Salehhashemi\OtpManager\Validators;

use Salehhashemi\OtpManager\Contracts\MobileValidatorInterface;

class DefaultMobileValidator implements MobileValidatorInterface
{
/**
* @inheritdoc
*/
public function validate(string $mobile): void
{
if (empty($mobile)) {
throw new \InvalidArgumentException('Mobile number cannot be empty.');
}
}
}
1 change: 0 additions & 1 deletion tests/OtpManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public function test_sendAndRetryCheck_throws_validation_exception_for_quick_ret
$otpManager->send('1234567890', 'sms');

$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Too many OTP attempts. Please try again in');

$otpManager->sendAndRetryCheck('1234567890', 'sms');
}
Expand Down

0 comments on commit 9ca5de5

Please sign in to comment.