4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
15 use Civi\API\Exception\NotImplementedException
;
18 * Update one or more $ENTITY with new values.
20 * Use the `where` clause (required) to select them.
22 class BasicUpdateAction
extends AbstractUpdateAction
{
26 * Function(array $item, BasicUpdateAction $thisAction): array
31 * BasicUpdateAction constructor.
33 * @param string $entityName
34 * @param string $actionName
35 * @param callable $setter
37 public function __construct($entityName, $actionName, $setter = NULL) {
38 parent
::__construct($entityName, $actionName);
39 // Accept setter as 4th param for now, but emit deprecated warning
40 $this->setter
= func_get_args()[3] ??
NULL;
42 \CRM_Core_Error
::deprecatedWarning(__CLASS__
. ' constructor received $setter as 4th param; it should be the 3rd as the $select param has been removed');
45 $this->setter
= $setter;
52 * @throws \API_Exception
54 protected function updateRecords(array $items): array {
55 return array_map([$this, 'writeRecord'], $items);
59 * This Basic Update class can be used in one of two ways:
61 * 1. Use this class directly by passing a callable ($setter) to the constructor.
62 * 2. Extend this class and override this function.
64 * Either way, this function should return an array representing the one modified object.
68 * @throws \Civi\API\Exception\NotImplementedException
70 protected function writeRecord($item) {
71 if (is_callable($this->setter
)) {
72 $this->addCallbackToDebugOutput($this->setter
);
73 return call_user_func($this->setter
, $item, $this);
75 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());