| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextTemplateOptionsBuilder.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.Collections.ObjectModel; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Text.Templating; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builder for creating <see cref="TextTemplateOptions"/> instances for use with text templates. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class TextTemplateOptionsBuilder |
| | | 17 | | { |
| | 57 | 18 | | private readonly Dictionary<string, object?> _arguments = new(StringComparer.OrdinalIgnoreCase); |
| | | 19 | | private decimal? _quantity; |
| | 57 | 20 | | private QuantityRenderingMode _quantityRenderingMode = QuantityRenderingMode.PlaceholderOnly; |
| | 57 | 21 | | private string _quantitySeparator = " "; |
| | | 22 | | private string? _quantityFormat; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Sets the count used for pluralization and rendering. |
| | | 26 | | /// Automatically exposes the "count" rendering argument. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="count">The count value.</param> |
| | | 29 | | /// <param name="quantityRenderingMode">The mode for rendering quantity information.</param> |
| | | 30 | | /// <returns>The current builder.</returns> |
| | | 31 | | public TextTemplateOptionsBuilder WithQuantity(decimal count, QuantityRenderingMode quantityRenderingMode = Quantity |
| | | 32 | | { |
| | 6 | 33 | | _quantity = count; |
| | 6 | 34 | | _quantityRenderingMode = quantityRenderingMode; |
| | | 35 | | |
| | 6 | 36 | | return this; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Sets the count used for pluralization and rendering, with the quantity value rendered as a prefix to the transla |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="count">The count value.</param> |
| | | 43 | | /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param> |
| | | 44 | | /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param> |
| | | 45 | | /// <returns>The current builder.</returns> |
| | | 46 | | public TextTemplateOptionsBuilder PrefixWithQuantity(decimal count, string? quantityFormat = null, string quantitySe |
| | | 47 | | { |
| | 39 | 48 | | _quantity = count; |
| | 39 | 49 | | _quantityRenderingMode = QuantityRenderingMode.Prefix; |
| | 39 | 50 | | _quantitySeparator = quantitySeparator; |
| | 39 | 51 | | _quantityFormat = quantityFormat; |
| | | 52 | | |
| | 39 | 53 | | return this; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Sets the count used for pluralization and rendering, with the quantity value rendered as a suffix to the transla |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="count">The count value.</param> |
| | | 60 | | /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param> |
| | | 61 | | /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param> |
| | | 62 | | /// <returns>The current builder.</returns> |
| | | 63 | | public TextTemplateOptionsBuilder SuffixWithQuantity(decimal count, string? quantityFormat = null, string quantitySe |
| | | 64 | | { |
| | 3 | 65 | | _quantity = count; |
| | 3 | 66 | | _quantityRenderingMode = QuantityRenderingMode.Suffix; |
| | 3 | 67 | | _quantitySeparator = quantitySeparator; |
| | 3 | 68 | | _quantityFormat = quantityFormat; |
| | | 69 | | |
| | 3 | 70 | | return this; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Adds or replaces a rendering argument. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="name">The argument name.</param> |
| | | 77 | | /// <param name="value">The argument value.</param> |
| | | 78 | | /// <returns>The current builder.</returns> |
| | | 79 | | public TextTemplateOptionsBuilder WithArgument(string name, object? value) |
| | | 80 | | { |
| | 3 | 81 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 82 | | |
| | 3 | 83 | | _arguments[name] = value; |
| | | 84 | | |
| | 3 | 85 | | return this; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Adds multiple rendering arguments. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="arguments">The arguments to add.</param> |
| | | 92 | | /// <returns>The current builder.</returns> |
| | | 93 | | public TextTemplateOptionsBuilder WithArguments(IEnumerable<KeyValuePair<string, object?>> arguments) |
| | | 94 | | { |
| | 0 | 95 | | ArgumentNullException.ThrowIfNull(arguments); |
| | | 96 | | |
| | 0 | 97 | | foreach (var argument in arguments) |
| | | 98 | | { |
| | 0 | 99 | | _arguments[argument.Key] = argument.Value; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return this; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Builds the <see cref="TextTemplateOptions"/> instance with the configured options. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <returns>The created text template options.</returns> |
| | | 109 | | public TextTemplateOptions Build() |
| | 57 | 110 | | => new() |
| | 57 | 111 | | { |
| | 57 | 112 | | Quantity = _quantity, |
| | 57 | 113 | | QuantityFormat = _quantityFormat, |
| | 57 | 114 | | QuantityRenderingMode = _quantityRenderingMode, |
| | 57 | 115 | | QuantitySeparator = _quantitySeparator, |
| | 57 | 116 | | Arguments = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>(_arguments, StringCompar |
| | 57 | 117 | | }; |
| | | 118 | | } |
| | | 119 | | |