Skip to content

Commit

Permalink
2023-04-04 11:31 - v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisOuellet committed Apr 4, 2023
1 parent 61e41a7 commit 292c3b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.4
1.0.5
58 changes: 57 additions & 1 deletion src/phpSMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,55 @@ public function isReady(){
return ($this->Provider !== null && $this->SID !== null && $this->Token !== null && $this->Phone !== null);
}

/**
* Validate phone number format.
*
* @param string $number
* @return bool
*/
public function validate($number) {
// A simple regex for validating a phone number
$pattern = "/^\+?\d{1,4}[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/";

if (preg_match($pattern, $number)) {
return true;
} else {
return false;
}
}

/**
* Format phone number to E.164 format.
*
* @param string $number
* @param string $countryCode
* @return string|bool
*/
public function formatToE164($number, $countryCode) {
// Remove all non-digit characters from the phone number
$number = preg_replace('/\D/', '', $number);

// Check if the number has a leading '+'
if (substr($number, 0, 1) === '+') {
return $number;
}

// Check if the number has a leading '0'
if (substr($number, 0, 1) === '0') {
$number = substr($number, 1);
}

// Add the country code to the beginning of the number
$e164Number = '+' . $countryCode . $number;

// Validate the E.164 formatted number
if ($this->validatePhoneNumber($e164Number)) {
return $e164Number;
} else {
return false;
}
}

/**
* Send SMS.
*
Expand All @@ -157,6 +206,13 @@ public function isReady(){
public function send($Number, $Body){
try{

// Check Number is valid
if(!$this->validate($Number)){

// Throw Exception
throw new Exception("Number is not valid.");
}

// Check if Provider was configured
if(!$this->Provider){

Expand Down Expand Up @@ -197,7 +253,7 @@ public function send($Number, $Body){
// Construct Data Array
$Data = [
'From' => $this->Phone,
'To' => $Number,
'To' => $this->formatToE164($Number),
'Body' => $Body,
];

Expand Down

0 comments on commit 292c3b0

Please sign in to comment.