From 4ce53ba3de860a5d9aac1b1bbd22b7a29e839af2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 19 Sep 2022 11:54:40 -0400 Subject: [PATCH] Afform - Sync string functions with CRM_Utils_String --- CRM/Utils/String.php | 14 ++++++++++++-- ext/afform/core/afform.php | 8 ++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index 4b230e1879..c167374625 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -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. * diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 16c204bada..9aae5d9983 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -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"); -- 2.25.1