Delete outdated/unused crmExample Angular module
[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 \Civi\API\Exception\NotImplementedException
31 * @return \Civi\Api4\Generic\AbstractAction|array
32 */
33 public static function create(string $entity, string $action, array $params) {
34 switch ($params['version'] ?? NULL) {
35 case 3:
36 return [
37 'id' => self::getNextId(),
38 'version' => 3,
39 'params' => $params,
40 'fields' => NULL,
41 'entity' => self::normalizeEntityName($entity),
42 'action' => self::normalizeActionName($action),
43 ];
44
45 case 4:
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);
56 }
57 foreach ($params as $name => $param) {
58 $setter = 'set' . ucfirst($name);
59 $apiRequest->$setter($param);
60 }
61 return $apiRequest;
62
63 default:
64 throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
65 }
66 }
67
68 /**
69 * Normalize entity to be CamelCase.
70 *
71 * APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
72 *
73 * @param string $entity
74 * @return string
75 */
76 public static function normalizeEntityName($entity) {
77 return \CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel(\CRM_Utils_String::munge($entity), TRUE);
78 }
79
80 /**
81 * Normalize api action name to be lowercase.
82 *
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
88 */
89 public static function normalizeActionName($action) {
90 return strtolower(\CRM_Utils_String::munge($action));
91 }
92
93 public static function getNextId() {
94 return self::$nextId++;
95 }
96
97 }