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
;
20 class BasicSaveAction
extends AbstractSaveAction
{
24 * Function(array $item, BasicCreateAction $thisAction): array
29 * Basic Save constructor.
31 * @param string $entityName
32 * @param string $actionName
33 * @param callable $setter
35 public function __construct($entityName, $actionName, $setter = NULL) {
36 parent
::__construct($entityName, $actionName);
37 // Accept setter as 4th param for now, but emit deprecated warning
38 $this->setter
= func_get_args()[3] ??
NULL;
40 \CRM_Core_Error
::deprecatedWarning(__CLASS__
. ' constructor received $setter as 4th param; it should be the 3rd as the $select param has been removed');
43 $this->setter
= $setter;
48 * We pass the writeRecord function an array representing one item to write.
49 * We expect to get the same format back.
51 * @param \Civi\Api4\Generic\Result $result
53 public function _run(Result
$result) {
54 foreach ($this->records
as &$record) {
55 $record +
= $this->defaults
;
56 $this->formatWriteValues($record);
58 $this->validateValues();
59 foreach ($this->records
as $item) {
60 $result[] = $this->writeRecord($item);
63 /** @var BasicGetAction $get */
64 $get = \Civi\API\Request
::create($this->getEntityName(), 'get', ['version' => 4]);
66 ->setCheckPermissions($this->getCheckPermissions())
67 ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField()));
68 $result->exchangeArray((array) $get->execute());
73 * This Basic Save class can be used in one of two ways:
75 * 1. Use this class directly by passing a callable ($setter) to the constructor.
76 * 2. Extend this class and override this function.
78 * Either way, this function should return an array representing the one new object.
82 * @throws \Civi\API\Exception\NotImplementedException
84 protected function writeRecord($item) {
85 if (is_callable($this->setter
)) {
86 $this->addCallbackToDebugOutput($this->setter
);
87 return call_user_func($this->setter
, $item, $this);
89 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());