3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
18 private static $nextId = 1;
21 * Create a formatted/normalized request object.
23 * @param string $entity
25 * @param string $action
27 * @param array $params
32 * @throws \API_Exception
34 * the request descriptor; keys:
38 * - params: array (string $key => mixed $value) [deprecated in v4]
39 * - extra: unspecified
40 * - fields: NULL|array (string $key => array $fieldSpec)
41 * - options: \CRM_Utils_OptionBag derived from params [v4-only]
42 * - data: \CRM_Utils_OptionBag derived from params [v4-only]
43 * - chains: unspecified derived from params [v4-only]
45 public static function create($entity, $action, $params, $extra = NULL) {
46 $version = \CRM_Utils_Array
::value('version', $params);
50 $apiRequest['id'] = self
::$nextId++
;
51 $apiRequest['version'] = (int) $version;
52 $apiRequest['params'] = $params;
53 $apiRequest['extra'] = $extra;
54 $apiRequest['fields'] = NULL;
55 $apiRequest['entity'] = self
::normalizeEntityName($entity, $apiRequest['version']);
56 $apiRequest['action'] = self
::normalizeActionName($action, $apiRequest['version']);
60 $callable = ["Civi\\Api4\\$entity", $action];
61 if (!is_callable($callable)) {
62 throw new Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
64 $apiCall = call_user_func($callable);
65 $apiRequest['id'] = self
::$nextId++
;
66 unset($params['version']);
67 foreach ($params as $name => $param) {
68 $setter = 'set' . ucfirst($name);
69 $apiCall->$setter($param);
77 * Normalize entity to be CamelCase.
79 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
81 * @param string $entity
85 public static function normalizeEntityName($entity, $version) {
86 return \CRM_Utils_String
::convertStringToCamel(\CRM_Utils_String
::munge($entity));
90 * Normalize api action name to be lowercase.
92 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
98 public static function normalizeActionName($action, $version) {
99 return strtolower(\CRM_Utils_String
::munge($action));
102 public static function getNextId() {
103 return self
::$nextId++
;