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
30 * @throws \API_Exception
32 * the request descriptor; keys:
36 * - params: array (string $key => mixed $value) [deprecated in v4]
37 * - fields: NULL|array (string $key => array $fieldSpec)
38 * - options: \CRM_Utils_OptionBag derived from params [v4-only]
39 * - data: \CRM_Utils_OptionBag derived from params [v4-only]
40 * - chains: unspecified derived from params [v4-only]
42 public static function create($entity, $action, $params) {
43 $version = \CRM_Utils_Array
::value('version', $params);
47 $apiRequest['id'] = self
::$nextId++
;
48 $apiRequest['version'] = (int) $version;
49 $apiRequest['params'] = $params;
50 $apiRequest['fields'] = NULL;
51 $apiRequest['entity'] = self
::normalizeEntityName($entity);
52 $apiRequest['action'] = self
::normalizeActionName($action);
56 $callable = ["Civi\\Api4\\$entity", $action];
57 if (!is_callable($callable)) {
58 throw new Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
60 $apiCall = call_user_func($callable);
61 $apiRequest['id'] = self
::$nextId++
;
62 unset($params['version']);
63 foreach ($params as $name => $param) {
64 $setter = 'set' . ucfirst($name);
65 $apiCall->$setter($param);
73 * Normalize entity to be CamelCase.
75 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
77 * @param string $entity
80 public static function normalizeEntityName($entity) {
81 return \CRM_Utils_String
::convertStringToCamel(\CRM_Utils_String
::munge($entity));
85 * Normalize api action name to be lowercase.
87 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
93 public static function normalizeActionName($action) {
94 return strtolower(\CRM_Utils_String
::munge($action));
97 public static function getNextId() {
98 return self
::$nextId++
;