Afform - Sync string functions with CRM_Utils_String
authorColeman Watts <coleman@civicrm.org>
Mon, 19 Sep 2022 15:54:40 +0000 (11:54 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 19 Sep 2022 15:54:40 +0000 (11:54 -0400)
CRM/Utils/String.php
ext/afform/core/afform.php

index 4b230e1879ce9e9eb82089e04efd7d4bce856689..c1673746254673dedb9f88593dc713a1b24a8486 100644 (file)
@@ -88,7 +88,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Convert possibly underscore separated words to camel case.
+   * Convert possibly underscore, space or dash separated words to CamelCase.
    *
    * @param string $str
    * @param bool $ucFirst
@@ -96,7 +96,7 @@ class CRM_Utils_String {
    * @return string
    */
   public static function convertStringToCamel($str, $ucFirst = TRUE) {
-    $fragments = explode('_', $str);
+    $fragments = preg_split('/[-_ ]/', $str, -1, PREG_SPLIT_NO_EMPTY);
     $camel = implode('', array_map('ucfirst', $fragments));
     return $ucFirst ? $camel : lcfirst($camel);
   }
@@ -111,6 +111,16 @@ class CRM_Utils_String {
     return strtolower(ltrim(preg_replace('/(?=[A-Z])/', '_$0', $str), '_'));
   }
 
+  /**
+   * Converts `CamelCase` or `snake_case` to `dash-format`
+   *
+   * @param string $str
+   * @return string
+   */
+  public static function convertStringToDash(string $str): string {
+    return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
+  }
+
   /**
    * Takes a variable name and munges it randomly into another variable name.
    *
index 16c204bada3a73c0698bac7bb3d7d2dc989b8738..9aae5d998332e9d8cb74347058b8f6c4d4970d95 100644 (file)
@@ -500,14 +500,10 @@ function _afform_clear() {
 function _afform_angular_module_name($fileBaseName, $format = 'camel') {
   switch ($format) {
     case 'camel':
-      $camelCase = '';
-      foreach (preg_split('/[-_ ]/', $fileBaseName, -1, PREG_SPLIT_NO_EMPTY) as $shortNamePart) {
-        $camelCase .= ucfirst($shortNamePart);
-      }
-      return strtolower($camelCase[0]) . substr($camelCase, 1);
+      return \CRM_Utils_String::convertStringToCamel($fileBaseName, FALSE);
 
     case 'dash':
-      return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
+      return \CRM_Utils_String::convertStringToDash($fileBaseName);
 
     default:
       throw new \Exception("Unrecognized format");