| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using MechanicsSoftware.Domain.Exceptions; |
| | | 3 | | |
| | | 4 | | namespace MechanicsSoftware.Domain.ValueObjects; |
| | | 5 | | |
| | | 6 | | public sealed partial class LicensePlate : ValueObject |
| | | 7 | | { |
| | | 8 | | [GeneratedRegex(@"^[A-Z]{3}[0-9][A-Z][0-9]{2}$")] |
| | | 9 | | private static partial Regex MercosulFormat(); |
| | | 10 | | |
| | | 11 | | [GeneratedRegex(@"^[A-Z]{3}[0-9]{4}$")] |
| | | 12 | | private static partial Regex LegacyFormat(); |
| | | 13 | | |
| | | 14 | | public string Value { get; } |
| | | 15 | | |
| | 100 | 16 | | public LicensePlate(string value) |
| | 100 | 17 | | { |
| | 100 | 18 | | if (string.IsNullOrWhiteSpace(value)) |
| | 4 | 19 | | throw new DomainException("License plate cannot be empty."); |
| | | 20 | | |
| | 96 | 21 | | var normalized = value.ToUpperInvariant(); |
| | 96 | 22 | | if (normalized.Contains('-')) |
| | 4 | 23 | | normalized = normalized.Replace("-", ""); |
| | | 24 | | |
| | 96 | 25 | | if (!MercosulFormat().IsMatch(normalized) && !LegacyFormat().IsMatch(normalized)) |
| | 10 | 26 | | throw new DomainException($"'{value}' is not a valid Brazilian license plate."); |
| | | 27 | | |
| | 86 | 28 | | Value = normalized; |
| | 86 | 29 | | } |
| | | 30 | | |
| | | 31 | | protected override IEnumerable<object?> GetEqualityComponents() |
| | 32 | 32 | | { |
| | 32 | 33 | | yield return Value; |
| | 24 | 34 | | } |
| | | 35 | | |
| | 2 | 36 | | public override string ToString() => Value; |
| | | 37 | | } |