blob: 40c93624e34aad2914d2cbd8863b17fb9a861f02 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<?php
namespace Civi\Api4\Generic;
use Civi\API\Exception\NotImplementedException;
/**
* Update one or more records with new values.
*
* Use the where clause (required) to select them.
*/
class BasicUpdateAction extends AbstractUpdateAction {
/**
* @var callable
*
* Function(array $item, BasicUpdateAction $thisAction) => array
*/
private $setter;
/**
* BasicUpdateAction constructor.
*
* @param string $entityName
* @param string $actionName
* @param string|array $select
* One or more fields to select from each matching item.
* @param callable $setter
* Function(array $item, BasicUpdateAction $thisAction) => array
*/
public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
parent::__construct($entityName, $actionName, $select);
$this->setter = $setter;
}
/**
* We pass the writeRecord function an array representing one item to update.
* We expect to get the same format back.
*
* @param \Civi\Api4\Generic\Result $result
*/
public function _run(Result $result) {
foreach ($this->getBatchRecords() as $item) {
$result[] = $this->writeRecord($this->values + $item);
}
}
/**
* This Basic Update class can be used in one of two ways:
*
* 1. Use this class directly by passing a callable ($setter) to the constructor.
* 2. Extend this class and override this function.
*
* Either way, this function should return an array representing the one modified object.
*
* @param array $item
* @return array
* @throws \Civi\API\Exception\NotImplementedException
*/
protected function writeRecord($item) {
if (is_callable($this->setter)) {
return call_user_func($this->setter, $item, $this);
}
throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
}
}
|