| | | 1 | | using Plainquire.Filter.Abstractions; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | |
| | | 7 | | namespace Plainquire.Filter; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Factory to create <see cref="ValueFilter"/> for all values from filterSyntax. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ValueFilterFactory |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Create <see cref="ValueFilter"/> from filter syntax. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="filterSyntax">The filter micro syntax to create the filter from.</param> |
| | | 18 | | /// <param name="configuration">The filter configuration to use.</param> |
| | | 19 | | public static ValueFilter[] Create(string filterSyntax, FilterConfiguration? configuration = null) |
| | | 20 | | { |
| | 6612 | 21 | | var filters = SplitValues(filterSyntax, configuration); |
| | 6612 | 22 | | return filters.Select(filter => ValueFilter.Create(filter, configuration)).ToArray(); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | private static IEnumerable<string> SplitValues(string? filterSyntax, FilterConfiguration? configuration) |
| | | 26 | | { |
| | 6612 | 27 | | if (filterSyntax == null) |
| | 0 | 28 | | return []; |
| | | 29 | | |
| | 6612 | 30 | | configuration ??= FilterConfiguration.Default ?? new FilterConfiguration(); |
| | | 31 | | |
| | 6612 | 32 | | var escapeCharacter = Regex.Escape(configuration.EscapeCharacter.ToString()); |
| | 6612 | 33 | | var separatorCharacters = configuration.ValueSeparatorChars.Select(x => Regex.Escape(x.ToString())).ToList(); |
| | | 34 | | |
| | 6612 | 35 | | var splitRegex = $"(?<!{escapeCharacter})[{string.Join(string.Empty, separatorCharacters)}]"; |
| | 6612 | 36 | | var values = Regex |
| | 6612 | 37 | | .Split(filterSyntax, splitRegex, RegexOptions.None, RegexDefaults.Timeout) |
| | 6612 | 38 | | .ToList(); |
| | | 39 | | |
| | 6612 | 40 | | return values; |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc cref="ValueFilterFactory"/> |
| | | 45 | | [Obsolete($"Use {nameof(ValueFilterFactory)} instead.")] |
| | | 46 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "MA0048:File name must match type name", Justification = "Obs |
| | | 47 | | public static class ValueFiltersFactory |
| | | 48 | | { |
| | | 49 | | /// <inheritdoc cref="ValueFilterFactory.Create(string, FilterConfiguration?)"/> |
| | | 50 | | [Obsolete($"Use {nameof(ValueFilterFactory)}.{nameof(ValueFilterFactory.Create)} instead.")] |
| | | 51 | | public static ValueFilter[] Create(string filterSyntax, FilterConfiguration? configuration = null) |
| | | 52 | | => ValueFilterFactory.Create(filterSyntax, configuration); |
| | | 53 | | } |