| | | 1 | | using Enjoy.Domain.Shared.Errors; |
| | | 2 | | using Enjoy.Domain.Shared.Extensions; |
| | | 3 | | using Enjoy.Domain.Shared.Results; |
| | | 4 | | using System.Text.RegularExpressions; |
| | | 5 | | |
| | | 6 | | namespace Enjoy.Domain.Users.ValueObjects; |
| | | 7 | | |
| | | 8 | | public readonly record struct Name |
| | | 9 | | { |
| | 7 | 10 | | public string Value { get; } |
| | | 11 | | |
| | | 12 | | public const int MinLength = 2; |
| | | 13 | | public const int MaxLength = 50; |
| | | 14 | | |
| | | 15 | | public const string Pattern = @"^[a-zA-ZáéíóúÁÉÍÓÚñÑçÇüÜàÀèÈìÌòÒùÙâêÊîôûäëïöüß\s]+$"; |
| | | 16 | | |
| | 63 | 17 | | private Name(string value) => Value = value; |
| | | 18 | | |
| | | 19 | | public static Result<Name> Create(string value) |
| | 77 | 20 | | { |
| | 77 | 21 | | if (string.IsNullOrWhiteSpace(value)) |
| | 8 | 22 | | return Result<Name>.Failure(Error.Name.IsNullOrEmpty); |
| | | 23 | | |
| | 69 | 24 | | var normalized = value.Trim().CapitalizeWords(); |
| | 69 | 25 | | List<Error> errors = []; |
| | 69 | 26 | | if (normalized.Length < MinLength) |
| | 2 | 27 | | errors.Add(Error.Name.TooShort(MinLength)); |
| | 69 | 28 | | if (normalized.Length > MaxLength) |
| | 1 | 29 | | errors.Add(Error.Name.TooLong(MaxLength)); |
| | 69 | 30 | | if (!Regex.IsMatch(normalized, Pattern)) |
| | 4 | 31 | | errors.Add(Error.Name.InvalidFormat); |
| | | 32 | | |
| | 69 | 33 | | return errors.IsEmpty() |
| | 69 | 34 | | ? Result<Name>.Success(new(normalized)) |
| | 69 | 35 | | : Result<Name>.Failure(errors); |
| | 77 | 36 | | } |
| | | 37 | | |
| | 1 | 38 | | public override string ToString() => Value; |
| | | 39 | | |
| | 1 | 40 | | public static implicit operator string(Name name) => name.Value; |
| | | 41 | | } |