Merge pull request #14922 from civicrm/5.16
[civicrm-core.git] / Civi / API / Request.php
CommitLineData
d3159a21
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
d3159a21 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
d3159a21
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
d3159a21
TO
27namespace Civi\API;
28
6550386a
EM
29/**
30 * Class Request
31 * @package Civi\API
32 */
d3159a21 33class Request {
5558f278
TO
34 private static $nextId = 1;
35
d3159a21
TO
36 /**
37 * Create a formatted/normalized request object.
38 *
39 * @param string $entity
8882ff5c 40 * API entity name.
d3159a21 41 * @param string $action
8882ff5c 42 * API action name.
d3159a21 43 * @param array $params
8882ff5c 44 * API parameters.
d3159a21 45 * @param mixed $extra
8882ff5c 46 * Who knows? ...
89750f35
EM
47 *
48 * @throws \API_Exception
a6c01b45
CW
49 * @return array
50 * the request descriptor; keys:
d3159a21
TO
51 * - version: int
52 * - entity: string
53 * - action: string
54 * - params: array (string $key => mixed $value) [deprecated in v4]
55 * - extra: unspecified
56 * - fields: NULL|array (string $key => array $fieldSpec)
57 * - options: \CRM_Utils_OptionBag derived from params [v4-only]
58 * - data: \CRM_Utils_OptionBag derived from params [v4-only]
59 * - chains: unspecified derived from params [v4-only]
60 */
8bcc0d86 61 public static function create($entity, $action, $params, $extra = NULL) {
7b810209 62 $version = \CRM_Utils_Array::value('version', $params);
8bcc0d86 63 switch ($version) {
7b810209 64 default:
c64f69d9 65 $apiRequest = [];
8bcc0d86 66 $apiRequest['id'] = self::$nextId++;
7b810209 67 $apiRequest['version'] = (int) $version;
8bcc0d86
CW
68 $apiRequest['params'] = $params;
69 $apiRequest['extra'] = $extra;
70 $apiRequest['fields'] = NULL;
7b810209
CW
71 $apiRequest['entity'] = self::normalizeEntityName($entity, $apiRequest['version']);
72 $apiRequest['action'] = self::normalizeActionName($action, $apiRequest['version']);
8bcc0d86
CW
73 return $apiRequest;
74
75 case 4:
c64f69d9 76 $callable = ["Civi\\Api4\\$entity", $action];
34e21ce8
CW
77 if (!is_callable($callable)) {
78 throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
79 }
80 $apiCall = call_user_func($callable);
7b810209 81 $apiRequest['id'] = self::$nextId++;
8bcc0d86
CW
82 unset($params['version']);
83 foreach ($params as $name => $param) {
84 $setter = 'set' . ucfirst($name);
85 $apiCall->$setter($param);
d3159a21 86 }
8bcc0d86 87 return $apiCall;
d3159a21
TO
88 }
89
d3159a21
TO
90 }
91
bfcb4795 92 /**
7b810209
CW
93 * Normalize entity to be CamelCase.
94 *
95 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
bfcb4795
CW
96 *
97 * @param string $entity
7c2ce2c4
TO
98 * @param int $version
99 * @return string
bfcb4795 100 */
7c2ce2c4 101 public static function normalizeEntityName($entity, $version) {
7b810209 102 return \CRM_Utils_String::convertStringToCamel(\CRM_Utils_String::munge($entity));
bfcb4795
CW
103 }
104
d3159a21 105 /**
7b810209 106 * Normalize api action name to be lowercase.
d3159a21 107 *
7b810209
CW
108 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
109 *
110 * @param $action
111 * @param $version
112 * @return string
d3159a21 113 */
7b810209
CW
114 public static function normalizeActionName($action, $version) {
115 return strtolower(\CRM_Utils_String::munge($action));
d3159a21
TO
116 }
117
6e80d3a5
CW
118 public static function getNextId() {
119 return self::$nextId++;
120 }
121
89750f35 122}