2023-09-12 08:13:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class ValidPhoneInputRule implements Rule
|
|
|
|
{
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
2023-09-18 13:12:05 +00:00
|
|
|
if (!is_string($value) || !Str::startsWith($value, '+')) {
|
2023-09-12 08:13:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-09-18 13:12:05 +00:00
|
|
|
|
2023-09-20 10:14:08 +00:00
|
|
|
try {
|
|
|
|
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
|
|
|
|
return $phoneUtil->isValidNumber($phoneUtil->parse($value));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function message()
|
|
|
|
{
|
2023-09-18 13:12:05 +00:00
|
|
|
return 'The :attribute is invalid.';
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
}
|