Merge pull request #15330 from mattwire/paymentprocessor_testmodelivemode
[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 /**
14 * Class Request
15 * @package Civi\API
16 */
17 class Request {
18 private static $nextId = 1;
19
20 /**
21 * Create a formatted/normalized request object.
22 *
23 * @param string $entity
24 * API entity name.
25 * @param string $action
26 * API action name.
27 * @param array $params
28 * API parameters.
29 *
30 * @throws \API_Exception
31 * @return array
32 * the request descriptor; keys:
33 * - version: int
34 * - entity: string
35 * - action: string
36 * - params: array (string $key => mixed $value) [deprecated in v4]
37 * - fields: NULL|array (string $key => array $fieldSpec)
38 * - options: \CRM_Utils_OptionBag derived from params [v4-only]
39 * - data: \CRM_Utils_OptionBag derived from params [v4-only]
40 * - chains: unspecified derived from params [v4-only]
41 */
42 public static function create($entity, $action, $params) {
43 $version = \CRM_Utils_Array::value('version', $params);
44 switch ($version) {
45 default:
46 $apiRequest = [];
47 $apiRequest['id'] = self::$nextId++;
48 $apiRequest['version'] = (int) $version;
49 $apiRequest['params'] = $params;
50 $apiRequest['fields'] = NULL;
51 $apiRequest['entity'] = self::normalizeEntityName($entity);
52 $apiRequest['action'] = self::normalizeActionName($action);
53 return $apiRequest;
54
55 case 4:
56 $callable = ["Civi\\Api4\\$entity", $action];
57 if (!is_callable($callable)) {
58 throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
59 }
60 $apiCall = call_user_func($callable);
61 $apiRequest['id'] = self::$nextId++;
62 unset($params['version']);
63 foreach ($params as $name => $param) {
64 $setter = 'set' . ucfirst($name);
65 $apiCall->$setter($param);
66 }
67 return $apiCall;
68 }
69
70 }
71
72 /**
73 * Normalize entity to be CamelCase.
74 *
75 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
76 *
77 * @param string $entity
78 * @return string
79 */
80 public static function normalizeEntityName($entity) {
81 return \CRM_Utils_String::convertStringToCamel(\CRM_Utils_String::munge($entity));
82 }
83
84 /**
85 * Normalize api action name to be lowercase.
86 *
87 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
88 *
89 * @param $action
90 * @param $version
91 * @return string
92 */
93 public static function normalizeActionName($action) {
94 return strtolower(\CRM_Utils_String::munge($action));
95 }
96
97 public static function getNextId() {
98 return self::$nextId++;
99 }
100
101 }