Commit | Line | Data |
---|---|---|
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 |
11 | namespace Civi\API; |
12 | ||
6550386a EM |
13 | /** |
14 | * Class Request | |
15 | * @package Civi\API | |
16 | */ | |
d3159a21 | 17 | class 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. |
d3159a21 | 29 | * @param mixed $extra |
8882ff5c | 30 | * Who knows? ... |
89750f35 EM |
31 | * |
32 | * @throws \API_Exception | |
a6c01b45 CW |
33 | * @return array |
34 | * the request descriptor; keys: | |
d3159a21 TO |
35 | * - version: int |
36 | * - entity: string | |
37 | * - action: string | |
38 | * - params: array (string $key => mixed $value) [deprecated in v4] | |
39 | * - extra: unspecified | |
40 | * - fields: NULL|array (string $key => array $fieldSpec) | |
41 | * - options: \CRM_Utils_OptionBag derived from params [v4-only] | |
42 | * - data: \CRM_Utils_OptionBag derived from params [v4-only] | |
43 | * - chains: unspecified derived from params [v4-only] | |
44 | */ | |
8bcc0d86 | 45 | public static function create($entity, $action, $params, $extra = NULL) { |
7b810209 | 46 | $version = \CRM_Utils_Array::value('version', $params); |
8bcc0d86 | 47 | switch ($version) { |
7b810209 | 48 | default: |
c64f69d9 | 49 | $apiRequest = []; |
8bcc0d86 | 50 | $apiRequest['id'] = self::$nextId++; |
7b810209 | 51 | $apiRequest['version'] = (int) $version; |
8bcc0d86 CW |
52 | $apiRequest['params'] = $params; |
53 | $apiRequest['extra'] = $extra; | |
54 | $apiRequest['fields'] = NULL; | |
7b810209 CW |
55 | $apiRequest['entity'] = self::normalizeEntityName($entity, $apiRequest['version']); |
56 | $apiRequest['action'] = self::normalizeActionName($action, $apiRequest['version']); | |
8bcc0d86 CW |
57 | return $apiRequest; |
58 | ||
59 | case 4: | |
c64f69d9 | 60 | $callable = ["Civi\\Api4\\$entity", $action]; |
34e21ce8 CW |
61 | if (!is_callable($callable)) { |
62 | throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)"); | |
63 | } | |
64 | $apiCall = call_user_func($callable); | |
7b810209 | 65 | $apiRequest['id'] = self::$nextId++; |
8bcc0d86 CW |
66 | unset($params['version']); |
67 | foreach ($params as $name => $param) { | |
68 | $setter = 'set' . ucfirst($name); | |
69 | $apiCall->$setter($param); | |
d3159a21 | 70 | } |
8bcc0d86 | 71 | return $apiCall; |
d3159a21 TO |
72 | } |
73 | ||
d3159a21 TO |
74 | } |
75 | ||
bfcb4795 | 76 | /** |
7b810209 CW |
77 | * Normalize entity to be CamelCase. |
78 | * | |
79 | * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores. | |
bfcb4795 CW |
80 | * |
81 | * @param string $entity | |
7c2ce2c4 TO |
82 | * @param int $version |
83 | * @return string | |
bfcb4795 | 84 | */ |
7c2ce2c4 | 85 | public static function normalizeEntityName($entity, $version) { |
7b810209 | 86 | return \CRM_Utils_String::convertStringToCamel(\CRM_Utils_String::munge($entity)); |
bfcb4795 CW |
87 | } |
88 | ||
d3159a21 | 89 | /** |
7b810209 | 90 | * Normalize api action name to be lowercase. |
d3159a21 | 91 | * |
7b810209 CW |
92 | * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores. |
93 | * | |
94 | * @param $action | |
95 | * @param $version | |
96 | * @return string | |
d3159a21 | 97 | */ |
7b810209 CW |
98 | public static function normalizeActionName($action, $version) { |
99 | return strtolower(\CRM_Utils_String::munge($action)); | |
d3159a21 TO |
100 | } |
101 | ||
6e80d3a5 CW |
102 | public static function getNextId() { |
103 | return self::$nextId++; | |
104 | } | |
105 | ||
89750f35 | 106 | } |