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.

178 lines
5.5 KiB

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