Refine the cleaning of mobile number. #658
This commit is contained in:
parent
466d6c124f
commit
a6add363bd
1 changed files with 74 additions and 60 deletions
|
|
@ -36,6 +36,8 @@ class RisingTideGateway
|
|||
// at this point, assume that mobile is numeric and valid mobile number
|
||||
$clean_num = $this->cleanPhoneNumber($mobile_num);
|
||||
|
||||
if ($clean_num != '')
|
||||
{
|
||||
$headers = [
|
||||
'Content-Type: application/vnd.net.wyrls.Document-v3+json'
|
||||
];
|
||||
|
|
@ -87,6 +89,9 @@ class RisingTideGateway
|
|||
|
||||
error_log($result);
|
||||
}
|
||||
else
|
||||
error_log('Invalid mobile number provided. Cannot send SMS message.');
|
||||
}
|
||||
|
||||
public function validatePhoneNumber($mobile)
|
||||
{
|
||||
|
|
@ -114,29 +119,38 @@ class RisingTideGateway
|
|||
|
||||
protected function cleanPhoneNumber($mobile)
|
||||
{
|
||||
$num = trim($mobile);
|
||||
// remove any non digit character from string
|
||||
$clean_number = preg_replace('~\D~', '', $mobile);
|
||||
error_log('cleaned ' . $clean_number);
|
||||
|
||||
// at this point, assume that mobile is numeric and valid mobile number
|
||||
|
||||
// check if number begins with 63 and length is 12 for the format 639XXXXXXXXX
|
||||
if ((strlen($num) == 12) && (substr($num, 0, 2) == '63'))
|
||||
return $num;
|
||||
|
||||
if ($num[0] == '0')
|
||||
// does it fit our 09XXXXXXXXX pattern?
|
||||
if (preg_match('/^09[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
// remove the 0, prepend 63
|
||||
$stripped_num = substr($num, 1);
|
||||
$clean_num = '63' . $stripped_num;
|
||||
return $clean_num;
|
||||
}
|
||||
// remove first '0'
|
||||
$clean_number = substr($clean_number, 1);
|
||||
|
||||
if ($num[0] == '9')
|
||||
// prepend 63
|
||||
$clean_number = '63' . $clean_number;
|
||||
|
||||
error_log("CONVERTED TO $clean_number");
|
||||
return $clean_number;
|
||||
}
|
||||
// does it fit our 63XXXXXXXXXX pattern?
|
||||
else if (preg_match('/^63[0-9]{10}$/', $clean_number))
|
||||
{
|
||||
// leave alone
|
||||
return $clean_number;
|
||||
}
|
||||
// does it fit our 9XXXXXXXXX pattern?
|
||||
else if (preg_match('/^9[0-9]{9}$/', $clean_number))
|
||||
{
|
||||
// prepend 63
|
||||
$clean_num = '63' . $num;
|
||||
return $clean_num;
|
||||
$clean_number = '63' . $clean_number;
|
||||
|
||||
error_log("CONVERT TO $clean_number");
|
||||
return $clean_number;
|
||||
}
|
||||
|
||||
return $num;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue