using System; using System.Collections.Generic; using SabreTools.Models.Metadata; namespace SabreTools.Filter { /// /// Represents a set of filters that can be run against an object /// public class FilterRunner { /// /// Set of filters to be run against an object /// public FilterObject[] Filters { get; init; } public FilterRunner(FilterObject[]? filters) { if (filters == null) throw new ArgumentNullException(nameof(filters)); this.Filters = filters; } public FilterRunner(string[]? filterStrings) { if (filterStrings == null) throw new ArgumentNullException(nameof(filterStrings)); var filters = new List(); foreach (string filterString in filterStrings) { filters.Add(new FilterObject(filterString)); } this.Filters = filters.ToArray(); } /// /// Run filtering on a DictionaryBase item /// public bool Run(DictionaryBase dictionaryBase) { string? itemName = dictionaryBase switch { Header => MetadataFile.HeaderKey, Machine => MetadataFile.MachineKey, DatItem => TypeHelper.GetXmlRootAttributeElementName(dictionaryBase.GetType()), _ => null, }; // Loop through and run each filter in order foreach (var filter in this.Filters) { // If the filter isn't for this object type, skip if (filter.Key[0] != itemName) continue; // If we don't get a match, it's a failure if (!filter.Matches(dictionaryBase)) return false; } return true; } } }