Merge pull request #22255 from MegaphoneJon/membership-14-2021
[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\Event\CreateApi4RequestEvent;
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 // 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!)");
57 }
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");
62 }
63 $apiRequest = call_user_func_array($callable, $e->args);
64 foreach ($params as $name => $param) {
65 $setter = 'set' . ucfirst($name);
66 $apiRequest->$setter($param);
67 }
68 return $apiRequest;
69
70 default:
71 throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
72 }
73 }
74
75 /**
76 * Normalize entity to be CamelCase.
77 *
78 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
79 *
80 * @param string $entity
81 * @return string
82 */
83 public static function normalizeEntityName($entity) {
84 return \CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel(\CRM_Utils_String::munge($entity), TRUE);
85 }
86
87 /**
88 * Normalize api action name to be lowercase.
89 *
90 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
91 *
92 * @param $action
93 * @param $version
94 * @return string
95 */
96 public static function normalizeActionName($action) {
97 return strtolower(\CRM_Utils_String::munge($action));
98 }
99
100 public static function getNextId() {
101 return self::$nextId++;
102 }
103
104 }