2 using System.Collections.Generic;
5 using System.Text.RegularExpressions;
6 using System.Threading.Tasks;
7 using Newtonsoft.Json.Serialization;
9 namespace CPE.App.Web.Elucidat
11 public static class StringExtensions {
12 private static readonly Dictionary<string, string> ToBeEncoded = new Dictionary<string, string>() {
34 private static readonly Regex ReplaceRegex = new Regex(@"[%!# $&'()*+,/:;=?@\[\]]");
37 /// .NET URL encoding functions (e.g. WebUtility.UrlEncode) do not precisely match the action of php rawurlencode(),
38 /// which is used by the Elucidat API for signature checking. Therefore we use this custom function instead when URL encoding
39 /// any data to be sent to the API or used in signature generation.
41 /// <param name="s">The string to be encoded</param>
42 /// <returns>The encoded string</returns>
43 public static string RawUrlEncode(this string s) {
44 return ReplaceRegex.Replace(s, match => ToBeEncoded[match.Value]);
48 /// Transform a .NET style property name (e.g. ProjectCode) into a PHP-style property name (e.g. project_code)
50 /// <param name="propertyName"></param>
51 /// <returns></returns>
52 public static string TransformPropertyName(this string propertyName)
54 return String.Join("_", Regex.Split(propertyName, "(?=[A-Z])").Where(x => !String.IsNullOrWhiteSpace(x))).ToLower();