| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EnumExtensions.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.ComponentModel; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using MyNet.Humanizer.Display; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Humanizer.Facade; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Contains extension methods for humanizing Enums. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class EnumExtensions |
| | | 21 | | { |
| | | 22 | | extension(Enum value) |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the display name of the enum value, if it has a <see cref="DisplayNameAttribute"/>. Otherwise, returns |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="culture">The culture to use when retrieving the display name.</param> |
| | | 28 | | /// <returns>The display name of the enum value.</returns> |
| | 66 | 29 | | public string Humanize(CultureInfo? culture = null) => value.Humanize(DisplayTextOptions.Default, culture); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the display name of the enum value, if it has a <see cref="DisplayNameAttribute"/>. Otherwise, returns |
| | | 33 | | /// Uses the DI-registered <see cref="IDisplayTextStrategy{T}"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="options">The options to use when retrieving the display name.</param> |
| | | 36 | | /// <param name="culture">The culture to use when retrieving the display name.</param> |
| | | 37 | | /// <returns>The display name of the enum value.</returns> |
| | | 38 | | public string Humanize(DisplayTextOptions options, CultureInfo? culture = null) |
| | 90 | 39 | | => TextHumanizer.Humanize(value, options, culture); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | extension(string? input) |
| | | 43 | | { |
| | | 44 | | /// <summary> |
| | | 45 | | /// Dehumanizes the input string to the specified target enum type. The method first attempts to parse the input |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="culture">The culture to use for humanization.</param> |
| | | 48 | | /// <typeparam name="TEnum">The type of the target enum.</typeparam> |
| | | 49 | | /// <returns>The matched enum value.</returns> |
| | | 50 | | /// <exception cref="KeyNotFoundException">Thrown if no matching enum value is found.</exception> |
| | | 51 | | public TEnum DehumanizeTo<TEnum>(CultureInfo? culture = null) |
| | | 52 | | where TEnum : struct, Enum |
| | 21 | 53 | | => (TEnum)DehumanizeCore(input, typeof(TEnum), culture); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Dehumanizes the input string to the specified target enum type. The method first attempts to parse the input |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="targetEnum">The target enum type to dehumanize to.</param> |
| | | 59 | | /// <param name="culture">The culture to use for humanization.</param> |
| | | 60 | | /// <returns>The matched enum value.</returns> |
| | | 61 | | /// <exception cref="KeyNotFoundException">Thrown if no matching enum value is found.</exception> |
| | 3 | 62 | | public Enum DehumanizeTo(Type targetEnum, CultureInfo? culture = null) => DehumanizeCore(input, targetEnum, cult |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Attempts to dehumanize the input string to the specified target enum type. The method first attempts to pars |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="result">The resulting enum value if a match is found; otherwise, null.</param> |
| | | 68 | | /// <param name="culture">The culture to use for humanization.</param> |
| | | 69 | | /// <typeparam name="TEnum">The type of the target enum.</typeparam> |
| | | 70 | | /// <returns>True if a match is found; otherwise, false.</returns> |
| | | 71 | | public bool TryDehumanizeTo<TEnum>(out TEnum? result, CultureInfo? culture = null) |
| | | 72 | | where TEnum : struct, Enum |
| | | 73 | | { |
| | | 74 | | try |
| | | 75 | | { |
| | 6 | 76 | | result = input.DehumanizeTo<TEnum>(culture); |
| | 3 | 77 | | return true; |
| | | 78 | | } |
| | 3 | 79 | | catch (Exception) |
| | | 80 | | { |
| | 3 | 81 | | result = null; |
| | 3 | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Core method that performs the dehumanization logic for both generic and non-generic versions of DehumanizeTo. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="input">The input string that was being dehumanized.</param> |
| | | 91 | | /// <param name="targetEnum">The target enum type to dehumanize to.</param> |
| | | 92 | | /// <param name="culture">The culture to use for humanization.</param> |
| | | 93 | | /// <returns>The matched enum value or null if no match is found and the behavior is set to return default.</returns |
| | | 94 | | /// <exception cref="ArgumentNullException">Thrown when the targetEnum is null.</exception> |
| | | 95 | | /// <exception cref="ArgumentException">Thrown when the targetEnum is not an enum.</exception> |
| | | 96 | | private static Enum DehumanizeCore(string? input, Type targetEnum, CultureInfo? culture) |
| | | 97 | | { |
| | 24 | 98 | | ArgumentNullException.ThrowIfNull(targetEnum); |
| | | 99 | | |
| | 24 | 100 | | if (!targetEnum.IsEnum) |
| | | 101 | | { |
| | 3 | 102 | | throw new ArgumentException($"Type '{targetEnum}' is not an enum.", nameof(targetEnum)); |
| | | 103 | | } |
| | | 104 | | |
| | 21 | 105 | | if (string.IsNullOrWhiteSpace(input)) |
| | | 106 | | { |
| | 3 | 107 | | throw new ArgumentException($"Input '{input}' cannot be null or empty.", nameof(input)); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // 1. Current parsing |
| | 18 | 111 | | if (Enum.TryParse(targetEnum, input, true, out var parsed) && parsed is Enum parsedEnum) |
| | | 112 | | { |
| | 9 | 113 | | return parsedEnum; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // 2. Humanized parsing |
| | 114 | 117 | | foreach (var value in Enum.GetValues(targetEnum)) |
| | | 118 | | { |
| | 51 | 119 | | var enumValue = (Enum)value; |
| | | 120 | | |
| | 51 | 121 | | if (string.Equals(enumValue.Humanize(culture: culture), input, StringComparison.OrdinalIgnoreCase)) |
| | | 122 | | { |
| | 3 | 123 | | return enumValue; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | // 3. Not found |
| | 3 | 128 | | throw new KeyNotFoundException($"No matching enum value found for input '{input}' in enum type '{targetEnum}'.") |
| | 3 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |