| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="AddressFormatter.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Text.RegularExpressions; |
| | | 12 | | using MyNet.Geography; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Humanizer.Formatting.Addresses; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Current implementation of <see cref="IAddressFormatter"/> that provides a standard address format using invariant cu |
| | | 18 | | /// </summary> |
| | 12 | 19 | | public class AddressFormatter() : AddressFormatterBase(CultureInfo.InvariantCulture) |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc/> |
| | | 22 | | protected override string Template { get; } = "{Street}\n{PostalCode} {City}\n{Country}"; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Base class for address formatters, providing common functionality and properties for derived classes. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="culture">The culture information for the address formatter.</param> |
| | | 29 | | public abstract partial class AddressFormatterBase(CultureInfo culture) : IAddressFormatter |
| | | 30 | | { |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | public CultureInfo Culture => culture; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the address template used for formatting addresses. The template should contain tokens in the format of {To |
| | | 36 | | /// </summary> |
| | | 37 | | protected abstract string Template { get; } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | public string Format(Address address) |
| | | 41 | | { |
| | | 42 | | ArgumentNullException.ThrowIfNull(address); |
| | | 43 | | |
| | | 44 | | var values = new Dictionary<string, string?> |
| | | 45 | | { |
| | | 46 | | ["Street"] = address.Street, |
| | | 47 | | ["City"] = address.City, |
| | | 48 | | ["PostalCode"] = address.PostalCode, |
| | | 49 | | ["Country"] = address.Country?.Name |
| | | 50 | | }; |
| | | 51 | | |
| | | 52 | | var lines = Template |
| | | 53 | | .Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) |
| | | 54 | | .Select(line => ReplaceTokens(line, values)) |
| | | 55 | | .Select(CleanupWhitespace) |
| | | 56 | | .Where(static line => !string.IsNullOrWhiteSpace(line)); |
| | | 57 | | |
| | | 58 | | return string.Join(Environment.NewLine, lines); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Replaces the tokens in the template with the corresponding values from the dictionary. Tokens are defined in the |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="template">The template string containing tokens to be replaced.</param> |
| | | 65 | | /// <param name="values">A dictionary containing the values for the tokens.</param> |
| | | 66 | | /// <returns>The template string with tokens replaced by their corresponding values.</returns> |
| | | 67 | | private static string ReplaceTokens(string template, Dictionary<string, string?> values) => TokenRegex().Replace(tem |
| | | 68 | | { |
| | | 69 | | var token = match.Groups[1].Value; |
| | | 70 | | |
| | | 71 | | return values.TryGetValue(token, out var value) |
| | | 72 | | ? value ?? string.Empty |
| | | 73 | | : string.Empty; |
| | | 74 | | }); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Cleans up the whitespace in the provided string by replacing multiple consecutive whitespace characters with a s |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="value">The string value to clean up.</param> |
| | | 80 | | /// <returns>The cleaned-up string with normalized whitespace.</returns> |
| | | 81 | | private static string CleanupWhitespace(string value) => MultiWhitespaceRegex().Replace(value, " ").Trim(); |
| | | 82 | | |
| | | 83 | | [GeneratedRegex(@"\{(.*?)\}")] |
| | | 84 | | private static partial Regex TokenRegex(); |
| | | 85 | | |
| | | 86 | | [GeneratedRegex(@"\s+")] |
| | | 87 | | private static partial Regex MultiWhitespaceRegex(); |
| | | 88 | | } |
| | | 89 | | |