| | | 1 | | using TeleFlow.Annotations; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 4 | | |
| | | 5 | | internal sealed class TelegramTextFilter |
| | | 6 | | { |
| | | 7 | | public TelegramTextFilter(string value, TextMatchMode mode, bool ignoreCase) |
| | | 8 | | { |
| | 161 | 9 | | ArgumentException.ThrowIfNullOrWhiteSpace(value); |
| | | 10 | | |
| | | 11 | | Value = value; |
| | | 12 | | Mode = mode; |
| | | 13 | | IgnoreCase = ignoreCase; |
| | 161 | 14 | | } |
| | | 15 | | |
| | | 16 | | public string Value { get; } |
| | | 17 | | |
| | | 18 | | public TextMatchMode Mode { get; } |
| | | 19 | | |
| | | 20 | | public bool IgnoreCase { get; } |
| | | 21 | | |
| | | 22 | | public bool Matches(string? text) |
| | | 23 | | { |
| | 152 | 24 | | if (text is null) |
| | | 25 | | { |
| | 0 | 26 | | return false; |
| | | 27 | | } |
| | | 28 | | |
| | 152 | 29 | | var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| | | 30 | | |
| | 152 | 31 | | return Mode switch |
| | 152 | 32 | | { |
| | 152 | 33 | | TextMatchMode.Equals => string.Equals(text, Value, comparison), |
| | 0 | 34 | | TextMatchMode.StartsWith => text.StartsWith(Value, comparison), |
| | 0 | 35 | | TextMatchMode.Contains => text.Contains(Value, comparison), |
| | 0 | 36 | | _ => throw new InvalidOperationException($"Unsupported text match mode '{Mode}'.") |
| | 152 | 37 | | }; |
| | | 38 | | } |
| | | 39 | | } |