Merge pull request #17813 from JKingsnorth/patch-15
[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
6550386a
EM
13/**
14 * Class Request
15 * @package Civi\API
16 */
d3159a21 17class Request {
5558f278
TO
18 private static $nextId = 1;
19
d3159a21
TO
20 /**
21 * Create a formatted/normalized request object.
22 *
23 * @param string $entity
8882ff5c 24 * API entity name.
d3159a21 25 * @param string $action
8882ff5c 26 * API action name.
d3159a21 27 * @param array $params
8882ff5c 28 * API parameters.
89750f35 29 *
3a8dc228
CW
30 * @throws \Civi\API\Exception\NotImplementedException
31 * @return \Civi\Api4\Generic\AbstractAction|array
d3159a21 32 */
3a8dc228 33 public static function create(string $entity, string $action, array $params) {
080b7aca 34 switch ($params['version'] ?? NULL) {
3a8dc228
CW
35 case 3:
36 return [
f27ab61d 37 'id' => self::getNextId(),
3a8dc228
CW
38 'version' => 3,
39 'params' => $params,
40 'fields' => NULL,
41 'entity' => self::normalizeEntityName($entity),
42 'action' => self::normalizeActionName($action),
43 ];
8bcc0d86
CW
44
45 case 4:
3a8dc228
CW
46 // For custom pseudo-entities
47 if (strpos($entity, 'Custom_') === 0) {
48 $apiRequest = \Civi\Api4\CustomValue::$action(substr($entity, 7));
49 }
50 else {
51 $callable = ["\\Civi\\Api4\\$entity", $action];
52 if (!is_callable($callable)) {
53 throw new \Civi\API\Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
54 }
55 $apiRequest = call_user_func($callable);
34e21ce8 56 }
8bcc0d86
CW
57 foreach ($params as $name => $param) {
58 $setter = 'set' . ucfirst($name);
3a8dc228 59 $apiRequest->$setter($param);
d3159a21 60 }
3a8dc228 61 return $apiRequest;
d3159a21 62
3a8dc228 63 default:
080b7aca 64 throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
3a8dc228 65 }
d3159a21
TO
66 }
67
bfcb4795 68 /**
7b810209
CW
69 * Normalize entity to be CamelCase.
70 *
71 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
bfcb4795
CW
72 *
73 * @param string $entity
7c2ce2c4 74 * @return string
bfcb4795 75 */
5a3846f7 76 public static function normalizeEntityName($entity) {
74c303ca 77 return \CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel(\CRM_Utils_String::munge($entity), TRUE);
bfcb4795
CW
78 }
79
d3159a21 80 /**
7b810209 81 * Normalize api action name to be lowercase.
d3159a21 82 *
7b810209
CW
83 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
84 *
85 * @param $action
86 * @param $version
87 * @return string
d3159a21 88 */
5a3846f7 89 public static function normalizeActionName($action) {
7b810209 90 return strtolower(\CRM_Utils_String::munge($action));
d3159a21
TO
91 }
92
6e80d3a5
CW
93 public static function getNextId() {
94 return self::$nextId++;
95 }
96
89750f35 97}