From 3217ffce06abf6b2afa464089e2da8d6bdb2a195 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Wed, 27 Sep 2023 19:35:58 +0000 Subject: [PATCH] Formatter --- src/PhoneNumbers/CountryInfo_NorthAmerica.cs | 1 + .../MXPhoneNumberFormatProvider.cs | 20 +++++++++++++++++++ .../CountryInfo_NorthAmerica_Tests.cs | 2 +- ...PhoneNumber_ToString_NorthAmerica_Tests.cs | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/PhoneNumbers/Formatters/FormatProviders/MXPhoneNumberFormatProvider.cs diff --git a/src/PhoneNumbers/CountryInfo_NorthAmerica.cs b/src/PhoneNumbers/CountryInfo_NorthAmerica.cs index de18aa3ff..827b46fbd 100644 --- a/src/PhoneNumbers/CountryInfo_NorthAmerica.cs +++ b/src/PhoneNumbers/CountryInfo_NorthAmerica.cs @@ -192,6 +192,7 @@ public partial class CountryInfo { CallingCode = "52", Continent = NorthAmerica, + FormatProvider = MXPhoneNumberFormatProvider.Instance, Iso3166Code = "MX", Name = "Mexico", NdcLengths = new ReadOnlyCollection(new[] { 2 }), diff --git a/src/PhoneNumbers/Formatters/FormatProviders/MXPhoneNumberFormatProvider.cs b/src/PhoneNumbers/Formatters/FormatProviders/MXPhoneNumberFormatProvider.cs new file mode 100644 index 000000000..b42ccbbaf --- /dev/null +++ b/src/PhoneNumbers/Formatters/FormatProviders/MXPhoneNumberFormatProvider.cs @@ -0,0 +1,20 @@ +namespace PhoneNumbers.Formatters.FormatProviders; + +/// +/// A for Mexico numbers. +/// +internal sealed class MXPhoneNumberFormatProvider : ComplexPhoneNumberFormatProvider +{ + private MXPhoneNumberFormatProvider() + { + } + + internal static PhoneNumberFormatProvider Instance { get; } = new MXPhoneNumberFormatProvider(); + + protected override string ProvideFormat(PhoneNumber phoneNumber, bool international) => + phoneNumber.NationalSignificantNumber!.Length switch + { + 10 => "## #### ####", + _ => base.ProvideFormat(phoneNumber, international), + }; +} diff --git a/test/PhoneNumbers.Tests/CountryInfo_NorthAmerica_Tests.cs b/test/PhoneNumbers.Tests/CountryInfo_NorthAmerica_Tests.cs index 897373e7b..50c12d77e 100644 --- a/test/PhoneNumbers.Tests/CountryInfo_NorthAmerica_Tests.cs +++ b/test/PhoneNumbers.Tests/CountryInfo_NorthAmerica_Tests.cs @@ -234,7 +234,7 @@ public void CountryInfo_Mexico() Assert.False(countryInfo.AllowsLocalGeographicDialling); Assert.Equal("52", countryInfo.CallingCode); Assert.Equal(CountryInfo.NorthAmerica, countryInfo.Continent); - Assert.IsType(countryInfo.FormatProvider); + Assert.IsType(countryInfo.FormatProvider); Assert.False(countryInfo.IsEuropeanUnionMember); Assert.Equal("MX", countryInfo.Iso3166Code); Assert.Equal("Mexico", countryInfo.Name); diff --git a/test/PhoneNumbers.Tests/PhoneNumber_ToString_NorthAmerica_Tests.cs b/test/PhoneNumbers.Tests/PhoneNumber_ToString_NorthAmerica_Tests.cs index 28824ae29..88ad055b9 100644 --- a/test/PhoneNumbers.Tests/PhoneNumber_ToString_NorthAmerica_Tests.cs +++ b/test/PhoneNumbers.Tests/PhoneNumber_ToString_NorthAmerica_Tests.cs @@ -79,6 +79,13 @@ public void Grenada_Numbers(string input, string format, string expected) => public void Jamaica_Numbers(string input, string format, string expected) => Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format)); + [Theory] + [InlineData("+525550154158", "E.123", "+52 55 5015 4158")] + [InlineData("+525550154158", "N", "55 5015 4158")] + [InlineData("+525550154158", "RFC3966", "tel:+52-55-5015-4158")] + public void Mexico_Numbers(string input, string format, string expected) => + Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format)); + [Theory] [InlineData("+16644913789", "E.123", "+1 664-491-3789")] [InlineData("+16644913789", "N", "(664) 491-3789")]