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 \Civi\API\Exception\NotImplementedException
31 * @return \Civi\Api4\Generic\AbstractAction|array
33 public static function create(string $entity, string $action, array $params) {
34 switch ($params['version'] ??
NULL) {
37 'id' => self
::getNextId(),
41 'entity' => self
::normalizeEntityName($entity),
42 'action' => self
::normalizeActionName($action),
46 // For custom pseudo-entities
47 if (strpos($entity, 'Custom_') === 0) {
48 $apiRequest = \Civi\Api4\CustomValue
::$action(substr($entity, 7));
51 $callable = ["\\Civi\\Api4\\$entity", $action];
52 if (!is_callable($callable)) {
53 throw new \Civi\API\Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
55 $apiRequest = call_user_func($callable);
57 foreach ($params as $name => $param) {
58 $setter = 'set' . ucfirst($name);
59 $apiRequest->$setter($param);
64 throw new \Civi\API\Exception\
NotImplementedException("Unknown api version");
69 * Normalize entity to be CamelCase.
71 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
73 * @param string $entity
76 public static function normalizeEntityName($entity) {
77 return \CRM_Utils_String
::convertStringToCamel(\CRM_Utils_String
::munge($entity));
81 * Normalize api action name to be lowercase.
83 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
89 public static function normalizeActionName($action) {
90 return strtolower(\CRM_Utils_String
::munge($action));
93 public static function getNextId() {
94 return self
::$nextId++
;