From 1f34d3239d12940a154b2b2a123501799b45b0a3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 23 Mar 2023 09:05:39 -0400 Subject: [PATCH] Api4 - Prevent developer error mixing up the `addValue` and `addWhere` functions --- Civi/Api4/Action/Setting/Set.php | 13 +--- Civi/Api4/Generic/AbstractCreateAction.php | 21 +----- Civi/Api4/Generic/AbstractUpdateAction.php | 23 +------ Civi/Api4/Generic/BasicGetFieldsAction.php | 13 +--- Civi/Api4/Generic/CheckAccessAction.php | 13 +--- Civi/Api4/Generic/Traits/GetSetValueTrait.php | 64 +++++++++++++++++++ 6 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 Civi/Api4/Generic/Traits/GetSetValueTrait.php diff --git a/Civi/Api4/Action/Setting/Set.php b/Civi/Api4/Action/Setting/Set.php index cd8413331f..753a51876e 100644 --- a/Civi/Api4/Action/Setting/Set.php +++ b/Civi/Api4/Action/Setting/Set.php @@ -22,6 +22,8 @@ use Civi\Api4\Generic\Result; */ class Set extends AbstractSettingAction { + use \Civi\Api4\Generic\Traits\GetSetValueTrait; + /** * Setting names/values to set. * @@ -51,15 +53,4 @@ class Set extends AbstractSettingAction { } } - /** - * Add an item to the values array - * @param string $settingName - * @param mixed $value - * @return $this - */ - public function addValue($settingName, $value) { - $this->values[$settingName] = $value; - return $this; - } - } diff --git a/Civi/Api4/Generic/AbstractCreateAction.php b/Civi/Api4/Generic/AbstractCreateAction.php index c1f88ad9ac..b807f38a31 100644 --- a/Civi/Api4/Generic/AbstractCreateAction.php +++ b/Civi/Api4/Generic/AbstractCreateAction.php @@ -26,6 +26,8 @@ use Civi\Api4\Utils\CoreUtil; */ abstract class AbstractCreateAction extends AbstractAction { + use Traits\GetSetValueTrait; + /** * Field values to set for the new $ENTITY. * @@ -33,25 +35,6 @@ abstract class AbstractCreateAction extends AbstractAction { */ protected $values = []; - /** - * @param string $fieldName - * @return mixed|null - */ - public function getValue(string $fieldName) { - return $this->values[$fieldName] ?? NULL; - } - - /** - * Add a field value. - * @param string $fieldName - * @param mixed $value - * @return $this - */ - public function addValue(string $fieldName, $value) { - $this->values[$fieldName] = $value; - return $this; - } - /** * @throws \CRM_Core_Exception * @throws \Civi\API\Exception\UnauthorizedException diff --git a/Civi/Api4/Generic/AbstractUpdateAction.php b/Civi/Api4/Generic/AbstractUpdateAction.php index d4b78a13eb..4c5942fe37 100644 --- a/Civi/Api4/Generic/AbstractUpdateAction.php +++ b/Civi/Api4/Generic/AbstractUpdateAction.php @@ -28,6 +28,8 @@ use Civi\Api4\Utils\CoreUtil; */ abstract class AbstractUpdateAction extends AbstractBatchAction { + use Traits\GetSetValueTrait; + /** * Field values to update. * @@ -107,27 +109,6 @@ abstract class AbstractUpdateAction extends AbstractBatchAction { $result->exchangeArray($this->updateRecords($items)); } - /** - * @param string $fieldName - * - * @return mixed|null - */ - public function getValue(string $fieldName) { - return $this->values[$fieldName] ?? NULL; - } - - /** - * Add an item to the values array. - * - * @param string $fieldName - * @param mixed $value - * @return $this - */ - public function addValue(string $fieldName, $value) { - $this->values[$fieldName] = $value; - return $this; - } - /** * @throws \CRM_Core_Exception */ diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 0b10f4f6a1..82dd6e0e5f 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -32,6 +32,8 @@ use Civi\Api4\Utils\CoreUtil; */ class BasicGetFieldsAction extends BasicGetAction { + use Traits\GetSetValueTrait; + /** * Fetch option lists for fields? * @@ -206,17 +208,6 @@ class BasicGetFieldsAction extends BasicGetAction { return $sub[$this->action] ?? $this->action; } - /** - * Add an item to the values array - * @param string $fieldName - * @param mixed $value - * @return $this - */ - public function addValue(string $fieldName, $value) { - $this->values[$fieldName] = $value; - return $this; - } - /** * Helper function to retrieve options from an option group (for non-DAO entities). * diff --git a/Civi/Api4/Generic/CheckAccessAction.php b/Civi/Api4/Generic/CheckAccessAction.php index a3cd5dc0a3..21094e15cc 100644 --- a/Civi/Api4/Generic/CheckAccessAction.php +++ b/Civi/Api4/Generic/CheckAccessAction.php @@ -24,6 +24,8 @@ use Civi\Api4\Utils\CoreUtil; */ class CheckAccessAction extends AbstractAction { + use Traits\GetSetValueTrait; + /** * @var string * @required @@ -59,15 +61,4 @@ class CheckAccessAction extends AbstractAction { return TRUE; } - /** - * Add an item to the values array - * @param string $fieldName - * @param mixed $value - * @return $this - */ - public function addValue(string $fieldName, $value) { - $this->values[$fieldName] = $value; - return $this; - } - } diff --git a/Civi/Api4/Generic/Traits/GetSetValueTrait.php b/Civi/Api4/Generic/Traits/GetSetValueTrait.php new file mode 100644 index 0000000000..fb9649eb5d --- /dev/null +++ b/Civi/Api4/Generic/Traits/GetSetValueTrait.php @@ -0,0 +1,64 @@ + 2) { + throw new \CRM_Core_Exception('APIv4 function `addValue` incorrectly called with 3 arguments.'); + } + $this->values[$fieldName] = $value; + return $this; + } + + /** + * Overwrite all values + * + * @param array $values + * @return $this + */ + public function setValues(array $values) { + $this->values = $values; + return $this; + } + + /** + * Retrieve a single value + * + * @param string $fieldName + * @return mixed|null + */ + public function getValue(string $fieldName) { + return $this->values[$fieldName] ?? NULL; + } + + /** + * @return array + */ + public function getValues() { + return $this->values; + } + +} -- 2.25.1