Merge pull request #16642 from eileenmcnaughton/ss2
[civicrm-core.git] / Civi / API / Request.php
index 11c32ffc5e0adfc2bd810826535653a5b354b51a..22fa382a7104c953555db1fa02cde56b6e8dc1ca 100644 (file)
@@ -27,46 +27,42 @@ class Request {
    * @param array $params
    *   API parameters.
    *
-   * @throws \API_Exception
-   * @return array
-   *   the request descriptor; keys:
-   *   - version: int
-   *   - entity: string
-   *   - action: string
-   *   - params: array (string $key => mixed $value) [deprecated in v4]
-   *   - fields: NULL|array (string $key => array $fieldSpec)
-   *   - options: \CRM_Utils_OptionBag derived from params [v4-only]
-   *   - data: \CRM_Utils_OptionBag derived from params [v4-only]
-   *   - chains: unspecified derived from params [v4-only]
+   * @throws \Civi\API\Exception\NotImplementedException
+   * @return \Civi\Api4\Generic\AbstractAction|array
    */
-  public static function create($entity, $action, $params) {
-    $version = \CRM_Utils_Array::value('version', $params);
-    switch ($version) {
-      default:
-        $apiRequest = [];
-        $apiRequest['id'] = self::$nextId++;
-        $apiRequest['version'] = (int) $version;
-        $apiRequest['params'] = $params;
-        $apiRequest['fields'] = NULL;
-        $apiRequest['entity'] = self::normalizeEntityName($entity);
-        $apiRequest['action'] = self::normalizeActionName($action);
-        return $apiRequest;
+  public static function create(string $entity, string $action, array $params) {
+    switch ($params['version'] ?? NULL) {
+      case 3:
+        return [
+          'id' => self::getNextId(),
+          'version' => 3,
+          'params' => $params,
+          'fields' => NULL,
+          'entity' => self::normalizeEntityName($entity),
+          'action' => self::normalizeActionName($action),
+        ];
 
       case 4:
-        $callable = ["Civi\\Api4\\$entity", $action];
-        if (!is_callable($callable)) {
-          throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
+        // For custom pseudo-entities
+        if (strpos($entity, 'Custom_') === 0) {
+          $apiRequest = \Civi\Api4\CustomValue::$action(substr($entity, 7));
+        }
+        else {
+          $callable = ["\\Civi\\Api4\\$entity", $action];
+          if (!is_callable($callable)) {
+            throw new \Civi\API\Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
+          }
+          $apiRequest = call_user_func($callable);
         }
-        $apiCall = call_user_func($callable);
-        $apiRequest['id'] = self::$nextId++;
-        unset($params['version']);
         foreach ($params as $name => $param) {
           $setter = 'set' . ucfirst($name);
-          $apiCall->$setter($param);
+          $apiRequest->$setter($param);
         }
-        return $apiCall;
-    }
+        return $apiRequest;
 
+      default:
+        throw new \Civi\API\Exception\NotImplementedException("Unknown api version");
+    }
   }
 
   /**