PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MvcExtensions/Model/MyText.cs

https://github.com/SchokoMousse/MVCExtensions
C# | 336 lines | 289 code | 47 blank | 0 comment | 24 complexity | a96b3a6dc2ac0a71b02fcabc7b391e0b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace MvcExtensions.Model
  6. {
  7. public static class StringExtensions
  8. {
  9. public static string IsNotEmpty(this string me)
  10. {
  11. return string.IsNullOrEmpty(me)?"Value can not be empty":null;
  12. }
  13. public static string IsLengthSmallerThen( this string me,int length)
  14. {
  15. return me==null||me.Length<=length?null:"Value is too long (max "+length.ToString()+ " characters";
  16. }
  17. public static string IsRegexMatch( this string me,string regex,string msg)
  18. {
  19. return me==null||Regex.IsMatch(me,regex)?null:msg;
  20. }
  21. }
  22. public abstract class MyText
  23. {
  24. public MyText()
  25. {
  26. _values = new string[] { null};
  27. }
  28. protected virtual int GetValueIndex
  29. {
  30. get { return 0; }
  31. }
  32. protected string[] _values;
  33. public virtual string Value
  34. {
  35. get { return DoGetValue(); }
  36. set { DoSetValue(value); }
  37. }
  38. public override bool Equals(object obj)
  39. {
  40. if (Value == null) return obj == null;
  41. return Value.Equals(obj);
  42. }
  43. public override int GetHashCode()
  44. {
  45. return Value.GetHashCode();
  46. }
  47. public override string ToString()
  48. {
  49. if (Value == null) return Value;
  50. return Value.ToString();
  51. }
  52. public static implicit operator string(MyText s)
  53. {
  54. if (s == null) return null;
  55. return s.Value;
  56. }
  57. protected static T Assign<T>(string s) where T : MyText, new()
  58. {
  59. var res = new T();
  60. res.Value = s;
  61. return res;
  62. }
  63. public virtual void DoSetValue(string v)
  64. {
  65. _values[GetValueIndex] = v;
  66. }
  67. public virtual string DoGetValue()
  68. {
  69. return _values[GetValueIndex];
  70. }
  71. }
  72. public abstract class MyValidatedText : MyText
  73. {
  74. protected List<Func<string,string>> ValidationChecks = new List<Func<string,string>>();
  75. public MyValidatedText(params Func<string,string>[] ValidationChecks)
  76. {
  77. this.ValidationChecks.AddRange(ValidationChecks);
  78. }
  79. public string Validate(string value,bool throwex)
  80. {
  81. var val= ValidationChecks.Select(x => x(value)).FirstOrDefault();
  82. if (val != null && throwex)
  83. throw new ArgumentException(val);
  84. else
  85. return val;
  86. }
  87. public override void DoSetValue(string value)
  88. {
  89. Validate(value,true);
  90. base.DoSetValue(value);
  91. }
  92. }
  93. public abstract class MyValidatedXlatText : MyValidatedText
  94. {
  95. protected MyValidatedXlatText(params Func<string, string>[] ValidationChecks) : base(ValidationChecks)
  96. {
  97. _values = new string[] { null, null, null, null,null };
  98. }
  99. protected override int GetValueIndex
  100. {
  101. get
  102. {
  103. var x = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
  104. switch (x)
  105. {
  106. case "fr": return 1; break;
  107. case "en": return 2; break;
  108. case "de": return 3; break;
  109. case "pl": return 4; break;
  110. default: return 0;
  111. }
  112. }
  113. }
  114. public override string DoGetValue()
  115. {
  116. var x = _values[GetValueIndex];
  117. return string.IsNullOrEmpty(x)?"[NL]"+NL:x;
  118. }
  119. public override void DoSetValue(string value)
  120. {
  121. Validate(value, true);
  122. _values[GetValueIndex] = value;
  123. if (!string.IsNullOrEmpty(value) && NL == null)
  124. NL = value;
  125. }
  126. public virtual string NL { get { return _values[0]; } set { _values[0] = value; } }
  127. public virtual string FR { get { return _values[1]; } set { _values[1] = value; } }
  128. public virtual string EN { get { return _values[2]; } set { _values[2] = value; } }
  129. public virtual string DE { get { return _values[3]; } set { _values[3] = value; } }
  130. public virtual string PL { get { return _values[4]; } set { _values[4] = value; } }
  131. }
  132. public class ShortText : MyValidatedText
  133. {
  134. public ShortText() : base(x => x.IsLengthSmallerThen(16)) { }
  135. public static implicit operator string(ShortText s)
  136. {
  137. return s == null ? null : s.Value;
  138. }
  139. public static implicit operator ShortText(string value)
  140. {
  141. return new ShortText() { Value = value };
  142. }
  143. }
  144. public class NonEmptyShortText : MyValidatedText
  145. {
  146. public NonEmptyShortText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(16)) { }
  147. public static implicit operator string(NonEmptyShortText s)
  148. {
  149. return s == null ? null : s.Value;
  150. }
  151. public static implicit operator NonEmptyShortText(string value)
  152. {
  153. return new NonEmptyShortText() { Value = value };
  154. }
  155. }
  156. public class NormalText : MyValidatedText
  157. {
  158. public NormalText() : base(x => x.IsLengthSmallerThen(256)) { }
  159. public static implicit operator string(NormalText s)
  160. {
  161. return s == null ? null : s.Value;
  162. }
  163. public static implicit operator NormalText(string value)
  164. {
  165. return new NormalText() { Value = value };
  166. }
  167. }
  168. public class NonEmptyNormalText : MyValidatedText
  169. {
  170. public NonEmptyNormalText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(256)) { }
  171. public static implicit operator string(NonEmptyNormalText s)
  172. {
  173. return s == null ? null : s.Value;
  174. }
  175. public static implicit operator NonEmptyNormalText(string value)
  176. {
  177. return new NonEmptyNormalText() { Value = value };
  178. }
  179. }
  180. public class LongText : MyValidatedText
  181. {
  182. public LongText() : base(x => x.IsLengthSmallerThen(1024)) { }
  183. public static implicit operator LongText(string value)
  184. {
  185. return new LongText() { Value = value };
  186. }
  187. }
  188. public class NonEmptyLongText : MyValidatedText
  189. {
  190. public NonEmptyLongText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(1024)) { }
  191. public static implicit operator NonEmptyLongText(string value)
  192. {
  193. return new NonEmptyLongText() { Value = value };
  194. }
  195. }
  196. public class MemoText : MyValidatedText
  197. {
  198. public MemoText() : base() { }
  199. public static implicit operator MemoText(string value)
  200. {
  201. return new MemoText() { Value = value};
  202. }
  203. }
  204. public class NonEmptyMemoText : MyValidatedText
  205. {
  206. public NonEmptyMemoText() : base(x => x.IsNotEmpty()) { }
  207. public static implicit operator string(NonEmptyMemoText s)
  208. {
  209. return s == null ? null : s.Value;
  210. }
  211. public static implicit operator NonEmptyMemoText(string value)
  212. {
  213. return new NonEmptyMemoText() { Value = value };
  214. }
  215. }
  216. public class EmailText : MyValidatedText
  217. {
  218. public EmailText():base(x=>x.IsRegexMatch(@"[a-z0-9._%-]+@[a-z0-9.-]+\.[A-Za-z]{2,4}","Invalid email address")) {}
  219. public override void DoSetValue(string value)
  220. {
  221. if (value!=null) value = value.ToLowerInvariant();
  222. base.DoSetValue(value);
  223. }
  224. public static implicit operator string(EmailText s)
  225. {
  226. return s == null ? null : s.Value;
  227. }
  228. public static implicit operator EmailText(string value)
  229. {
  230. return new EmailText() { Value = value };
  231. }
  232. }
  233. public class XShortText : MyValidatedXlatText
  234. {
  235. public XShortText() : base(x => x.IsLengthSmallerThen(16)) { }
  236. }
  237. public class XNonEmptyShortText : MyValidatedXlatText
  238. {
  239. public XNonEmptyShortText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(16)) { }
  240. public static implicit operator string(XNonEmptyShortText s)
  241. {
  242. return s == null ? null : s.Value;
  243. }
  244. public static implicit operator XNonEmptyShortText(string value)
  245. {
  246. return new XNonEmptyShortText() { Value = value };
  247. }
  248. }
  249. public class XNormalText : MyValidatedXlatText
  250. {
  251. public XNormalText() : base(x => x.IsLengthSmallerThen(256)) { }
  252. }
  253. public class XNonEmptyNormalText : MyValidatedXlatText
  254. {
  255. public XNonEmptyNormalText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(256)) { }
  256. public static implicit operator string(XNonEmptyNormalText s)
  257. {
  258. return s == null ? null : s.Value;
  259. }
  260. public static implicit operator XNonEmptyNormalText(string value)
  261. {
  262. return new XNonEmptyNormalText() { Value = value };
  263. }
  264. }
  265. public class XLongText : MyValidatedXlatText
  266. {
  267. public XLongText() : base(x => x.IsLengthSmallerThen(1024)) { }
  268. public static implicit operator string(XLongText s)
  269. {
  270. return s == null ? null : s.Value;
  271. }
  272. public static implicit operator XLongText(string value)
  273. {
  274. return new XLongText() { Value = value };
  275. }
  276. }
  277. public class XNonEmptyLongText : MyValidatedXlatText
  278. {
  279. public XNonEmptyLongText() : base(x => x.IsNotEmpty(), x => x.IsLengthSmallerThen(1024)) { }
  280. }
  281. public class XMemoText : MyValidatedXlatText
  282. {
  283. public XMemoText() : base() { }
  284. }
  285. public class XNonEmptyMemoText : MyValidatedXlatText
  286. {
  287. public XNonEmptyMemoText() : base(x => x.IsNotEmpty()) { }
  288. }
  289. }