sumatube/SumaTube.Domain/ValueObjects/DateBirth.cs
2025-04-20 23:33:46 -03:00

66 lines
1.7 KiB
C#

using BaseDomain;
using System;
using System.Diagnostics.Metrics;
using System.IO;
using System.Reflection.Emit;
namespace SumaTube.Domain
{
public class DateBirth : AValueObject
{
public DateTime Value { get; private set; }
public DateBirth(string dateOfBirth)
{
DateTime dateAsDate;
if (dateOfBirth!=null && (DateTime.TryParse(dateOfBirth, out dateAsDate) || dateAsDate > DateTime.Now))
{
this.Value = dateAsDate;
this.IsValid = true;
}
else
{
this.IsValid = false;
}
}
public DateBirth(DateTime dateOfBirth)
{
if (dateOfBirth > DateTime.Now)
{
this.IsValid = false;
}
this.Value = dateOfBirth;
}
public int YearsOld {
get
{
var age = DateTime.Now.Year - this.Value.Year;
if (this.Value.Date > DateTime.Now.AddYears(-age)) age--;
return age;
}
}
public static implicit operator string(DateBirth d) => d.Value.ToString("pt-BR");
public static explicit operator DateBirth(DateTime b) => new DateBirth(b);
public static explicit operator DateBirth(string b) => new DateBirth(b);
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
public override bool GetValidationExpression()
{
if (this.Value > DateTime.Now)
{
this.IsValid = false;
}
return this.IsValid;
}
}
}