using System.Collections.Generic; namespace AnP.Utils{ public class Common{ public static readonly Options GET_DICTIONARY_OPTIONS = new Options(Options.NO_OVERWRITE); public static readonly Options GET_OPTIONS = new Options(Options.ALLOW_NULLS); public static List get_strings(object? items){ List strings = new List(); if(items != null){ if(Check.is_string(items)) strings.Add((string)items); else if(Check.is_array(items)) foreach(object item in (IEnumerable)items) if(Check.is_string(item)) strings.Add((string)item); } return strings; } public static List get_keys(object? items){ List keys = new List(); if(items != null){ if(Check.is_key(items)) keys.Add((string)items); else if(Check.is_array(items)) foreach(object item in (IEnumerable)items) if(Check.is_key(item)){ string key = (string)item; if(!keys.Contains(key)) keys.Add(key); }; } return keys; } public static List> get_dictionaries(object? items){ List> dictionaries = new List>(); if(items != null){ if(Check.is_dictionary(items)) dictionaries.Add((Dictionary)items); else if(Check.is_array(items)) foreach(object item in (IEnumerable)items) if(Check.is_dictionary(item)) dictionaries.Add((IDictionary)item); } return dictionaries; } public static Dictionary get_dictionary(object? items, int custom_options = 0){ Dictionary dictionary = new Dictionary(); Options options = new Options(custom_options, GET_DICTIONARY_OPTIONS); if(items != null){ if(Check.is_dictionary(items)) dictionary = (Dictionary)items; else if(Check.is_array(items)) foreach(object item in (IEnumerable)items) if(Check.is_dictionary(item)) foreach(KeyValuePair pair in (IDictionary)item) if(options.overwrite == true || !dictionary.ContainsKey(pair.Key)) dictionary[pair.Key] = pair.Value; } return dictionary; } public static T? get(object keys, object inputs, T? _default = default(T?), int custom_options = 0){ Options options = new Options(custom_options, GET_OPTIONS); List keys_list = get_keys(keys); if(keys_list.Count != 0) foreach(IDictionary subinputs in get_dictionaries(inputs)) foreach(string key in keys_list) if(subinputs.ContainsKey(key) && ( options.allow_null == true || subinputs[key] != null )) return subinputs[key]; return _default; } } }