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 $className = CoreUtil
::getApiClass($entity);
49 $callable = [$className, $action];
50 if (!$className ||
!is_callable($callable)) {
51 throw new \Civi\API\Exception\
NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
53 // Check enabled components
54 $daoName = \CRM_Core_DAO_AllCoreTables
::getFullName($entity);
55 if ($daoName && defined("{$daoName}::COMPONENT") && !\CRM_Core_Component
::isEnabled($daoName::COMPONENT
)) {
56 throw new \Civi\API\Exception\
NotImplementedException("$entity API is not available because " . $daoName::COMPONENT
. " component is disabled");
58 $args = (array) CoreUtil
::getInfoItem($entity, 'class_args');
59 $apiRequest = call_user_func_array($callable, $args);
60 foreach ($params as $name => $param) {
61 $setter = 'set' . ucfirst($name);
62 $apiRequest->$setter($param);
67 throw new \Civi\API\Exception\
NotImplementedException("Unknown api version");
72 * Normalize entity to be CamelCase.
74 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
76 * @param string $entity
79 public static function normalizeEntityName($entity) {
80 return \CRM_Core_DAO_AllCoreTables
::convertEntityNameToCamel(\CRM_Utils_String
::munge($entity), TRUE);
84 * Normalize api action name to be lowercase.
86 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
92 public static function normalizeActionName($action) {
93 return strtolower(\CRM_Utils_String
::munge($action));
96 public static function getNextId() {
97 return self
::$nextId++
;