Last active
October 7, 2016 12:45
-
-
Save dominicthomas/8bf5b778bce2924d0d103907c26a0fed to your computer and use it in GitHub Desktop.
Simple class to format and convert to E164, mobile phone numbers for the UK and the US. Probably better to just use google libphonenumber.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PhoneNumberFormatter { | |
private final String simCountryIso; | |
private final Map<String, String> countryCodeMap = Collections.unmodifiableMap( | |
new HashMap<String, String>() {{ | |
put("GB", "44"); | |
put("US", "1"); | |
}}); | |
private final Map<String, String> countryPatternMap = Collections.unmodifiableMap( | |
new HashMap<String, String>() {{ | |
put("GB", "^(\\+44\\s?7\\d{9}|\\(?07\\d{9}\\)?)"); | |
put("US", "^([\\+][1]{1})?\\(?([2-9]{3})\\)?([0-9]{3})([0-9]{4})$"); | |
}}); | |
public static PhoneNumberFormatter init(Context context) { | |
final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
return new PhoneNumberFormatter(telephonyManager.getSimCountryIso().toUpperCase()); | |
} | |
public PhoneNumberFormatter(String simCountryIso) { | |
this.simCountryIso = simCountryIso; | |
} | |
public Optional<String> getFormattedPhoneNumber(String phoneNumber) { | |
if (phoneNumber == null || simCountryIso == null) { | |
return Optional.absent(); | |
} | |
phoneNumber = phoneNumber.replaceAll("\\s+", ""); | |
if (!isValid(phoneNumber, simCountryIso)) { | |
return Optional.absent(); | |
} | |
if (!countryCodeMap.containsKey(simCountryIso)) { | |
return Optional.absent(); | |
} | |
final String dialingCode = countryCodeMap.get(simCountryIso); | |
final boolean startsWithCountryCode = phoneNumber.startsWith("+" + dialingCode); | |
final boolean startsWithZero = phoneNumber.startsWith("0"); | |
if (!startsWithCountryCode && !startsWithZero) { | |
final String formattedString = "+" + dialingCode + phoneNumber; | |
return Optional.of(formattedString); | |
} else if (!startsWithCountryCode) { | |
final String numberWithoutFirstDigit = phoneNumber.substring(1, phoneNumber.length()); | |
final String formattedString = "+" + dialingCode + numberWithoutFirstDigit; | |
return Optional.of(formattedString); | |
} | |
if (!phoneNumber.startsWith("+")) { | |
phoneNumber = "+" + phoneNumber; | |
} | |
return Optional.of(phoneNumber); | |
} | |
private boolean isValid(String phoneNumber, String simCountryIso) { | |
if (!countryPatternMap.containsKey(simCountryIso)) { | |
return false; | |
} | |
final String pattern = countryPatternMap.get(simCountryIso); | |
return Pattern.compile(pattern).matcher(phoneNumber).matches(); | |
} | |
} | |
public class PhoneNumberFormatterTest extends BaseTest { | |
private PhoneNumberFormatter phoneNumberValidator_GB; | |
private PhoneNumberFormatter phoneNumberValidator_US; | |
@Before | |
public void onSetup() { | |
phoneNumberValidator_GB = new PhoneNumberFormatter("GB"); | |
phoneNumberValidator_US = new PhoneNumberFormatter("US"); | |
} | |
@Test | |
public void test_phone_number_validation_isCorrect_GB() { | |
final String startPhoneNumber = "07000000000"; | |
final String expectedPhoneNumber = "+447000000000"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_GB.getFormattedPhoneNumber(startPhoneNumber); | |
assertTrue(formattedPhoneNumber.isPresent()); | |
assertEquals(expectedPhoneNumber, formattedPhoneNumber.get()); | |
} | |
@Test | |
public void test_phone_number_with_code_validation_isCorrect_GB() { | |
final String expectedPhoneNumber = "+447000000000"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_GB.getFormattedPhoneNumber(expectedPhoneNumber); | |
assertTrue(formattedPhoneNumber.isPresent()); | |
assertEquals(expectedPhoneNumber, formattedPhoneNumber.get()); | |
} | |
@Test | |
public void test_invalid_phone_number_doesFail_GB() { | |
final String invalidPhoneNumber = "+10700000"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_GB.getFormattedPhoneNumber(invalidPhoneNumber); | |
assertTrue(!formattedPhoneNumber.isPresent()); | |
} | |
@Test | |
public void test_phone_number_validation_isCorrect_US() { | |
final String startPhoneNumber = "2345678900"; | |
final String expectedPhoneNumber = "+12345678900"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_US.getFormattedPhoneNumber(startPhoneNumber); | |
assertTrue(formattedPhoneNumber.isPresent()); | |
assertEquals(expectedPhoneNumber, formattedPhoneNumber.get()); | |
} | |
@Test | |
public void test_phone_number_with_code_validation_isCorrect_US() { | |
final String expectedPhoneNumber = "+12345678900"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_US.getFormattedPhoneNumber(expectedPhoneNumber); | |
assertTrue(formattedPhoneNumber.isPresent()); | |
assertEquals(expectedPhoneNumber, formattedPhoneNumber.get()); | |
} | |
@Test | |
public void test_invalid_phone_number_doesFail_US() { | |
final String invalidPhoneNumber = "23456789"; | |
final Optional<String> formattedPhoneNumber = phoneNumberValidator_US.getFormattedPhoneNumber(invalidPhoneNumber); | |
assertTrue(!formattedPhoneNumber.isPresent()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment