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 | * Create a new $ENTITY from supplied values. |
19b53e5b | 19 | * |
fc95d9a5 | 20 | * This action will create 1 new $ENTITY. |
e3c6d5ff | 21 | * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that. |
19b53e5b C |
22 | */ |
23 | class BasicCreateAction extends AbstractCreateAction { | |
24 | ||
25 | /** | |
26 | * @var callable | |
9bafff7c | 27 | * Function(array $item, BasicCreateAction $thisAction): array |
19b53e5b C |
28 | */ |
29 | private $setter; | |
30 | ||
31 | /** | |
32 | * Basic Create constructor. | |
33 | * | |
34 | * @param string $entityName | |
35 | * @param string $actionName | |
36 | * @param callable $setter | |
19b53e5b C |
37 | */ |
38 | public function __construct($entityName, $actionName, $setter = NULL) { | |
39 | parent::__construct($entityName, $actionName); | |
40 | $this->setter = $setter; | |
41 | } | |
42 | ||
43 | /** | |
44 | * We pass the writeRecord function an array representing one item to write. | |
45 | * We expect to get the same format back. | |
46 | * | |
47 | * @param \Civi\Api4\Generic\Result $result | |
48 | */ | |
49 | public function _run(Result $result) { | |
961e974c | 50 | $this->formatWriteValues($this->values); |
19b53e5b C |
51 | $this->validateValues(); |
52 | $result->exchangeArray([$this->writeRecord($this->values)]); | |
53 | } | |
54 | ||
55 | /** | |
56 | * This Basic Create class can be used in one of two ways: | |
57 | * | |
58 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
59 | * 2. Extend this class and override this function. | |
60 | * | |
61 | * Either way, this function should return an array representing the one new object. | |
62 | * | |
63 | * @param array $item | |
64 | * @return array | |
65 | * @throws \Civi\API\Exception\NotImplementedException | |
66 | */ | |
67 | protected function writeRecord($item) { | |
68 | if (is_callable($this->setter)) { | |
32f72d83 | 69 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
70 | return call_user_func($this->setter, $item, $this); |
71 | } | |
72 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
73 | } | |
74 | ||
75 | } |