+ /**
+ * Normalize/validate entity and action names
+ *
+ * @param string $entity
+ * @param int $version
+ * @return string
+ * @throws \API_Exception
+ */
+ public static function normalizeEntityName($entity, $version) {
+ if ($version <= 3) {
+ // APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
+ // We normalize entity to be CamelCase.
+ return \CRM_Utils_String::convertStringToCamel(\CRM_Utils_String::munge($entity));
+ }
+ else {
+ // APIv4 requires exact spelling & capitalization of entity/action name; deviations should cause errors
+ if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $entity)) {
+ throw new \API_Exception("Malformed entity");
+ }
+ return $entity;
+ }
+ }
+
+ public static function normalizeActionName($action, $version) {
+ if ($version <= 3) {
+ // APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
+ // We normalize action to be lowercase.
+ return strtolower(\CRM_Utils_String::munge($action));
+ }
+ else {
+ // APIv4 requires exact spelling & capitalization of entity/action name; deviations should cause errors
+ if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $action)) {
+ throw new \API_Exception("Malformed action");
+ }
+ // TODO: Not sure about camelCase actions - in v3 they are all lowercase.
+ return strtolower($action{0}) . substr($action, 1);
+ }
+ }
+