Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Make apiv3-specific helper generally available. #17169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Admin/Page/APIExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static function getDocBlock($entity, $action) {
// Fetch block for a specific action
else {
$action = strtolower($action);
$fnName = 'civicrm_api3_' . _civicrm_api_get_entity_name_from_camel($entity) . '_' . $action;
$fnName = 'civicrm_api3_' . CRM_Utils_String::convertEntityToLowerCase($entity) . '_' . $action;
// Support the alternate "1 file per action" structure
$actionFile = "api/v3/$entity/" . ucfirst($action) . '.php';
$actionFileContents = file_get_contents("api/v3/$entity/" . ucfirst($action) . '.php', FILE_USE_INCLUDE_PATH);
Expand Down
25 changes: 25 additions & 0 deletions CRM/Utils/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ public static function convertStringToCamel($string) {
return implode('', $fragments);
}

/**
* Convert CamelCase to snake_case, with special handling for some entity names.
*
* Eg. Activity returns activity
* UFGroup returns uf_group
* OptionValue returns option_value
*
* @param string $entity
*
* @return string
*/
public static function convertEntityToLowerCase(string $entity): string {
if ($entity === strtolower($entity)) {
return $entity;
}
if ($entity === 'PCP') {
return 'pcp';
}
return strtolower(ltrim(str_replace('U_F',
'uf',
// That's CamelCase, beside an odd UFCamel that is expected as uf_camel
preg_replace('/(?=[A-Z])/', '_$0', $entity)
), '_'));
}

/**
* Takes a variable name and munges it randomly into another variable name.
*
Expand Down
19 changes: 6 additions & 13 deletions api/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,15 @@ function _civicrm_api_replace_variable($value, $parentResult, $separator) {
*
* @return string
* Entity name in underscore separated format.
*
* @deprecated
*/
function _civicrm_api_get_entity_name_from_camel($entity) {
if (!$entity || $entity === strtolower($entity)) {
return $entity;
}
elseif ($entity == 'PCP') {
return 'pcp';
}
else {
$entity = ltrim(strtolower(str_replace('U_F',
'uf',
// That's CamelCase, beside an odd UFCamel that is expected as uf_camel
preg_replace('/(?=[A-Z])/', '_$0', $entity)
)), '_');
if (!$entity) {
// @todo - this should not be called when empty.
return '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally add deprecation noise if this is hit

}
return $entity;
return CRM_Utils_String::convertEntityToLowerCase($entity);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,7 @@ function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create'
if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
return $fieldName;
}
if ($fieldName == _civicrm_api_get_entity_name_from_camel($entity) . '_id') {
if ($fieldName === (CRM_Utils_String::convertEntityToLowerCase($entity) . '_id')) {
return 'id';
}
$result = civicrm_api($entity, 'getfields', [
Expand Down