The Request::create function was broken for v4, and ActionUtil had been serving the same purpose but in a non-broken way.
* @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']) {
+ case 3:
+ return [
+ 'id' => self::$nextId++,
+ '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("CiviCRM API version {$params['version']} not found.");
+ }
}
/**
use Civi\API\Exception\NotImplementedException;
use Civi\Api4\Generic\BasicGetAction;
-use Civi\Api4\Utils\ActionUtil;
use Civi\Api4\Utils\ReflectionUtils;
/**
private function loadAction($actionName, $method = NULL) {
try {
if (!isset($this->_actions[$actionName]) && (!$this->_actionsToGet || in_array($actionName, $this->_actionsToGet))) {
- $action = ActionUtil::getAction($this->getEntityName(), $actionName);
+ $action = \Civi\API\Request::create($this->getEntityName(), $actionName, ['version' => 4]);
if (is_object($action)) {
$this->_actions[$actionName] = ['name' => $actionName];
if ($this->_isFieldSelected('description', 'comment', 'see')) {
namespace Civi\Api4\Generic;
use Civi\Api4\Utils\ReflectionUtils;
-use Civi\Api4\Utils\ActionUtil;
/**
* Base class for all api actions.
*/
public function entityFields() {
if (!$this->_entityFields) {
- $getFields = ActionUtil::getAction($this->getEntityName(), 'getFields');
+ $getFields = \Civi\API\Request::create($this->getEntityName(), 'getFields', [
+ 'version' => 4,
+ 'checkPermissions' => $this->checkPermissions,
+ 'action' => $this->getActionName(),
+ 'includeCustom' => FALSE,
+ ]);
$result = new Result();
- if (method_exists($this, 'getBaoName')) {
- $getFields->setIncludeCustom(FALSE);
- }
- $getFields
- ->setCheckPermissions($this->checkPermissions)
- ->setAction($this->getActionName())
- ->_run($result);
+ $getFields->_run($result);
$this->_entityFields = (array) $result->indexBy('name');
}
return $this->_entityFields;
namespace Civi\Api4\Generic;
use Civi\API\Exception\NotImplementedException;
-use Civi\Api4\Utils\ActionUtil;
/**
* Lists information about fields for the $ENTITY entity.
*/
public function _run(Result $result) {
try {
- $actionClass = ActionUtil::getAction($this->getEntityName(), $this->getAction());
+ $actionClass = \Civi\API\Request::create($this->getEntityName(), $this->getAction(), ['version' => 4]);
}
catch (NotImplementedException $e) {
}
return $this;
}
+ /**
+ * @param bool $includeCustom
+ * @return $this
+ */
+ public function setIncludeCustom(bool $includeCustom) {
+ // Be forgiving if the param doesn't exist and don't throw an exception
+ if (property_exists($this, 'includeCustom')) {
+ $this->includeCustom = $includeCustom;
+ }
+ return $this;
+ }
+
public function fields() {
return [
[
namespace Civi\Api4\Generic;
-use Civi\Api4\Utils\ActionUtil;
-
/**
* Replaces an existing set of $ENTITIES with a new one.
*
$idField = $this->getSelect()[0];
$toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array::collect($idField, $this->records))));
- $saveAction = ActionUtil::getAction($this->getEntityName(), 'save');
+ $saveAction = \Civi\API\Request::create($this->getEntityName(), 'save', ['version' => 4]);
$saveAction
->setCheckPermissions($this->getCheckPermissions())
->setReload($this->reload)
namespace Civi\Api4\Generic;
use Civi\API\Exception\NotImplementedException;
-use Civi\Api4\Utils\ActionUtil;
/**
* $ACTION one or more $ENTITIES.
}
if ($this->reload) {
/** @var BasicGetAction $get */
- $get = ActionUtil::getAction($this->getEntityName(), 'get');
+ $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]);
$get
->setCheckPermissions($this->getCheckPermissions())
->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField()));
/**
* @inheritDoc
- * @method $this setIncludeCustom(bool $value)
* @method bool getIncludeCustom()
*/
class DAOGetFieldsAction extends BasicGetFieldsAction {
+++ /dev/null
-<?php
-
-/*
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC. All rights reserved. |
- | |
- | This work is published under the GNU AGPLv3 license with some |
- | permitted exceptions and without any warranty. For full license |
- | and copyright information, see https://civicrm.org/licensing |
- +--------------------------------------------------------------------+
- */
-
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
- */
-
-
-namespace Civi\Api4\Utils;
-
-class ActionUtil {
-
- /**
- * @param $entityName
- * @param $actionName
- * @return \Civi\Api4\Generic\AbstractAction
- * @throws \Civi\API\Exception\NotImplementedException
- */
- public static function getAction($entityName, $actionName) {
- // For custom pseudo-entities
- if (strpos($entityName, 'Custom_') === 0) {
- return \Civi\Api4\CustomValue::$actionName(substr($entityName, 7));
- }
- else {
- $callable = ["\\Civi\\Api4\\$entityName", $actionName];
- if (!is_callable($callable)) {
- throw new \Civi\API\Exception\NotImplementedException("API ($entityName, $actionName) does not exist (join the API team and implement it!)");
- }
- return call_user_func($callable);
- }
- }
-
-}
* @throws \Civi\API\Exception\NotImplementedException
*/
function civicrm_api4(string $entity, string $action, array $params = [], $index = NULL) {
- $apiCall = \Civi\Api4\Utils\ActionUtil::getAction($entity, $action);
$indexField = $index && is_string($index) && !CRM_Utils_Rule::integer($index) ? $index : NULL;
$removeIndexField = FALSE;
$params['select'][] = $indexField;
$removeIndexField = TRUE;
}
- foreach ($params as $name => $param) {
- $setter = 'set' . ucfirst($name);
- $apiCall->$setter($param);
- }
+ $apiCall = \Civi\API\Request::create($entity, $action, ['version' => 4] + $params);
if ($index && is_array($index)) {
$indexCol = reset($index);