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 | ||
19b53e5b C |
13 | namespace Civi\Api4\Generic; |
14 | ||
15 | use Civi\API\Exception\NotImplementedException; | |
16 | ||
17 | /** | |
fc95d9a5 | 18 | * Update one or more $ENTITY with new values. |
19b53e5b | 19 | * |
fc95d9a5 | 20 | * Use the `where` clause (required) to select them. |
19b53e5b C |
21 | */ |
22 | class BasicUpdateAction extends AbstractUpdateAction { | |
23 | ||
24 | /** | |
25 | * @var callable | |
9bafff7c | 26 | * Function(array $item, BasicUpdateAction $thisAction): array |
19b53e5b C |
27 | */ |
28 | private $setter; | |
29 | ||
30 | /** | |
31 | * BasicUpdateAction constructor. | |
32 | * | |
33 | * @param string $entityName | |
34 | * @param string $actionName | |
19b53e5b | 35 | * @param callable $setter |
19b53e5b | 36 | */ |
29ab318b CW |
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; | |
41 | if ($this->setter) { | |
42 | \CRM_Core_Error::deprecatedWarning(__CLASS__ . ' constructor received $setter as 4th param; it should be the 3rd as the $select param has been removed'); | |
43 | } | |
44 | else { | |
45 | $this->setter = $setter; | |
46 | } | |
19b53e5b C |
47 | } |
48 | ||
49 | /** | |
29ab318b CW |
50 | * @param array $items |
51 | * @return array | |
19b53e5b | 52 | * @throws \API_Exception |
19b53e5b | 53 | */ |
29ab318b CW |
54 | protected function updateRecords(array $items): array { |
55 | return array_map([$this, 'writeRecord'], $items); | |
19b53e5b C |
56 | } |
57 | ||
58 | /** | |
59 | * This Basic Update class can be used in one of two ways: | |
60 | * | |
61 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
62 | * 2. Extend this class and override this function. | |
63 | * | |
64 | * Either way, this function should return an array representing the one modified object. | |
65 | * | |
66 | * @param array $item | |
67 | * @return array | |
68 | * @throws \Civi\API\Exception\NotImplementedException | |
69 | */ | |
70 | protected function writeRecord($item) { | |
71 | if (is_callable($this->setter)) { | |
32f72d83 | 72 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
73 | return call_user_func($this->setter, $item, $this); |
74 | } | |
75 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
76 | } | |
77 | ||
78 | } |