Api4 - add more debug output
[civicrm-core.git] / Civi / Api4 / Generic / BasicUpdateAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace Civi\Api4\Generic;
23
24use Civi\API\Exception\NotImplementedException;
25
26/**
27 * Update one or more records with new values.
28 *
29 * Use the where clause (required) to select them.
30 */
31class BasicUpdateAction extends AbstractUpdateAction {
32
33 /**
34 * @var callable
35 *
36 * Function(array $item, BasicUpdateAction $thisAction) => array
37 */
38 private $setter;
39
40 /**
41 * BasicUpdateAction constructor.
42 *
43 * @param string $entityName
44 * @param string $actionName
45 * @param string|array $select
46 * One or more fields to select from each matching item.
47 * @param callable $setter
48 * Function(array $item, BasicUpdateAction $thisAction) => array
49 */
50 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
51 parent::__construct($entityName, $actionName, $select);
52 $this->setter = $setter;
53 }
54
55 /**
56 * We pass the writeRecord function an array representing one item to update.
57 * We expect to get the same format back.
58 *
59 * @param \Civi\Api4\Generic\Result $result
60 * @throws \API_Exception
61 * @throws \Civi\API\Exception\NotImplementedException
62 */
63 public function _run(Result $result) {
64 foreach ($this->getBatchRecords() as $item) {
65 $result[] = $this->writeRecord($this->values + $item);
66 }
67
68 if (!$result->count()) {
69 throw new \API_Exception('Cannot ' . $this->getActionName() . ' ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
70 }
71 }
72
73 /**
74 * This Basic Update class can be used in one of two ways:
75 *
76 * 1. Use this class directly by passing a callable ($setter) to the constructor.
77 * 2. Extend this class and override this function.
78 *
79 * Either way, this function should return an array representing the one modified object.
80 *
81 * @param array $item
82 * @return array
83 * @throws \Civi\API\Exception\NotImplementedException
84 */
85 protected function writeRecord($item) {
86 if (is_callable($this->setter)) {
32f72d83 87 $this->addCallbackToDebugOutput($this->setter);
19b53e5b
C
88 return call_user_func($this->setter, $item, $this);
89 }
90 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
91 }
92
93}