PhoneNumber.parse constructor

PhoneNumber.parse(
  1. String value
)

Parse given value into PhoneNumber instance

Implementation

factory PhoneNumber.parse(String value) {
  final String normalizedValue = normalize(value);
  final Iterable<Country> matchedCountries =
      countries.where((c) => c.matches(normalizedValue));

  for (final item in matchedCountries) {
    if (item.isValidNumber(normalizedValue)) {
      return PhoneNumber._(
        nationalNumber: normalizedValue.substring(item.prefixLength),
        formattedNumber: '+$normalizedValue',
        country: item,
        isValid: true,
      );
    }
  }

  if (matchedCountries.isNotEmpty) {
    return PhoneNumber._(
      country: matchedCountries.first,
      nationalNumber:
          normalizedValue.substring(matchedCountries.first.prefixLength),
      formattedNumber: '+$normalizedValue',
      isValid: false,
    );
  }

  return PhoneNumber._(
    country: null,
    nationalNumber: normalizedValue,
    formattedNumber: '+$normalizedValue',
    isValid: false,
  );
}