Merge pull request #23317 from seamuslee001/php81_trim_codegen
[civicrm-core.git] / Civi / API / Request.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11 namespace Civi\API;
12
13 use Civi\Api4\Utils\CoreUtil;
14
15 /**
16 * Class Request
17 * @package Civi\API
18 */
19 class Request {
20 private static $nextId = 1;
21
22 /**
23 * Create a formatted/normalized request object.
24 *
25 * @param string $entity
26 * API entity name.
27 * @param string $action
28 * API action name.
29 * @param array $params
30 * API parameters.
31 *
32 * @throws \Civi\API\Exception\NotImplementedException
33 * @return \Civi\Api4\Generic\AbstractAction|array
34 */
35 public static function create(string $entity, string $action, array $params) {
36 switch ($params['version'] ?? NULL) {
37 case 3:
38 return [
39 'id' => self::getNextId(),
40 'version' => 3,
41 'params' => $params,
42 'fields' => NULL,
43 'entity' => self::normalizeEntityName($entity),
44 'action' => self::normalizeActionName($action),
45 ];
46
47 case 4:
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!)");
52 }
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");
57 }
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);
63 }
64 return $apiRequest;
65
66 default:
67 throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
68 }
69 }
70
71 /**
72 * Normalize entity to be CamelCase.
73 *
74 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
75 *
76 * @param string $entity
77 * @return string
78 */
79 public static function normalizeEntityName($entity) {
80 return \CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel(\CRM_Utils_String::munge($entity), TRUE);
81 }
82
83 /**
84 * Normalize api action name to be lowercase.
85 *
86 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
87 *
88 * @param $action
89 * @param $version
90 * @return string
91 */
92 public static function normalizeActionName($action) {
93 return strtolower(\CRM_Utils_String::munge($action));
94 }
95
96 public static function getNextId() {
97 return self::$nextId++;
98 }
99
100 }