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 +--------------------------------------------------------------------+
13 use Civi\Api4\Utils\CoreUtil
;
20 private static $nextId = 1;
23 * Create a formatted/normalized request object.
25 * @param string $entity
27 * @param string $action
29 * @param array $params
32 * @throws \Civi\API\Exception\NotImplementedException
33 * @return \Civi\Api4\Generic\AbstractAction|array
35 public static function create(string $entity, string $action, array $params) {
36 switch ($params['version'] ??
NULL) {
39 'id' => self
::getNextId(),
43 'entity' => self
::normalizeEntityName($entity),
44 'action' => self
::normalizeActionName($action),
48 // For custom pseudo-entities
49 if (strpos($entity, 'Custom_') === 0) {
50 $apiRequest = \Civi\Api4\CustomValue
::$action(substr($entity, 7));
53 $callable = [CoreUtil
::getApiClass($entity), $action];
54 if (!is_callable($callable)) {
55 throw new \Civi\API\Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
57 $apiRequest = call_user_func($callable);
59 foreach ($params as $name => $param) {
60 $setter = 'set' . ucfirst($name);
61 $apiRequest->$setter($param);
66 throw new \Civi\API\Exception\
NotImplementedException("Unknown api version");
71 * Normalize entity to be CamelCase.
73 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
75 * @param string $entity
78 public static function normalizeEntityName($entity) {
79 return \CRM_Core_DAO_AllCoreTables
::convertEntityNameToCamel(\CRM_Utils_String
::munge($entity), TRUE);
83 * Normalize api action name to be lowercase.
85 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
91 public static function normalizeActionName($action) {
92 return strtolower(\CRM_Utils_String
::munge($action));
95 public static function getNextId() {
96 return self
::$nextId++
;