33 lines
973 B
C#
33 lines
973 B
C#
|
|
namespace Domain.ValueObjects;
|
||
|
|
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
|
||
|
|
public partial class Email
|
||
|
|
{
|
||
|
|
public string Value { get; }
|
||
|
|
|
||
|
|
private Email(string value) => Value = value;
|
||
|
|
|
||
|
|
public static Email Create(string email)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(email))
|
||
|
|
throw new ArgumentException("Email is required", nameof(email));
|
||
|
|
|
||
|
|
email = email.Trim().ToLowerInvariant();
|
||
|
|
|
||
|
|
if (!EmailRegex().IsMatch(email))
|
||
|
|
throw new ArgumentException("Invalid email format", nameof(email));
|
||
|
|
|
||
|
|
return new Email(email);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override string ToString() => Value;
|
||
|
|
public override bool Equals(object? obj) => obj is Email other && Value == other.Value;
|
||
|
|
public override int GetHashCode() => Value.GetHashCode();
|
||
|
|
|
||
|
|
public static implicit operator string(Email email) => email.Value;
|
||
|
|
|
||
|
|
[GeneratedRegex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.Compiled)]
|
||
|
|
private static partial Regex EmailRegex();
|
||
|
|
}
|