| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace Plainquire.Filter; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Extension methods for <see cref="ValueFilter"/>. |
| | 10 | | /// </summary> |
| | 11 | | public static class ValueFilterExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Humanizes the filter syntax. |
| | 15 | | /// </summary> |
| | 16 | | /// <typeparam name="TValue">The type of the filtered value.</typeparam> |
| | 17 | | /// <param name="filterSyntax">The filter syntax.</param> |
| | 18 | | /// <param name="valueName">Name of the value.</param> |
| | 19 | | /// <param name="configuration">The filter configuration to use.</param> |
| | 20 | | public static string HumanizeFilterSyntax<TValue>(this string? filterSyntax, string valueName, FilterConfiguration? |
| | 21 | | { |
| 16 | 22 | | if (string.IsNullOrWhiteSpace(filterSyntax)) |
| 1 | 23 | | return $"{valueName} is unfiltered"; |
| | 24 | |
|
| 15 | 25 | | var filters = ValueFiltersFactory.Create(filterSyntax, configuration) |
| 15 | 26 | | .GroupBy(x => x.Operator) |
| 15 | 27 | | .Select(filterGroup => |
| 15 | 28 | | { |
| 15 | 29 | | var filterOperator = filterGroup.Key; |
| 15 | 30 | | var operatorName = filterOperator.GetOperatorName<TValue>(); |
| 15 | 31 | |
|
| 15 | 32 | | if (filterOperator is FilterOperator.IsNull or FilterOperator.NotNull) |
| 15 | 33 | | return $"{valueName} {operatorName}"; |
| 15 | 34 | |
|
| 15 | 35 | | var filterValues = filterGroup.Select(x => x.Value).ToArray(); |
| 15 | 36 | | var valuesButLast = filterValues[..^1]; |
| 15 | 37 | | var prefixValueList = string.Join("', '", valuesButLast); |
| 15 | 38 | | var valueList = !string.IsNullOrEmpty(prefixValueList) |
| 15 | 39 | | ? $"'{prefixValueList}' or '{filterValues[^1]}'" |
| 15 | 40 | | : $"'{filterValues[^1]}'"; |
| 15 | 41 | |
|
| 15 | 42 | | return $"{valueName} {operatorName} {valueList}"; |
| 15 | 43 | | }) |
| 15 | 44 | | .ToList(); |
| | 45 | |
|
| 15 | 46 | | return string.Join(" or ", filters); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Creates filter micro syntax string from <see cref="ValueFilter"/>. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="filters">The filters.</param> |
| | 53 | | /// <autogeneratedoc /> |
| | 54 | | public static string? ToString(IReadOnlyCollection<ValueFilter>? filters) |
| | 55 | | { |
| 2 | 56 | | if (filters == null) |
| 0 | 57 | | return null; |
| | 58 | |
|
| 2 | 59 | | var filterStrings = filters |
| 2 | 60 | | .Where(x => x != null) |
| 2 | 61 | | .Select(x => x.ToString()?.Replace(",", "\\,")); |
| | 62 | |
|
| 2 | 63 | | return string.Join(",", filterStrings); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private static string GetOperatorName<TValue>(this FilterOperator filterOperator) |
| | 67 | | { |
| 16 | 68 | | filterOperator = filterOperator != FilterOperator.Default ? filterOperator : GetDefaultOperator<TValue>(); |
| 16 | 69 | | return filterOperator switch |
| 16 | 70 | | { |
| 2 | 71 | | FilterOperator.Contains => "contains", |
| 0 | 72 | | FilterOperator.StartsWith => "starts with", |
| 0 | 73 | | FilterOperator.EndsWith => "ends with", |
| 1 | 74 | | FilterOperator.EqualCaseSensitive => "is (case-sensitive)", |
| 4 | 75 | | FilterOperator.EqualCaseInsensitive => "is", |
| 1 | 76 | | FilterOperator.NotEqual => "is not", |
| 1 | 77 | | FilterOperator.LessThanOrEqual => "is less than or equal to", |
| 2 | 78 | | FilterOperator.LessThan => "is less than", |
| 1 | 79 | | FilterOperator.GreaterThanOrEqual => "is greater than or equal to", |
| 2 | 80 | | FilterOperator.GreaterThan => "is greater than", |
| 1 | 81 | | FilterOperator.IsNull => "is null", |
| 1 | 82 | | FilterOperator.NotNull => "is not null", |
| 0 | 83 | | _ => throw new ArgumentOutOfRangeException(nameof(filterOperator)) |
| 16 | 84 | | }; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | private static FilterOperator GetDefaultOperator<TValue>() |
| 4 | 88 | | => typeof(TValue) == typeof(string) |
| 4 | 89 | | ? FilterOperator.Contains |
| 4 | 90 | | : FilterOperator.EqualCaseInsensitive; |
| | 91 | | } |