73 lines
1.5 KiB
PHP
73 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
class PhoneValidationRule implements Rule
|
|
{
|
|
/**
|
|
* Create a new rule instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function passes($attribute, $phone)
|
|
{
|
|
$phone = str_replace(' ', '', $phone);
|
|
$pattern = "/^\+?(265|0)?[89]\d{8}$/i";
|
|
$retval = preg_match($pattern, $phone, $matches, PREG_OFFSET_CAPTURE);
|
|
|
|
if (count($matches) < 1) {
|
|
return false;
|
|
}
|
|
elseif (strlen($phone) == 9) {
|
|
$msisdn = "265" . $phone;
|
|
}
|
|
elseif (strlen($phone) == 10) {
|
|
$phone = ltrim($phone, 0);
|
|
$msisdn = "265" . $phone;
|
|
}
|
|
elseif (strlen($phone) == 12) {
|
|
$msisdn = $phone;
|
|
}
|
|
if (!isset($msisdn)) {
|
|
return FALSE;
|
|
}
|
|
|
|
$prefix = substr($msisdn, 0, 4);
|
|
|
|
if ($prefix == '2658') {
|
|
$network = 'tnm';
|
|
}
|
|
elseif ($prefix == '2659' ) {
|
|
$network = 'airtel';
|
|
}
|
|
else{
|
|
return FALSE;
|
|
}
|
|
return $msisdn;
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return 'Phone number is not valid';
|
|
}
|
|
}
|