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\Event\CreateApi4RequestEvent
;
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 // Load the API kernel service for registering API providers, as
49 // otherwise subscribers to the civi.api4.createRequest event registered
50 // through the EventSubscriberInterface will not be registered.
51 \Civi
::service('civi_api_kernel');
52 $e = new CreateApi4RequestEvent($entity);
53 \Civi
::dispatcher()->dispatch('civi.api4.createRequest', $e);
54 $callable = [$e->className
, $action];
55 if (!$e->className ||
!is_callable($callable)) {
56 throw new \Civi\API\Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
58 // Check enabled components
59 $daoName = \CRM_Core_DAO_AllCoreTables
::getFullName($entity);
60 if ($daoName && defined("{$daoName}::COMPONENT") && !\CRM_Core_Component
::isEnabled($daoName::COMPONENT
)) {
61 throw new \Civi\API\Exception\
NotImplementedException("$entity API is not available because " . $daoName::COMPONENT
. " component is disabled");
63 $apiRequest = call_user_func_array($callable, $e->args
);
64 foreach ($params as $name => $param) {
65 $setter = 'set' . ucfirst($name);
66 $apiRequest->$setter($param);
71 throw new \Civi\API\Exception\
NotImplementedException("Unknown api version");
76 * Normalize entity to be CamelCase.
78 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
80 * @param string $entity
83 public static function normalizeEntityName($entity) {
84 return \CRM_Core_DAO_AllCoreTables
::convertEntityNameToCamel(\CRM_Utils_String
::munge($entity), TRUE);
88 * Normalize api action name to be lowercase.
90 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
96 public static function normalizeActionName($action) {
97 return strtolower(\CRM_Utils_String
::munge($action));
100 public static function getNextId() {
101 return self
::$nextId++
;