MVCLogin/VCart.Domain/ValueObjects/Phone.cs

35 lines
947 B
C#

using BaseDomain;
using System.Text.RegularExpressions;
namespace Blinks.Domain
{
public class Phone : AValueObject
{
public string Value { get; set; }
public Phone(string phone)
{
this.IsPhoneValid(this.Value);
Value = phone;
}
public override bool GetValidationExpression()
{
return this.IsPhoneValid(this.Value);
}
public bool IsPhoneValid(string phone)
{
var pattern = "^\\s*(\\d{2}|\\d{0})[-. ]?(\\d{5}|\\d{4})[-. ]?(\\d{4})[-. ]?\\s*$";
return this.IsValid = Regex.IsMatch(Value, pattern);
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return this.Value;
}
public static implicit operator string(Phone name) => name.Value;
public static explicit operator Phone(string phone) => new Phone(phone);
}
}