using System.Text.RegularExpressions;
using GeradoresService.Core;
using GeradoresService.Data;
namespace GeradoresService.Generators.Contactos;
/// Código postal português (NNNN-NNN). Apenas formato.
public sealed partial class CodigoPostalGenerator : GeneratorBase
{
public override string Id => "codigo-postal";
public override string Name => "Código postal";
public override string Category => Categories.Contactos;
public override string Description => "Código postal português (CP4-CP3) com localidade.";
public override bool HasChecksum => false;
public override GeneratedItem Generate(GeneratorContext context)
{
var (cp, locality, district) = Random(context);
return new GeneratedItem(cp, new Dictionary
{
["Código postal"] = cp,
["Localidade"] = locality,
["Distrito"] = district,
});
}
public static (string Cp, string Locality, string District) Random(GeneratorContext context)
{
var l = context.Pick(PtData.Localities);
return ($"{l.Cp4}-{1 + context.Random.Next(999):D3}", l.Locality, l.District);
}
public override ValidationResult Validate(string value)
{
var m = Pattern().Match(value.Trim());
if (!m.Success)
return ValidationResult.WrongFormat("Formato esperado: NNNN-NNN (ex.: 1000-001).");
var cp = $"{m.Groups[1].Value}-{m.Groups[2].Value}";
return ValidationResult.Valid(cp, "Formato de código postal válido.");
}
[GeneratedRegex(@"^([1-9]\d{3})[-\s]?(\d{3})$")]
private static partial Regex Pattern();
}