Commit | Line | Data |
---|---|---|
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 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace Civi\Api4\Generic; |
21 | ||
22 | use Civi\API\Exception\NotImplementedException; | |
23 | ||
24 | /** | |
fc95d9a5 | 25 | * Update one or more $ENTITY with new values. |
19b53e5b | 26 | * |
fc95d9a5 | 27 | * Use the `where` clause (required) to select them. |
19b53e5b C |
28 | */ |
29 | class BasicUpdateAction extends AbstractUpdateAction { | |
30 | ||
31 | /** | |
32 | * @var callable | |
33 | * | |
34 | * Function(array $item, BasicUpdateAction $thisAction) => array | |
35 | */ | |
36 | private $setter; | |
37 | ||
38 | /** | |
39 | * BasicUpdateAction constructor. | |
40 | * | |
41 | * @param string $entityName | |
42 | * @param string $actionName | |
43 | * @param string|array $select | |
44 | * One or more fields to select from each matching item. | |
45 | * @param callable $setter | |
46 | * Function(array $item, BasicUpdateAction $thisAction) => array | |
47 | */ | |
48 | public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) { | |
49 | parent::__construct($entityName, $actionName, $select); | |
50 | $this->setter = $setter; | |
51 | } | |
52 | ||
53 | /** | |
54 | * We pass the writeRecord function an array representing one item to update. | |
55 | * We expect to get the same format back. | |
56 | * | |
57 | * @param \Civi\Api4\Generic\Result $result | |
58 | * @throws \API_Exception | |
59 | * @throws \Civi\API\Exception\NotImplementedException | |
60 | */ | |
61 | public function _run(Result $result) { | |
961e974c | 62 | $this->formatWriteValues($this->values); |
19b53e5b C |
63 | foreach ($this->getBatchRecords() as $item) { |
64 | $result[] = $this->writeRecord($this->values + $item); | |
65 | } | |
19b53e5b C |
66 | } |
67 | ||
68 | /** | |
69 | * This Basic Update class can be used in one of two ways: | |
70 | * | |
71 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
72 | * 2. Extend this class and override this function. | |
73 | * | |
74 | * Either way, this function should return an array representing the one modified object. | |
75 | * | |
76 | * @param array $item | |
77 | * @return array | |
78 | * @throws \Civi\API\Exception\NotImplementedException | |
79 | */ | |
80 | protected function writeRecord($item) { | |
81 | if (is_callable($this->setter)) { | |
32f72d83 | 82 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
83 | return call_user_func($this->setter, $item, $this); |
84 | } | |
85 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
86 | } | |
87 | ||
88 | } |