Commit 7671769a authored by Tran Anh Tu's avatar Tran Anh Tu
Browse files

fixed

parent 2b9bf9f5
......@@ -2,3 +2,6 @@
# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = silent
# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = none
using Newtonsoft.Json;
using System;
using System.Linq;
namespace Appstract.UmbracoContactNumber
{
/// <summary>
/// ContactNumber contains the country code and local number of a phone number.
/// </summary>
public class ContactNumber
{
/// <summary>
/// CountryCodeAndPhoneCode gets or sets the two-letter country code as well as the phone code in the format "dk#45"
/// </summary>
[JsonProperty("CountryCodeAndPhoneCode")]
public string CountryCodeAndPhoneCode { get; set; }
/// <summary>
/// Number gets or sets the phone number.
/// </summary>
[JsonProperty("ContactNumber")]
public string Number { get; set; }
/// <summary>
/// FormattedContactNumber gets the formatted <see cref="PhoneCode"/> and <see cref="Number"/> in the format "+45 12345678".
/// If no <see cref="PhoneCode"/> is present, the phone number will be returned.
/// </summary>
public string FormattedContactNumber => string.IsNullOrWhiteSpace(PhoneCode) ? $"{Number}" : $"+{PhoneCode} {Number}";
/// <summary>
/// PhoneCode gets the phone code for the chosen country code.
/// </summary>
public string PhoneCode => CountryCodeAndPhoneCode?.Split('#')?.LastOrDefault();
/// <summary>
/// GetFormattedPhoneFromJson deserializes the input JSON into the ContactNumber class and returns the value of <see cref="FormattedContactNumber"/>.
/// </summary>
/// <param name="jsonPhone">The JSON serialized <see cref="ContactNumber"/>.</param>
/// <returns>The formatted contact number if successful. The input JSON otherwise.</returns>
public static string GetFormattedPhoneFromJson(string jsonPhone)
{
if (string.IsNullOrEmpty(jsonPhone)) { return null; }
try
{
return JsonConvert.DeserializeObject<ContactNumber>(jsonPhone).FormattedContactNumber;
}
catch (Exception)
{
return jsonPhone;
}
}
}
}
using Newtonsoft.Json;
using System.Linq;
namespace Appstract.UmbracoContactNumber
{
public static class ContactNumberUtility
{
public static string GetFormatted(string unformattedNumber)
{
var number = JsonConvert.DeserializeObject<SerializableContactNumber>(unformattedNumber);
if (string.IsNullOrWhiteSpace(number.ContactNumber) || string.IsNullOrWhiteSpace(number.CountryCodeAndPhoneCode))
{
return "Error: Incorrect phone number";
}
return "+" + number.CountryCodeAndPhoneCode?.Split('#')?.LastOrDefault() + " "+ number.ContactNumber;
}
}
public class SerializableContactNumber
{
[JsonProperty("CountryCodeAndPhoneCode")]
public string CountryCodeAndPhoneCode { get; set; }
[JsonProperty("ContactNumber")]
public string ContactNumber { get; set; }
}
}
using Newtonsoft.Json;
using System;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Appstract.UmbracoContactNumber
{
/// <summary>
/// ContactNumberValueConverter converts JSON into instances of the <see cref="ContactNumber"/> class.
/// </summary>
public class ContactNumberValueConverter : PropertyValueConverterBase
{
/// <inheritdoc/>
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
{
if (!(source is string s))
{
return null;
}
return JsonConvert.DeserializeObject<ContactNumber>(s);
}
/// <inheritdoc/>
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
{
return PropertyCacheLevel.Element;
}
/// <inheritdoc/>
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
{
return typeof(ContactNumber);
}
/// <inheritdoc/>
public override bool IsConverter(IPublishedPropertyType propertyType)
{
return propertyType.EditorAlias.Equals("AppstractUmbracoContactNumber");
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment