roadie/Roadie.Api.Library/Utility/SafeParser.cs

159 lines
4.9 KiB
C#
Raw Normal View History

2018-11-03 21:21:36 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-02-03 03:26:25 +00:00
using System.Text.RegularExpressions;
2018-11-03 21:21:36 +00:00
namespace Roadie.Library.Utility
{
public static class SafeParser
{
2018-11-04 20:33:37 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Safely return a Boolean for a given Input.
/// <remarks>Has Additional String Operations</remarks>
2018-11-04 20:33:37 +00:00
/// </summary>
public static bool ToBoolean(object input)
2018-11-03 21:21:36 +00:00
{
2019-06-28 21:24:32 +00:00
switch (input)
2018-11-03 21:21:36 +00:00
{
2019-06-28 21:24:32 +00:00
case null:
2018-11-04 20:33:37 +00:00
return false;
2019-07-03 16:21:29 +00:00
2019-06-28 21:24:32 +00:00
case bool t:
return t;
2019-07-03 16:21:29 +00:00
2019-06-28 21:24:32 +00:00
default:
switch (input.ToString().ToLower())
{
case "true":
case "1":
case "y":
case "yes":
return true;
default:
return false;
}
2018-11-03 21:21:36 +00:00
}
}
public static DateTime? ToDateTime(object input)
{
2019-07-03 16:21:29 +00:00
if (input == null) return default(DateTime?);
2018-11-03 21:21:36 +00:00
try
{
2019-07-03 16:21:29 +00:00
var dt = DateTime.MinValue;
2018-11-03 21:21:36 +00:00
var i = input as string ?? input.ToString();
if (!string.IsNullOrEmpty(i))
{
2019-07-03 16:21:29 +00:00
if (Regex.IsMatch(i, @"([0-9]{4}).+([0-9]{4})")) i = i.Substring(0, 4);
2019-02-03 03:26:25 +00:00
i = Regex.Replace(i, @"(\\)", "/");
2019-06-02 04:27:17 +00:00
i = Regex.Replace(i, @"(;)", "/");
2019-02-03 03:26:25 +00:00
i = Regex.Replace(i, @"(\/+)", "/");
2019-02-03 03:33:05 +00:00
i = Regex.Replace(i, @"(-+)", "/");
2018-11-03 21:21:36 +00:00
var parts = i.Contains("/") ? i.Split('/').ToList() : new List<string> { i };
2019-07-03 16:21:29 +00:00
if (parts.Count == 2)
if (parts[0] != null && parts[1] != null && parts[0] == parts[1])
parts = new List<string> { parts[0], "01" };
while (parts.Count < 3) parts.Insert(0, "01");
2018-11-03 21:21:36 +00:00
var tsRaw = string.Empty;
foreach (var part in parts)
{
2019-07-03 16:21:29 +00:00
if (tsRaw.Length > 0) tsRaw += "/";
2018-11-03 21:21:36 +00:00
tsRaw += part;
}
2019-02-03 03:26:25 +00:00
2019-07-03 16:21:29 +00:00
DateTime.TryParse(tsRaw, out dt);
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
2018-11-03 21:21:36 +00:00
try
{
return ChangeType<DateTime?>(input);
}
catch
{
2019-06-28 21:24:32 +00:00
// ignored
2018-11-03 21:21:36 +00:00
}
2019-06-28 21:24:32 +00:00
2018-11-03 21:21:36 +00:00
return dt != DateTime.MinValue ? (DateTime?)dt : null;
}
catch
{
return default(DateTime?);
}
}
2018-11-04 20:33:37 +00:00
public static T ToEnum<T>(object input) where T : struct, IConvertible
2018-11-03 21:21:36 +00:00
{
2019-07-03 16:21:29 +00:00
if (input == null) return default(T);
2018-11-22 23:12:57 +00:00
Enum.TryParse(input.ToString(), true, out T r);
2018-11-04 20:33:37 +00:00
return r;
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
public static Guid? ToGuid(object input)
{
if (input == null) return null;
var i = input.ToString();
if (!string.IsNullOrEmpty(i) && i.Length > 0 && i[1] == ':') i = i.Substring(2, i.Length - 2);
if (!Guid.TryParse(i, out var result)) return null;
return result;
}
2018-11-03 21:21:36 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Safely Return a Number For Given Input
2018-11-03 21:21:36 +00:00
/// </summary>
2018-11-04 20:33:37 +00:00
public static T ToNumber<T>(object input)
2018-11-03 21:21:36 +00:00
{
2019-07-03 16:21:29 +00:00
if (input == null) return default(T);
2018-11-04 20:33:37 +00:00
try
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
return ChangeType<T>(input);
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
catch
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
return default(T);
2018-11-03 21:21:36 +00:00
}
}
2018-11-04 20:33:37 +00:00
public static string ToString(object input, string defaultValue = null)
2018-11-03 21:21:36 +00:00
{
2018-11-04 20:33:37 +00:00
defaultValue = defaultValue ?? string.Empty;
2019-06-28 21:24:32 +00:00
switch (input)
2018-11-04 20:33:37 +00:00
{
2019-06-28 21:24:32 +00:00
case null:
return defaultValue;
2019-07-03 16:21:29 +00:00
2019-06-28 21:24:32 +00:00
case string r:
return r.Trim();
2019-07-03 16:21:29 +00:00
2019-06-28 21:24:32 +00:00
default:
return defaultValue;
2018-11-04 20:33:37 +00:00
}
2018-11-03 21:21:36 +00:00
}
public static int? ToYear(string input)
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(input)) return null;
2019-06-28 21:24:32 +00:00
int parsed;
2018-11-03 21:21:36 +00:00
if (input.Length == 4)
{
2019-07-03 16:21:29 +00:00
if (int.TryParse(input, out parsed)) return parsed > 0 ? (int?)parsed : null;
2018-11-03 21:21:36 +00:00
}
else if (input.Length > 4)
{
2019-07-03 16:21:29 +00:00
if (int.TryParse(input.Substring(0, 4), out parsed)) return parsed > 0 ? (int?)parsed : null;
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
2018-11-03 21:21:36 +00:00
return null;
}
2019-07-03 16:21:29 +00:00
private static T ChangeType<T>(object value)
{
var t = typeof(T);
if (!t.IsGenericType || t.GetGenericTypeDefinition() != typeof(Nullable<>))
return (T)Convert.ChangeType(value, t);
if (value == null) return default(T);
t = Nullable.GetUnderlyingType(t);
return (T)Convert.ChangeType(value, t);
}
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
}