Merge pull request #22210 from MegaphoneJon/optional-before-required
[civicrm-core.git] / Civi / API / Request.php
CommitLineData
d3159a21
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
d3159a21 5 | |
41498ac5
TO
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 |
d3159a21 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
d3159a21
TO
11namespace Civi\API;
12
8badd1d8 13use Civi\Api4\Event\CreateApi4RequestEvent;
eb378b8a 14
6550386a
EM
15/**
16 * Class Request
17 * @package Civi\API
18 */
d3159a21 19class Request {
5558f278
TO
20 private static $nextId = 1;
21
d3159a21
TO
22 /**
23 * Create a formatted/normalized request object.
24 *
25 * @param string $entity
8882ff5c 26 * API entity name.
d3159a21 27 * @param string $action
8882ff5c 28 * API action name.
d3159a21 29 * @param array $params
8882ff5c 30 * API parameters.
89750f35 31 *
3a8dc228
CW
32 * @throws \Civi\API\Exception\NotImplementedException
33 * @return \Civi\Api4\Generic\AbstractAction|array
d3159a21 34 */
3a8dc228 35 public static function create(string $entity, string $action, array $params) {
080b7aca 36 switch ($params['version'] ?? NULL) {
3a8dc228
CW
37 case 3:
38 return [
f27ab61d 39 'id' => self::getNextId(),
3a8dc228
CW
40 'version' => 3,
41 'params' => $params,
42 'fields' => NULL,
43 'entity' => self::normalizeEntityName($entity),
44 'action' => self::normalizeActionName($action),
45 ];
8bcc0d86
CW
46
47 case 4:
cceb7641
JS
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 $kernel = \Civi::service('civi_api_kernel');
8badd1d8
CW
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!)");
34e21ce8 57 }
8badd1d8 58 $apiRequest = call_user_func_array($callable, $e->args);
8bcc0d86
CW
59 foreach ($params as $name => $param) {
60 $setter = 'set' . ucfirst($name);
3a8dc228 61 $apiRequest->$setter($param);
d3159a21 62 }
3a8dc228 63 return $apiRequest;
d3159a21 64
3a8dc228 65 default:
080b7aca 66 throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
3a8dc228 67 }
d3159a21
TO
68 }
69
bfcb4795 70 /**
7b810209
CW
71 * Normalize entity to be CamelCase.
72 *
73 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
bfcb4795
CW
74 *
75 * @param string $entity
7c2ce2c4 76 * @return string
bfcb4795 77 */
5a3846f7 78 public static function normalizeEntityName($entity) {
74c303ca 79 return \CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel(\CRM_Utils_String::munge($entity), TRUE);
bfcb4795
CW
80 }
81
d3159a21 82 /**
7b810209 83 * Normalize api action name to be lowercase.
d3159a21 84 *
7b810209
CW
85 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
86 *
87 * @param $action
88 * @param $version
89 * @return string
d3159a21 90 */
5a3846f7 91 public static function normalizeActionName($action) {
7b810209 92 return strtolower(\CRM_Utils_String::munge($action));
d3159a21
TO
93 }
94
6e80d3a5
CW
95 public static function getNextId() {
96 return self::$nextId++;
97 }
98
89750f35 99}