Skip to content

Commit

Permalink
Merge pull request #999 from mollie/PIPRES-469/number-format
Browse files Browse the repository at this point in the history
PIPRES-469 Phone number will now have more strict rules to have E.164 format
  • Loading branch information
GytisZum authored Dec 9, 2024
2 parents 64a8452 + 7127479 commit 3a1627c
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/Provider/PhoneNumberProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,32 @@ public function getFromAddress(Address $address)
$phoneNumber = $this->getMobileOrPhone($address);

$phoneNumber = str_replace(' ', '', $phoneNumber);
$phoneNumber = str_replace('+', '', $phoneNumber);

if (empty($phoneNumber) || empty(str_replace(' ', '', $phoneNumber))) {
if (empty($phoneNumber)) {
return null;
}

while ('0' === $phoneNumber[0]) {
$phoneNumber = substr($phoneNumber, 1);

if (empty($phoneNumber) && $phoneNumber !== '0') {
// If the phone number starts with a '+', validate that it's in E.164 format
if ($phoneNumber[0] === '+') {
// E.164 format: +<country_code><number> and should be between 3 and 18 digits total
if (preg_match("/^\+\d{3,18}$/", $phoneNumber)) {
return $phoneNumber; // Return the number if it matches E.164 format
} else {
return null;
}
}

if ('+' !== $phoneNumber[0]) {
$phoneNumber = '+' . $phoneNumber;
}

$regex = "/^\+\d{3,18}$/";
if (1 == preg_match($regex, $phoneNumber)) {
return $phoneNumber;
} else {
// If the phone number starts with '0' (a local number), it's considered invalid in E.164 format
if ($phoneNumber[0] === '0') {
return null;
}

return null;
}

private function getMobileOrPhone(Address $address)
{
// Retrieve either the mobile or regular phone number based on the address format
$addressFormat = new AddressFormat((int) $address->id_country);

if (strpos($addressFormat->format, 'phone_mobile') !== false) {
Expand Down

0 comments on commit 3a1627c

Please sign in to comment.