Merge pull request #22606 from colemanw/relationshipPseudoFields
[civicrm-core.git] / CRM / Utils / String.php
index 8e761a5be8231f11393b9cc4accd14d4c817571c..5ee79d831c24d622da13a9e617cd63a1d92216a6 100644 (file)
@@ -99,6 +99,16 @@ class CRM_Utils_String {
     return $ucFirst ? $camel : lcfirst($camel);
   }
 
+  /**
+   * Inverse of above function, converts camelCase to snake_case
+   *
+   * @param string $str
+   * @return string
+   */
+  public static function convertStringToSnakeCase(string $str): string {
+    return strtolower(ltrim(preg_replace('/(?=[A-Z])/', '_$0', $str), '_'));
+  }
+
   /**
    * Takes a variable name and munges it randomly into another variable name.
    *
@@ -670,7 +680,7 @@ class CRM_Utils_String {
     $alphabetSize = strlen($alphabet);
     $result = '';
     for ($i = 0; $i < $len; $i++) {
-      $result .= $alphabet{rand(1, $alphabetSize) - 1};
+      $result .= $alphabet[rand(1, $alphabetSize) - 1];
     }
     return $result;
   }
@@ -680,10 +690,10 @@ class CRM_Utils_String {
    * "admin foo" => array(NULL,"admin foo")
    * "cms:admin foo" => array("cms", "admin foo")
    *
-   * @param $delim
+   * @param string $delim
    * @param string $string
    *   E.g. "view all contacts". Syntax: "[prefix:]name".
-   * @param null $defaultPrefix
+   * @param string|null $defaultPrefix
    *
    * @return array
    *   (0 => string|NULL $prefix, 1 => string $value)
@@ -941,7 +951,7 @@ class CRM_Utils_String {
    * safe, standard data interchange formats such as JSON rather than PHP's
    * serialization format when dealing with user input.
    *
-   * @param string|NULL $string
+   * @param string|null $string
    *
    * @return mixed
    */
@@ -1028,7 +1038,9 @@ class CRM_Utils_String {
     $cachingValue = $smarty->caching;
     $smarty->caching = 0;
     $smarty->assign('smartySingleUseString', $templateString);
-    $templateString = $smarty->fetch('string:{eval var=$smartySingleUseString}');
+    // Do not escape the smartySingleUseString as that is our smarty template
+    // and is likely to contain html.
+    $templateString = (string) $smarty->fetch('string:{eval var=$smartySingleUseString|smarty:nodefaults}');
     $smarty->caching = $cachingValue;
     $smarty->assign('smartySingleUseString', NULL);
     return $templateString;