You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

  1. using Apewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. namespace Apewer.Models
  7. {
  8. /// <summary></summary>
  9. [Serializable]
  10. public class StringPairs : List<KeyValuePair<string, string>>, IToJson
  11. {
  12. /// <summary>添加项。返回错误信息。</summary>
  13. public string Add(string key, string value)
  14. {
  15. if (string.IsNullOrEmpty(key)) return "参数 Name 无效。";
  16. // if (string.IsNullOrEmpty(value)) return "参数 Value 无效。";
  17. var kvp = new KeyValuePair<string, string>(key, value);
  18. Add(kvp);
  19. return null;
  20. }
  21. /// <summary>获取所有 Key。</summary>
  22. public string[] GetAllKeys()
  23. {
  24. var keys = new string[Count];
  25. for (var i = 0; i < Count; i++) keys[i] = this[i].Key;
  26. return keys;
  27. }
  28. /// <summary>获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。</summary>
  29. public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true)
  30. {
  31. if (string.IsNullOrEmpty(key)) return new string[0];
  32. var lowerArg = TextUtility.ToLower(key);
  33. var values = new List<string>();
  34. foreach (var item in this)
  35. {
  36. if (item.Key == key)
  37. {
  38. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  39. continue;
  40. }
  41. if (ignoreCase)
  42. {
  43. var lowerItem = TextUtility.ToLower(item.Key);
  44. if (lowerItem == lowerArg)
  45. {
  46. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  47. continue;
  48. }
  49. }
  50. }
  51. return values.ToArray();
  52. }
  53. /// <summary>获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。</summary>
  54. public string GetValue(string key, bool ignoreCase = true)
  55. {
  56. if (string.IsNullOrEmpty(key)) return null;
  57. // 先精准匹配。
  58. foreach (var item in this)
  59. {
  60. if (string.IsNullOrEmpty(item.Key)) continue;
  61. if (item.Key == key)
  62. {
  63. if (string.IsNullOrEmpty(item.Value)) continue;
  64. return item.Value;
  65. }
  66. }
  67. // 再模糊匹配。
  68. if (ignoreCase)
  69. {
  70. var lowerArg = TextUtility.ToLower(key);
  71. foreach (var item in this)
  72. {
  73. if (string.IsNullOrEmpty(item.Key)) continue;
  74. var lowerKey = TextUtility.ToLower(item.Key);
  75. if (lowerKey == lowerArg)
  76. {
  77. if (string.IsNullOrEmpty(item.Value)) continue;
  78. return item.Value;
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. /// <summary>对 Key 升序排序。</summary>
  85. public void Sort()
  86. {
  87. SortAsc();
  88. }
  89. /// <summary>对 Key 升序排序。</summary>
  90. public void SortAsc()
  91. {
  92. base.Sort(new Comparison<KeyValuePair<string, string>>((a, b) => a.Key.CompareTo(b.Key)));
  93. }
  94. /// <summary>对 Key 降序排序。</summary>
  95. public void SortDesc()
  96. {
  97. base.Sort(new Comparison<KeyValuePair<string, string>>((b, a) => a.Key.CompareTo(b.Key)));
  98. }
  99. /// <summary>检查拥有指定的 Key。</summary>
  100. public bool HasKey(string key, bool ignoreCase = false)
  101. {
  102. var a = ignoreCase ? key.SafeLower() : key;
  103. if (string.IsNullOrEmpty(a))
  104. {
  105. foreach (var k in GetAllKeys())
  106. {
  107. if (string.IsNullOrEmpty(k)) return true;
  108. }
  109. }
  110. else
  111. {
  112. foreach (var k in GetAllKeys())
  113. {
  114. var b = ignoreCase ? k.SafeLower() : k;
  115. if (a == b) return true;
  116. }
  117. }
  118. return false;
  119. }
  120. /// <summary></summary>
  121. public Json ToJson() => ToJson(false);
  122. /// <summary></summary>
  123. public Json ToJson(bool lowerKey)
  124. {
  125. var array = Json.NewArray();
  126. foreach (var pair in this)
  127. {
  128. var item = Json.NewObject();
  129. item[lowerKey ? "key" : "Key"] = pair.Key ?? "";
  130. item[lowerKey ? "value" : "Value"] = pair.Value ?? "";
  131. array.AddItem(item);
  132. }
  133. return array;
  134. }
  135. /// <summary>从指定对象解析,生成 StringPairs 对象实例为副本。</summary>
  136. public static StringPairs From(NameValueCollection collection)
  137. {
  138. var sp = new StringPairs();
  139. if (collection != null)
  140. {
  141. sp.Capacity = collection.Count;
  142. foreach (var key in collection.AllKeys)
  143. {
  144. sp.Add(key, collection[key]);
  145. }
  146. sp.Capacity = sp.Count;
  147. }
  148. return sp;
  149. }
  150. }
  151. }