/// <inheritdoc /> public string PrettifyE164(string phoneNumber) { _logger.LogDebug($"Prettifying phone number: {phoneNumber}"); // Match the hone number with the E.164 format Match parsedNumber = MatchSingleE164Number(phoneNumber); // Extract the country code string countryCode = ExtractCountryCodeFromE164(parsedNumber); string subscriberNumber = ExtractSubscriberNumberFromE164(parsedNumber); _logger.LogDebug($"Getting pretty formats for country code: {countryCode}"); // Get the list of pretty formats for the country code var prettyFormats = new List <E164Format>(_formatStore.GetFormatsByCountry(countryCode)); // Find the matching format E164Format matchedFormat = FindMatchingFormat(subscriberNumber, prettyFormats); if (matchedFormat == null) { throw new ArgumentException($"Invalid phone number supplied: {phoneNumber}"); } _logger.LogDebug($"Found matching format for phone number ({phoneNumber}): {matchedFormat.Format}"); // Appply matched format string formattedNumber = FormatE164(subscriberNumber, matchedFormat); return(formattedNumber); }
/// <summary> /// Applys a pretty format to a phone number /// </summary> private string FormatE164(string subscriberNumber, E164Format format) { var formattedNumberBuilder = new StringBuilder(); int subscriberIndex = 0; for (int i = 0; i < format.Format.Length; i++) { var currentFormatCharacter = format.Format[i]; // Start with the leading 0, if there if (currentFormatCharacter == '0' && i == 0) { formattedNumberBuilder.Append(currentFormatCharacter); } // Or, if format has a space, just add it else if (currentFormatCharacter == ' ') { formattedNumberBuilder.Append(' '); } // Otherwise, just add the subscriber character we have got to else { formattedNumberBuilder.Append(subscriberNumber[subscriberIndex]); subscriberIndex++; } } return(formattedNumberBuilder.ToString()); }