}
/**
- * Convert possibly underscore separated words to camel case.
+ * Convert possibly underscore, space or dash separated words to CamelCase.
*
* @param string $str
* @param bool $ucFirst
* @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);
}
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.
*
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");